How do I declare and initialize an array in Java?

ID : 333

viewed : 285

Tags : javaarraysjava

Top 5 Answer for How do I declare and initialize an array in Java?

vote vote


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


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


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


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


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


 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; 


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


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

vote vote


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.

vote vote


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.

vote vote


I find it is helpful if you understand each part:

Type[] name = new Type[5]; 

Type[] is the type of the variable called name ("name" is called the identifier). The literal "Type" is the base type, and the brackets mean this is the array type of that base. Array types are in turn types of their own, which allows you to make multidimensional arrays like Type[][] (the array type of Type[]). The keyword new says to allocate memory for the new array. The number between the bracket says how large the new array will be and how much memory to allocate. For instance, if Java knows that the base type Type takes 32 bytes, and you want an array of size 5, it needs to internally allocate 32 * 5 = 160 bytes.

You can also create arrays with the values already there, such as

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

which not only creates the empty space but fills it with those values. Java can tell that the primitives are integers and that there are 5 of them, so the size of the array can be determined implicitly.

Top 3 video Explaining How do I declare and initialize an array in Java?