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


当前回答

在Java8中,您可以使用类似的功能。

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

其他回答

声明对象引用数组:

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
    }
}

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

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

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

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

 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];

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

对于基本类型:

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"};

要创建类对象的数组,可以使用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

对于循环:

//repeats for every value in the array
for (ClassName variableName : arrayName){
}
//Note that using this for loop prevents you from editing arrayName

for循环,允许您编辑arrayName(常规for循环):

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