# C语言数组小结

2017-01-01 10:29:39来源:作者:人点击

`[code]# include <stdio.h>int main(void){ int a[4] = {1,2,3,4};  //数组的名字是a,数组的长度是4 for (int i=0; i<4; i++) {printf("%d/n",a[i]); } return 0;}import java.util.ArrayList;public class App1{  public static void main(String[] args)  { int[] a = {1,2,3,4};  //定义一个一维数组,变量的名字分别是a[0]、a[1]、a[2]…… for (int i=0; i<a.length; i++) { System.out.println(a[i]); }}}`

`[code]# include <stdio.h>//本程序用来测试break的用法int main(void){ int a[5]; //定义一个一维数组,共含有5个元素 scanf("%d",&a[0]); printf("a[0]的数值是:%d/n",a[0]); scanf("%d",&a[3]); printf("a[3]的数值是:%d/n",a[3]); for (int i=0; i<5; i++) { printf("%d/t",a[i]); } return 0;}`

`[code]# include <stdio.h>//本程序用来测试一维数组的倒置int main(void){ int a[7] = {1,2,3,4,5,6,7}; int i = 0; int j = 6; int t; while (i < j) { t = a[i]; a[i] = a[j]; a[j] = t; i++; j--; } for (int k=0; k<7; k++) { printf("%d /t",a[k]); } return 0;}//运行结果:7 6 5 4 3 2 1import java.util.ArrayList;import java.util.Collections;public class App1{  public static void main(String[] args)  { //Collection的接口的实现类:Set、List本身并没有提供导致、排序等方法, //这些方法是由Collections提供的 ArrayList arr = new ArrayList(); for (int i=0; i<7; i++) { arr.add(i); System.out.println(arr.get(i)); } System.out.println("---------------"); //接下来进行倒置 Collections.reverse(arr); for (int i=0; i<7; i++) { System.out.println(arr.get(i)); }  }}0123456---------------6543210`

`[code]# include <stdio.h>int main(void){int arr[3][3] = {{1,2,3}, {4,5,6},{7,8,9}};for (int i=0; i<3; i++)  //行数{for (int j=0; j<3; j++){ printf("%d/t",arr[i][j]);;} printf("/n");} return 0;}public class App1{  public static void main(String[] args)  {int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};for (int i=0; i<3; i++)  //行数{for (int j=0; j<3; j++){ System.out.print(arr[i][j]+"/t");} System.out.println("/n");}System.out.println("二维数组的长度是:"+arr.length);//长度是3,而不是12  }}//运行结果:123456789二维数组的长度是:3`