目录
方式一
int[ ] arr = new int[3]
方式二:在定义的时候同时赋值
int[ ] arr = new int[ ]{1, 2, 3}
// 需要注意,在赋值的时候,在后面一个中括号中不要写数组内的数据个数,如果写了会报错
方式三
int[ ] arr = {1, 2, 3}
// 直接赋值,这种方式用的最多,最方便;
JVM对自身的内存分为5个区域
=============
=============
int[ ] arr = new int[3]
arr[0] = 1;
package day002;
public class arrayDemo {
public static void main(String[] args){
test001(); //遍历数组
test002(); //获取数组最大值
}
public static void test001(){
int[] arr = new int[]{1, 2, 3};
for(int i = 0; i < 3 ; i++){
System.out.println(arr[i]);
}
}
public static void test002(){
int[] arr2 = new int[]{1, 5, 3};
int max = arr2[0];
for(int j = 1; j<arr2.length; j++){
if(max < arr2[j]){
max = arr2[j];
}
}
System.out.println(max);
}
}
// 方式一
int [ ] [ ] arr = new int[3] [4];
// 方式二
int [ ] [ ] = { {1, 2}, {1, 6}};
说明:二维或者多维数组实际上是一个数组内存储的每个对象,是数组;
原文:https://www.cnblogs.com/chenadong/p/13064955.html