如何在Java中声明和初始化数组?


当前回答

这里有很多答案。我添加了一些创建数组的技巧(从考试的角度来看,知道这一点很好)

声明和定义数组intintArray[]=新int[3];这将创建一个长度为3的数组。由于它持有一个基元类型int,所有值默认设置为0。例如intArray[2];//将返回0在变量名称前使用方括号[]int[]intArray=新int[3];intArray[0]=1;//数组内容现在为{1,0,0}初始化并向阵列提供数据int[]intArray=新int[]{1,2,3};这一次,无需在方框括号中提及尺寸。甚至一个简单的变体是:int[]intArray={1,2,3,4};长度为0的数组int[]intArray=新int[0];int length=intArray.length;//将返回长度0类似于多维数组int intArray[][]=新int[2][3];//这将创建一个长度为2的数组//每个元素包含另一个长度为3的数组。// { {0,0,0},{0,0,0} }int lenght1=intArray.length;//将返回2int length2=intArray[0].length;//将返回3

在变量之前使用方括号:

    int[][] intArray = new int[2][3];

如果你在最后放一个盒子支架,那是绝对好的:

    int[] intArray [] = new int[2][4];
    int[] intArray[][] = new int[2][3][4]

一些示例

    int [] intArray [] = new int[][] {{1,2,3},{4,5,6}};
    int [] intArray1 [] = new int[][] {new int[] {1,2,3}, new int [] {4,5,6}};
    int [] intArray2 [] = new int[][] {new int[] {1,2,3},{4,5,6}}
    // All the 3 arrays assignments are valid
    // Array looks like {{1,2,3},{4,5,6}}

每个内部元素的大小不一定相同。

    int [][] intArray = new int[2][];
    intArray[0] = {1,2,3};
    intArray[1] = {4,5};
    //array looks like {{1,2,3},{4,5}}

    int[][] intArray = new int[][2] ; // This won't compile. Keep this in mind.

如果使用上述语法,则必须确保向前方向必须在方括号中指定值。否则它将无法编译。一些示例:

    int [][][] intArray = new int[1][][];
    int [][][] intArray = new int[1][2][];
    int [][][] intArray = new int[1][2][3];

另一个重要特征是协变

    Number[] numArray = {1,2,3,4};   // java.lang.Number
    numArray[0] = new Float(1.5f);   // java.lang.Float
    numArray[1] = new Integer(1);    // java.lang.Integer
   // You can store a subclass object in an array that is declared
   // to be of the type of its superclass.
   // Here 'Number' is the superclass for both Float and Integer.

   Number num[] = new Float[5]; // This is also valid

重要信息:对于引用的类型,数组中存储的默认值为空。

其他回答

有时我用它来初始化字符串数组:

private static final String[] PROPS = "lastStart,storetime,tstore".split(",");

它以更昂贵的初始化为代价,减少了报价混乱。

制作阵列有两种主要方法:

对于空数组:

int[] array = new int[n]; // "n" being the number of spaces to allocate in the array

对于一个初始化的数组:

int[] array = {1,2,3,4 ...};

您还可以创建多维数组,如下所示:

int[][] array2d = new int[x][y]; // "x" and "y" specify the dimensions
int[][] array2d = { {1,2,3 ...}, {4,5,6 ...} ...};

以基元类型int为例。有几种方法可以声明和int数组:

int[] i = new int[capacity];
int[] i = new int[] {value1, value2, value3, etc};
int[] i = {value1, value2, value3, etc};

在所有这些中,可以使用inti[]而不是int[]i。

对于反射,可以使用(Type[])Array.newInstance(Type.class,capacity);

注意,在方法参数中。。。表示变量参数。本质上,任何数量的参数都可以。用代码更容易解释:

public static void varargs(int fixed1, String fixed2, int... varargs) {...}
...
varargs(0, "", 100); // fixed1 = 0, fixed2 = "", varargs = {100}
varargs(0, "", 100, 200); // fixed1 = 0, fixed2 = "", varargs = {100, 200};

在该方法中,varargs被视为普通的int[]。类型只能在方法参数中使用,因此int.i=newint[]{}不会编译。

请注意,当将int[]传递给方法(或任何其他Type[])时,不能使用第三种方法。在语句int[]i=*{a,b,c,d,etc}*中,编译器假设{…}表示int[]。但这是因为您正在声明一个变量。将数组传递给方法时,声明必须是newType[capartment]或newType[]{…}。

多维数组

多维数组更难处理。本质上,2D阵列是阵列的阵列。int[][]表示int[]的数组。关键是,如果int[][]声明为int[x][y],则最大索引为i[x-1][y-1]。基本上,矩形int[3][5]是:

[0, 0] [1, 0] [2, 0]
[0, 1] [1, 1] [2, 1]
[0, 2] [1, 2] [2, 2]
[0, 3] [1, 3] [2, 3]
[0, 4] [1, 4] [2, 4]

声明数组:int[]arr;

初始化数组:int[]arr=new int[10];10表示数组中允许的元素数

声明多维数组:int[][]arr;

初始化多维数组:int[][]arr=new int[10][17];10行17列和170个元素,因为10乘以17等于170。

初始化数组意味着指定数组的大小。

为Java 8和更高版本声明和初始化。创建一个简单的整数数组:

int [] a1 = IntStream.range(1, 20).toArray();
System.out.println(Arrays.toString(a1));
// Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

为[-50,50]之间的整数和双[0,1E17]创建随机数组:

int [] a2 = new Random().ints(15, -50, 50).toArray();
double [] a3 = new Random().doubles(5, 0, 1e17).toArray();

两个序列的功率:

double [] a4 = LongStream.range(0, 7).mapToDouble(i -> Math.pow(2, i)).toArray();
System.out.println(Arrays.toString(a4));
// Output: [1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0]

对于String[],必须指定构造函数:

String [] a5 = Stream.generate(()->"I will not squeak chalk").limit(5).toArray(String[]::new);
System.out.println(Arrays.toString(a5));

多维数组:

String [][] a6 = List.of(new String[]{"a", "b", "c"} , new String[]{"d", "e", "f", "g"})
    .toArray(new String[0][]);
System.out.println(Arrays.deepToString(a6));
// Output: [[a, b, c], [d, e, f, g]]