How do I declare and initialize an array in Java?

2018-12-30 23:35发布

How do I declare and initialize an array in Java?

21条回答
看淡一切
2楼-- · 2018-12-31 00:21

With local variable type inference you only have to specify type once:

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

or

int[] values = { 1, 2, 3 }
查看更多
ら面具成の殇う
3楼-- · 2018-12-31 00:22

Declare and initialize for Java 8 and later. Create a simple integer array:

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]

Create a random array for integers between [-50, 50] and for doubles [0, 1E17]:

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

Power-of-two sequence:

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]

For String[] you must specify a constructor:

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

Multidimensional arrays:

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]]
查看更多
情到深处是孤独
4楼-- · 2018-12-31 00:23

You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).

For primitive types:

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

For classes, for example String, it's the same:

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

The third way of initializing is useful when you declare the array first and then initialize it. The cast is necessary here.

String[] myStringArray;
myStringArray = new String[]{"a","b","c"};
查看更多
听够珍惜
5楼-- · 2018-12-31 00:24

There are various ways in which you can declare an array in Java:

float floatArray[]; // Initialize later
int[] integerArray = new int[10];
String[] array = new String[] {"a", "b"};

You can find more information in the Sun tutorial site and the JavaDoc.

查看更多
爱死公子算了
6楼-- · 2018-12-31 00:25

If you want to create arrays using reflections then you can do like this:

 int size = 3;
 int[] intArray = (int[]) Array.newInstance(int.class, size ); 
查看更多
只若初见
7楼-- · 2018-12-31 00:25

For creating arrays of class Objects you can use the java.util.ArrayList. to define an array:

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

Assign values to the array:

arrayName.add(new ClassName(class parameters go here);

Read from the array:

ClassName variableName = arrayName.get(index);

Note:

variableName is a reference to the array meaning that manipulating variableName will manipulate arrayName

for loops:

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

for loop that allows you to edit arrayName (conventional for loop):

for (int i = 0; i < arrayName.size(); i++){
    //manipulate array here
}
查看更多
登录 后发表回答