+ 1
java easy array explain
explain it line by line please int[][] array = new int[5][6]; int[] x = {1, 2}; array[0] = x; System.out.println("array[0][1] is " + array[0][1]);
4 Respostas
+ 2
You're dealing with a two-dimensional array. Lesson 24.1 of the java course explains how they work.
+ 2
- create empty array[5] of empty arrays[6] of int
5,6 are length, int is integer, number,
name of array is 'array'
- create array named x with two elements 1,2
- add x array to 'array' at position [0]
it is possible because 'array' elements are another array
- print values on screen:
. "array[0][1] is " is information for user, its String of characters
. + join also to message:
. array in position 0 is x array and its element at position 1 is 2
+ 1
https://www.geeksforgeeks.org/introduction-to-arrays/amp/
https://www.geeksforgeeks.org/array-data-structure/
Hope this link helps you to know it in detail. Because sometimes it is difficult to write and explain
0
Line 1) Creates an array of two dimensions (an array of arrays) where the first array has 5 elements, and each of those elements is an array of 6 elements.
Every value is initialized to 0 by default in the array.
So for instance, the first and second elements of the outer array is {0,0,0,0,0,0}, {0,0,0,0,0,0}
Line 2) Creates an array x that contains two elements of values 1 and 2.
Its important to note that the indexes for these values are 0 and 1, so
x[0] = 1 and x[1] = 2
Line 3) Assigns the first element (and first array) of array[] as a reference to x[].
Whereas in line 1, array[0] = {0,0,0,0,0,0} ; now array[0] = {1,2}. The rest of the elements fir array[] remain the same, only the first element was changed to point at a different set of values.
Line 4) Prints the string inside the quotes , and appends (using the + to append) the value at array[0][1].
array[] is still a two dimensional array, so the first bracket [0] points to the first element {1,2} and [1] points to 2.