Новый курс! Каждый программист должен знать генеративный ИИ!
+ 3

# Array

int[][] array = {{1, 2}, {3, 4}, {5, 6}}; for (int i = array.length - 1; i >= 0; i——) { for (int j = array[i].length - 1; j >= 0; j——) System.out.print(array[i][j] + " "); System.out.println(); } I want explain for this code line by line with why we write everything please thank you

25th Oct 2021, 12:10 PM
STOP
8 ответов
+ 5
Line 1: You're defining an array of two dimensions with some values. It can be imagined as follows 1 2 3 4 5 6 Now this is an array of arrays. Each element of "array" is an array. Line 2: You're using a for loop to access every array in the outer array. The row index for your 2D array is "i". array.length gives you the number of elements in your array. Here it gives the number of sub arrays Line 3: You're using another for loop to access the inner arrays. The variable "j" is used to keep track of the column number. array[i].length gives you the length of the i'th inner array. It's like follows. array[i] is the inner array at index "i" and so array[i].length is the length of the inner array at index "i". Line 4: You're printing the element at array[i][j] followed by a space Line 5: After printing each row, you're printing a newline. So finally, you get the output as follows(assuming your code is right) 6 5 4 3 2 1 Feel free to ask doubts(if) in my answer. All the best =)
25th Oct 2021, 12:30 PM
Rishi
+ 3
Line 1: int[][] array = {{1, 2}, {3, 4}, {5, 6}}; * You're declaring a variable called "array" which is an 2-Dimensional Integer Array (int[][] -> each [] is a dimension) This means that this array may contain other arrays. More visual example: array: { Index 0: {1, 2}, Index 1: {3, 4}, Index 2: {5, 6} If you read array[2] you get another array: {5, 6}, that's why its called 2-D array If you want to access to a value from the gotten array write array[2][0] and it will return 5, if you write array[2][1] it returns 6. And so on with the other indexes. -------------------------------- Line2: for (int i = array.length - 1; i >= 0; i——) * You are starting a loop, from "int i = array.length - 1" to 0. i-- means decrease "i" value by 1 after each loop. In this case "array.length - 1" returns 2, because you have 3 elements inside the array -> *REMEMBER*: array: { Index 0: {1, 2}, Index 1: {3, 4}, Index 2: {5, 6}} these are 3 and you are substracting 1. So debugging its converted to -> for (int i = 2; i >= 0; i——) -------------------------------- Line 3: for (int j = array[i].length - 1; j >= 0; j——) * You are starting a loop inside the loop before. You must change the variable name, by standard convention it should be the next alphabet letter (j, k..) You are doing the same as before except now you are accessing to array[i]. array[i] returns the array inside the index "i" -> if "i" is 2 then array[i] returns {5, 6} and the length of the returned array is 2. so 2 - 1 = 1. So debugging its converted to -> for(int j = 1; j >= 0; j--) -------------------------------- Line 4: System.out.print(array[i][j] + " "); System.out.println(); * You are simply printing the value contained in array[i][j] followed by space ------------------- As you are looping in your arrays in reversed order the output would be: 6 5 4 3 2 1
25th Oct 2021, 12:36 PM
+ 3
Thanks all
25th Oct 2021, 12:37 PM
STOP
+ 3
STOP you're accessing elements in reverse order. So, your array has 3 elements with index 0, 1 and 2. So to access the last element first, you give array.length-1 As array.length is 3, you get value 2 for i during first iteration. In subsequent iterations, you decrement i by 1 and get the preceding elements in successive iterations
25th Oct 2021, 1:15 PM
Rishi
+ 1
That’s because array starts from 0 If array.length = 3 then you can access 3 elements but starting from 0 so 0, 1 and 2
25th Oct 2021, 1:10 PM
0
In line 2 , why we write array.length-1 .. i mean what is -1??
25th Oct 2021, 1:08 PM
STOP
25th Oct 2021, 1:08 PM
STOP
0
-1 is used to access the last element in tne array. For example: The index for an array starts at 0, so for an array with 10 elements, the first element will start at index 0 and the last element will be index 9. 0 - 9 is 10 elements. However if we want to start the loop at the last element, we need to specify the index of that element. For an array of 10 elements, array.length will equal 10. But index 10 does not exist in the array since it ranges from 0 to 9. So we subtract 1 from array.length to get the index if the last element. This will allow us to access the array staring from the last element.
27th Oct 2021, 7:13 AM
Emiliano Rodriguez
Актуальное сегодня
Game Creator