Categories
arrays java

How do I declare and initialize an array in Java?

2406

How do I declare and initialize an array in Java?

4

3078

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

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

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 an array first and then initialize it, pass an array as a function argument, or return an array. The explicit type is required.

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

2

  • 40

    What’s the purpose of having both the second and third way to do it?

    Apr 10, 2015 at 3:23

  • 151

    @iamcreasy It looks like the second way doesn’t work with return statements. return {1,2,3} gives an error, while return new int[]{1,2,3} works fine (assuming of course that your function returns an integer array).

    Apr 16, 2015 at 17:44


314

There are two types of array.

One Dimensional Array

Syntax for default values:

int[] num = new int[5];

Or (less preferred)

int num[] = new int[5];

Syntax with values given (variable/field initialization):

int[] num = {1,2,3,4,5};

Or (less preferred)

int num[] = {1, 2, 3, 4, 5};

Note: For convenience int[] num is preferable because it clearly tells that you are talking here about array. Otherwise no difference. Not at all.

Multidimensional array

Declaration

int[][] num = new int[5][2];

Or

int num[][] = new int[5][2];

Or

int[] num[] = new int[5][2];

Initialization

 num[0][0]=1;
 num[0][1]=2;
 num[1][0]=1;
 num[1][1]=2;
 num[2][0]=1;
 num[2][1]=2;
 num[3][0]=1;
 num[3][1]=2;
 num[4][0]=1;
 num[4][1]=2;

Or

 int[][] num={ {1,2}, {1,2}, {1,2}, {1,2}, {1,2} };

Ragged Array (or Non-rectangular Array)

 int[][] num = new int[5][];
 num[0] = new int[1];
 num[1] = new int[5];
 num[2] = new int[2];
 num[3] = new int[3];

So here we are defining columns explicitly.
Another Way:

int[][] num={ {1}, {1,2}, {1,2,3,4,5}, {1,2}, {1,2,3} };

For Accessing:

for (int i=0; i<(num.length); i++ ) {
    for (int j=0;j<num[i].length;j++)
        System.out.println(num[i][j]);
}

Alternatively:

for (int[] a : num) {
  for (int i : a) {
    System.out.println(i);
  }
}

Ragged arrays are multidimensional arrays.
For explanation see multidimensional array detail at the official java tutorials

3

  • Won’t the first one lead to a null/empty array, instead of array with default values?

    Feb 19, 2017 at 0:25


  • I agree on that point, and we can add one more feature, we can change the size dynamically.

    – AdamIJK

    Apr 26, 2017 at 11:26

  • 1

    I might argue with you on the point that a multidimensional array is a different “type” of array. It’s simply a term used to describe an array that happens to contain other arrays. Both the outer arrays and the inner arrays (and those in between, if they exist) are just regular arrays.

    – Tim M.

    Jul 18, 2017 at 15:19

131

Type[] variableName = new Type[capacity];

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



Type variableName[] = new Type[capacity]; 

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

is also valid, but I prefer the brackets after the type, because it’s easier to see that the variable’s type is actually an array.

3

  • 24

    I agree on that point. The type of the variable is not “TYPE”, but actually a TYPE[], so it makes sense to write it that way for me.

    – Chet

    Jul 29, 2009 at 14:31

  • 3

    Google style suggest this too.

    – wener

    Mar 5, 2014 at 12:43

  • 14

    Note that int[] a, b; will not be the same as int a[], b;, a mistake easy to make if you use the latter form.

    Mar 19, 2015 at 1:46