一.同一种类型数据的集合。其实,数组就是一个容器。
格式1:
元素类型 [] 数组名 = new 元素类型[元素个数或数组长度];
eg:
int[] arr = new int[5];
格式2:
元素类型[] 数组名 = new 元素类型[]{元素,元素,...};
二.注:用到数组时就要立马考虑到遍历数组。
1.遍历并打印数组元素
class ArrayBianLi {
//遍历数组
public static void main(String[] args) {
int arr[] ={2,34,24,1,4,5};
Demo.Print();//调用Demo这个类里Print()的方法。
//格式:类.函数名();
for (int x=0;x<arr.length ;x++ )
{
System.out.println(arr[x]);
}
}
}
class Demo {
public static void Print() {
System.out.println("哇哦");
}
} |
|