开始我的第一次博客之旅游:
数组的声明:
int[] a=null; a=new int[3];
声明一个int型的数组a,大小为3
数组的赋值:
方法一:a[0]=7;a[1]=-5;a[2]=10;
方法二:声明的时候就赋值 int[] a=new int[]{7,-5,10};
方法三:int[] a={7,-5,10};
数组的长度:
上面声明的数组长度为3;也可以这样获取:a.length
数组的遍历:
方法一:
<pre name="code" class="html">for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
方法二:
<pre name="code" class="html">for(int number:a){
System.out.println(number);
}
二维数组:
个人觉得二维数组就是一维数组中包含着多个数组;如 int[][] a=new int[][]{{1,5},{7,4},{-5,12,6}};数组a的长度为3,里面包含着3个小数组。遍历的时候先遍历整体,再
遍历每一个。如:for(int i=0;i<a.length;i++){ int[] b=a[i]; for(int j=0;j<b.length;j++){System.out.println(b[j]);}}将a[0]赋值给数组b,再遍历,将a[1]赋值给数
组b,再遍历。。。
或用增强的for循环
for(int[] num1:a){
for(int num2:num1){
<span style="white-space:pre"> </span>System.out.print(num2+" ");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
第一次写博客就写这么多。。。表示bu会用csdn<img alt="委屈" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/wronged.gif" />