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

2525

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


35
在发布新回答之前,请考虑此问题已经有25个以上的答案。请确保您的答案提供的信息不在现有答案中。 - janniks
1
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html - Babak
请查看以下链接:https://www.tutorialcup.com/java/how-to-return-an-array-in-java.htm。 - Rahul Gupta
https://www.tutorialcup.com/java/arrays-in-java.htm - Rahul Gupta
31个回答

13

在Java 8中,您可以使用类似于以下内容的东西。

String[] strs = IntStream.range(0, 15)  // 15 is the size
    .mapToObj(i -> Integer.toString(i))
    .toArray(String[]::new);

13

以原始数据类型int为例,有几种声明int数组的方法:

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

在这些情况下,你可以使用 int i[] 替代 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[]处理。Type...只能用在方法参数中,所以 int... i = new int[] {} 会编译错误。
请注意,当向方法传递一个 int[](或任何其他的 Type[])时,不能使用第三种方式。在语句 int[] i = *{a, b, c, d, etc}* 中,编译器会假设 {...} 意味着一个 int[],但那是因为你正在声明一个变量。当向方法传递数组时,声明必须是 new Type[capacity] 或者 new Type[] {...}

多维数组

多维数组更难处理。实际上,一个二维数组就是一个数组的数组。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]

12
如果你想使用反射来创建数组,你可以这样做:
int size = 3;
int[] intArray = (int[]) Array.newInstance(int.class, size ); 

你为什么想要以那种方式创建一个数组? - Dorian Gray
你的问题适用于本节提供的所有答案变体。真正的问题可能是为什么语言对于同一事物有多种声明方式?我认为答案可能是所有变体都可以帮助我们应对不同情况,因此语言支持我们按需执行任务。 - Muhammad Suleman

11

声明一个对象引用的数组:

class Animal {}

class Horse extends Animal {
    public static void main(String[] args) {

        /*
         * Array of Animal can hold Animal and Horse (all subtypes of Animal allowed)
         */
        Animal[] a1 = new Animal[10];
        a1[0] = new Animal();
        a1[1] = new Horse();

        /*
         * Array of Animal can hold Animal and Horse and all subtype of Horse
         */
        Animal[] a2 = new Horse[10];
        a2[0] = new Animal();
        a2[1] = new Horse();

        /*
         * Array of Horse can hold only Horse and its subtype (if any) and not
           allowed supertype of Horse nor other subtype of Animal.
         */
        Horse[] h1 = new Horse[10];
        h1[0] = new Animal(); // Not allowed
        h1[1] = new Horse();

        /*
         * This can not be declared.
         */
        Horse[] h2 = new Animal[10]; // Not allowed
    }
}

11

数组是一串项目的有序列表。

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,或使用new Type(...)初始化它们,类似StringInteger的类是特殊情况,将按以下方式处理。

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 维数组在空间方面是昂贵的。因为当你创建一个每个维度上都有 NM 维数组时,数组的总大小比 N^M 还要大,因为每个数组都有一个引用,在 M 维中存在一个 (M-1) 维的引用数组。总大小如下所示:

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

10

声明

一维数组

int[] nums1; // best practice
int []nums2;
int nums3[];

多维数组

int[][] nums1; // best practice
int [][]nums2;
int[] []nums3;
int[] nums4[];
int nums5[][];

声明和初始化

一维数组

包含默认值

int[] nums = new int[3]; // [0, 0, 0]

Object[] objects = new Object[3]; // [null, null, null]

通过数组字面量

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

Object[] objects1 = {new Object(), new Object(), new Object()};
Object[] objects2 = new Object[]{new Object(), new Object(), new Object()};

使用for循环

int[] nums = new int[3];
for (int i = 0; i < nums.length; i++) {
    nums[i] = i; // can contain any YOUR filling strategy
}

Object[] objects = new Object[3];
for (int i = 0; i < objects.length; i++) {
    objects[i] = new Object(); // can contain any YOUR filling strategy
}

使用 forRandom 循环

int[] nums = new int[10];
Random random = new Random();
for (int i = 0; i < nums.length; i++) {
    nums[i] = random.nextInt(10); // random int from 0 to 9
}

使用 Stream(自Java 8以来)

int[] nums1 = IntStream.range(0, 3)
                       .toArray(); // [0, 1, 2]
int[] nums2 = IntStream.rangeClosed(0, 3)
                       .toArray(); // [0, 1, 2, 3]
int[] nums3 = IntStream.of(10, 11, 12, 13)
                       .toArray(); // [10, 11, 12, 13]
int[] nums4 = IntStream.of(12, 11, 13, 10)
                       .sorted()
                       .toArray(); // [10, 11, 12, 13]
int[] nums5 = IntStream.iterate(0, x -> x <= 3, x -> x + 1)
                       .toArray(); // [0, 1, 2, 3]
int[] nums6 = IntStream.iterate(0, x -> x + 1)
                       .takeWhile(x -> x < 3)
                       .toArray(); // [0, 1, 2]

int size = 3;
Object[] objects1 = IntStream.range(0, size)
        .mapToObj(i -> new Object()) // can contain any YOUR filling strategy
        .toArray(Object[]::new);

Object[] objects2 = Stream.generate(() -> new Object()) // can contain any YOUR filling strategy
        .limit(size)
        .toArray(Object[]::new);

使用 Java 8 的 RandomStream

int size = 3;
int randomNumberOrigin = -10;
int randomNumberBound = 10
int[] nums = new Random().ints(size, randomNumberOrigin, randomNumberBound).toArray();

多维数组

带默认值

int[][] nums = new int[3][3]; // [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

Object[][] objects = new Object[3][3]; // [[null, null, null], [null, null, null], [null, null, null]]

使用数组字面量

int[][] nums1 = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
};
int[][] nums2 = new int[][]{
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
};

Object[][] objects1 = {
        {new Object(), new Object(), new Object()},
        {new Object(), new Object(), new Object()},
        {new Object(), new Object(), new Object()}
};
Object[][] objects2 = new Object[][]{
        {new Object(), new Object(), new Object()},
        {new Object(), new Object(), new Object()},
        {new Object(), new Object(), new Object()}
};

使用for循环

int[][] nums = new int[3][3];
for (int i = 0; i < nums.length; i++) {
    for (int j = 0; j < nums[i].length; i++) {
        nums[i][j] = i + j; // can contain any YOUR filling strategy
    }
}

Object[][] objects = new Object[3][3];
for (int i = 0; i < objects.length; i++) {
    for (int j = 0; j < nums[i].length; i++) {
        objects[i][j] = new Object(); // can contain any YOUR filling strategy
    }
}

10
如果你说的“array”是指使用java.util.Arrays,你可以这样做:
List<String> number = Arrays.asList("1", "2", "3");

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

这个很简单和直接创建一个 List。(List 和 Array 之间的区别是什么?)现在如果你真的想要一个 Array,你仍然可以使用:

String[] numberArray = Arrays.asList("1", "2", "3").toArray(new String[0]);

10
列表与数组不同。 - Tirno
有时候人们想要列表,却使用了数组。 - Sylhare
1
有时候他们不理解这两者之间的区别,因此一个好的答案至少应该提到List对象类似于数组类型实例,但并不相同 - GhostCat

8

声明并初始化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]]

-50和/或+50是否实际包含在内?也就是说,内部是否在一端或两端打开? - Peter Mortensen
2
-50 包括在内,而 +50 不包括在内。这个信息来源于 Java API 中的“给定起点(包含)和边界(不包含)”。我使用了 维基百科 上的区间声明。因此,我认为 [-50,50) 更正确。 - Kirill Podlivaev

7

要创建类对象的数组,您可以使用 java.util.ArrayList 来定义一个数组:

public ArrayList<ClassName> arrayName;
arrayName = new ArrayList<ClassName>();

给数组赋值:
arrayName.add(new ClassName(class parameters go here);

从数组中读取:

ClassName variableName = arrayName.get(index);

注意: variableName 是对数组的引用,这意味着操作 variableName 将操作 arrayName for 循环:
//repeats for every value in the array
for (ClassName variableName : arrayName){
}
//Note that using this for loop prevents you from editing arrayName

允许您编辑arrayName的for循环(传统的for循环):

for (int i = 0; i < arrayName.size(); i++){
    //manipulate array here
}

5

这里有很多答案。我添加了一些创建数组的巧妙方法(从考试角度来看,了解这个是很好的)。

  1. Declare and define an array

    int intArray[] = new int[3];
    

    This will create an array of length 3. As it holds a primitive type, int, all values are set to 0 by default. For example,

    intArray[2]; // Will return 0
    
  2. Using box brackets [] before the variable name

    int[] intArray = new int[3];
    intArray[0] = 1;  // Array content is now {1, 0, 0}
    
  3. Initialise and provide data to the array

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

    This time there isn't any need to mention the size in the box bracket. Even a simple variant of this is:

    int[] intArray = {1, 2, 3, 4};
    
  4. An array of length 0

    int[] intArray = new int[0];
    int length = intArray.length; // Will return length 0
    

    Similar for multi-dimensional arrays

    int intArray[][] = new int[2][3];
    // This will create an array of length 2 and
    //each element contains another array of length 3.
    // { {0,0,0},{0,0,0} }
    int lenght1 = intArray.length; // Will return 2
    int length2 = intArray[0].length; // Will return 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

重要提示:对于引用类型,在数组中存储的默认值为null。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接