How do I declare and initialize an array in Java?
If you want to initialize an array, try using Array Initializer: int[] data = {10,20,30,40,50,60,71,80,90,91}; // or int[] data; data = new int[] {10,20,30,40,50,60,71,80,90,91};
The initializer for an array is a comma-separated list of constant expressions enclosed in braces ( { } ). The initializer is preceded by an equal sign ( = ). You do not need to initialize all elements in an array.
In Java, here is how we can declare an array. dataType[] arrayName; dataType - it can be primitive data types like int , char , double , byte , etc. or Java objects. arrayName - it is an identifier.
type var-name[]; OR type[] var-name; An array declaration has two components: the type and the name. type declares the element type of the array. The element type determines the data type of each element that comprises the array.
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"};
There are two types of 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.
int[][] num = new int[5][2];
Or
int num[][] = new int[5][2];
Or
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;
Or
int[][] num={ {1,2}, {1,2}, {1,2}, {1,2}, {1,2} };
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 (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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With