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


当前回答

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

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

其他回答

数组是项目的顺序列表

int item = value;

int [] one_dimensional_array = { value, value, value, .., value };

int [][] two_dimensional_array =
{
  { value, value, value, .. value },
  { value, value, value, .. value },
    ..     ..     ..        ..
  { value, value, value, .. value }
};

如果它是一个物体,那么它就是同一个概念

Object item = new Object();

Object [] one_dimensional_array = { new Object(), new Object(), .. new Object() };

Object [][] two_dimensional_array =
{
  { new Object(), new Object(), .. new Object() },
  { new Object(), new Object(), .. new Object() },
    ..            ..               ..
  { new Object(), new Object(), .. new Object() }
};

对于对象,您需要将其赋值为null,以使用新的Type(..)初始化它们,像String和Integer这样的类是特殊情况,将按如下方式处理

String [] a = { "hello", "world" };
// is equivalent to
String [] a = { new String({'h','e','l','l','o'}), new String({'w','o','r','l','d'}) };

Integer [] b = { 1234, 5678 };
// is equivalent to
Integer [] b = { new Integer(1234), new Integer(5678) };

通常,您可以创建M维数组

int [][]..[] array =
//  ^ M times [] brackets

    {{..{
//  ^ M times { bracket

//            this is array[0][0]..[0]
//                         ^ M times [0]

    }}..}
//  ^ M times } bracket
;

值得注意的是,从空间角度来看,创建M维阵列是昂贵的。因为当您在所有维度上创建一个N的M维数组时,数组的总大小大于N^M,因为每个数组都有一个引用,并且在M维有一个(M-1)维引用数组。总尺寸如下

Space = N^M + N^(M-1) + N^(M-2) + .. + N^0
//      ^                              ^ array reference
//      ^ actual data

在Java 9中

使用不同的IntStream.iterate和IntStream.takeWhile方法:

int[] a = IntStream.iterate(10, x -> x <= 100, x -> x + 10).toArray();

Out: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]


int[] b = IntStream.iterate(0, x -> x + 1).takeWhile(x -> x < 10).toArray();

Out: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

在Java 10中

使用局部变量类型推断:

var letters = new String[]{"A", "B", "C"};

电影类的另一个完整示例:

public class A {

    public static void main(String[] args) {

        class Movie {

            String movieName;
            String genre;
            String movieType;
            String year;
            String ageRating;
            String rating;

            public Movie(String [] str)
            {
                this.movieName = str[0];
                this.genre = str[1];
                this.movieType = str[2];
                this.year = str[3];
                this.ageRating = str[4];
                this.rating = str[5];
            }
        }

        String [] movieDetailArr = {"Inception", "Thriller", "MovieType", "2010", "13+", "10/10"};

        Movie mv = new Movie(movieDetailArr);

        System.out.println("Movie Name: "+ mv.movieName);
        System.out.println("Movie genre: "+ mv.genre);
        System.out.println("Movie type: "+ mv.movieType);
        System.out.println("Movie year: "+ mv.year);
        System.out.println("Movie age : "+ mv.ageRating);
        System.out.println("Movie  rating: "+ mv.rating);
    }
}
Type[] variableName = new Type[capacity];

Type[] variableName = {comma-delimited values};



Type variableName[] = new Type[capacity]; 

Type variableName[] = {comma-delimited values};

也是有效的,但我更喜欢在类型后面加括号,因为更容易看出变量的类型实际上是一个数组。

如果要使用反射创建阵列,可以执行以下操作:

 int size = 3;
 int[] intArray = (int[]) Array.newInstance(int.class, size );