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


当前回答

package com.examplehub.basics;

import java.util.Arrays;

public class Array {

    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};

        /*
         * numbers[0] = 1
         * numbers[1] = 2
         * numbers[2] = 3
         * numbers[3] = 4
         * numbers[4] = 5
         */
        System.out.println("numbers[0] = " + numbers[0]);
        System.out.println("numbers[1] = " + numbers[1]);
        System.out.println("numbers[2] = " + numbers[2]);
        System.out.println("numbers[3] = " + numbers[3]);
        System.out.println("numbers[4] = " + numbers[4]);

        /*
         * Array index is out of bounds
         */
        //System.out.println(numbers[-1]);
        //System.out.println(numbers[5]);


        /*
         * numbers[0] = 1
         * numbers[1] = 2
         * numbers[2] = 3
         * numbers[3] = 4
         * numbers[4] = 5
         */
        for (int i = 0; i < 5; i++) {
            System.out.println("numbers[" + i + "] = " + numbers[i]);
        }

        /*
         * Length of numbers = 5
         */
        System.out.println("length of numbers = " + numbers.length);

        /*
         * numbers[0] = 1
         * numbers[1] = 2
         * numbers[2] = 3
         * numbers[3] = 4
         * numbers[4] = 5
         */
        for (int i = 0; i < numbers.length; i++) {
            System.out.println("numbers[" + i + "] = " + numbers[i]);
        }

        /*
         * numbers[4] = 5
         * numbers[3] = 4
         * numbers[2] = 3
         * numbers[1] = 2
         * numbers[0] = 1
         */
        for (int i = numbers.length - 1; i >= 0; i--) {
            System.out.println("numbers[" + i + "] = " + numbers[i]);
        }

        /*
         * 12345
         */
        for (int number : numbers) {
            System.out.print(number);
        }
        System.out.println();

        /*
         * [1, 2, 3, 4, 5]
         */
        System.out.println(Arrays.toString(numbers));



        String[] company = {"Google", "Facebook", "Amazon", "Microsoft"};

        /*
         * company[0] = Google
         * company[1] = Facebook
         * company[2] = Amazon
         * company[3] = Microsoft
         */
        for (int i = 0; i < company.length; i++) {
            System.out.println("company[" + i + "] = " + company[i]);
        }

        /*
         * Google
         * Facebook
         * Amazon
         * Microsoft
         */
        for (String c : company) {
            System.out.println(c);
        }

        /*
         * [Google, Facebook, Amazon, Microsoft]
         */
        System.out.println(Arrays.toString(company));

        int[][] twoDimensionalNumbers = {
                {1, 2, 3},
                {4, 5, 6, 7},
                {8, 9},
                {10, 11, 12, 13, 14, 15}
        };

        /*
         * total rows  = 4
         */
        System.out.println("total rows  = " + twoDimensionalNumbers.length);

        /*
         * row 0 length = 3
         * row 1 length = 4
         * row 2 length = 2
         * row 3 length = 6
         */
        for (int i = 0; i < twoDimensionalNumbers.length; i++) {
            System.out.println("row " + i + " length = " + twoDimensionalNumbers[i].length);
        }

        /*
         * row 0 = 1 2 3
         * row 1 = 4 5 6 7
         * row 2 = 8 9
         * row 3 = 10 11 12 13 14 15
         */
        for (int i = 0; i < twoDimensionalNumbers.length; i++) {
            System.out.print("row " + i + " = ");
            for (int j = 0; j < twoDimensionalNumbers[i].length; j++) {
                System.out.print(twoDimensionalNumbers[i][j] + " ");
            }
            System.out.println();
        }

        /*
         * row 0 = [1, 2, 3]
         * row 1 = [4, 5, 6, 7]
         * row 2 = [8, 9]
         * row 3 = [10, 11, 12, 13, 14, 15]
         */
        for (int i = 0; i < twoDimensionalNumbers.length; i++) {
            System.out.println("row " + i + " = " + Arrays.toString(twoDimensionalNumbers[i]));
        }

        /*
         * 1 2 3
         * 4 5 6 7
         * 8 9
         * 10 11 12 13 14 15
         */
        for (int[] ints : twoDimensionalNumbers) {
            for (int num : ints) {
                System.out.print(num + " ");
            }
            System.out.println();
        }

        /*
         * [1, 2, 3]
         * [4, 5, 6, 7]
         * [8, 9]
         * [10, 11, 12, 13, 14, 15]
         */
        for (int[] ints : twoDimensionalNumbers) {
            System.out.println(Arrays.toString(ints));
        }


        int length = 5;
        int[] array = new int[length];
        for (int i = 0; i < 5; i++) {
            array[i] = i + 1;
        }

        /*
         * [1, 2, 3, 4, 5]
         */
        System.out.println(Arrays.toString(array));

    }
}

来源于examplehub/java

其他回答

用于使用0以外的默认值进行初始化:

var arr = new int[1000];
Arrays.fill(arr, 1);

下面显示了数组的声明,但未初始化数组:

 int[] myIntArray = new int[3];

下面显示了数组的声明和初始化:

int[] myIntArray = {1,2,3};

现在,下面还显示了数组的声明和初始化:

int[] myIntArray = new int[]{1,2,3};

但第三个显示了匿名数组对象创建的属性,该属性由引用变量“myIntArray”指向,因此如果我们只写“newint[]{1,2,3};”,那么这就是如何创建匿名数组对象。

如果我们只写:

int[] myIntArray;

这不是数组的声明,但以下语句使上述声明完成:

myIntArray=new int[3];

另一种声明和初始化ArrayList的方法:

private List<String> list = new ArrayList<String>(){{
    add("e1");
    add("e2");
}};

如果“array”是指使用java.util.Arrays,那么可以使用:

List<String> number = Arrays.asList("1", "2", "3");

// Out: ["1", "2", "3"]

这个非常简单明了。

您可以使用数组声明或数组文字(但仅当您立即声明并影响变量时,数组文字不能用于重新分配数组)。

对于基本类型:

int[] myIntArray = new int[3]; // each element of the array is initialised to 0
int[] myIntArray = {1, 2, 3};
int[] myIntArray = new int[]{1, 2, 3};

// Since Java 8. Doc of IntStream: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html

int [] myIntArray = IntStream.range(0, 100).toArray(); // From 0 to 99
int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 100
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).toArray(); // The order is preserved.
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).sorted().toArray(); // Sort 

对于类,例如String,它是相同的:

String[] myStringArray = new String[3]; // each element is initialised to null
String[] myStringArray = {"a", "b", "c"};
String[] myStringArray = new String[]{"a", "b", "c"};

当您首先声明一个数组,然后初始化它,将一个数组作为函数参数传递,或者返回一个数组时,第三种初始化方法非常有用。需要显式类型。

String[] myStringArray;
myStringArray = new String[]{"a", "b", "c"};