Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Two dimensional arrays  RSS feed

 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, again OCA book. It has an example which I've tried to reproduce, but it doesn't work. Before I share the code, let me say that I've dealt with multidimensional arrays before, but I've never come across this syntax, which is odd and confusing. ANyay, here's the code, which doesn't really work:

This generates this output:

To be honest, I'm not sure what it was supposed to generate...I was just trying to understand the code
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah sorry I meant this
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What values do arguments[2] onwards have?

Answer that and you'll answer your question.
 
Campbell Ritchie
Marshal
Posts: 55748
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There ain't no such as a multidimensional array in Java®. That is why you can do what you are seeing in the book. You can see multidimensional arrays in C; if you have an array like this {{1, 2, 3}, {4, 5, 6}} it can be implemented like this:-Java® doesn't do that. The way that would be implemented is more like this, which is an array of arrays:-That is much more versatile because it permits jagged arrays, and it is probably easier to implement because in Java® arrays are full‑blown object. Many of the newer C implementations also implement 2D arrays as arrays of arrays.

Now look at your inner loop (lines 6, 7, and 8) in isolation. That is a common or garden for‑each loop which iterates an int[] and I am sure you understand that. Now consider the outer for‑each loop in lines 5 and 9. Consider you have an array which contains arrays, so you look at the first array which contains five 0s. Then you look at the second array which contains four 0s. Now you expect to print nine 0s. So far so good. As you doubtless know, a newly‑created array of reference types looks like this:-Now, you declared an array of four arrays, but only initialised the first two to point to real arrays. Remember that an int[] is a reference type, so an int[4][] starts off life containing four nulls. When you try to extract values from the thrid array in the inner loop, you are handling a null, so you suffer a null pointer exception. Because the Exception is displayed via System.err its output can appear before the usual output from System.out. If you repeat the run you will probably find nine 0s and the error message in a different location.
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To see what Campbell is talking about, put the following before your for loops:


(don't forget to import java.util.Arrays)
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how It would look like
After
IndexPoints to
0null
1null
2null
3null

After belwo code:
IndexPoints to
0new array of int with size 5-->new array index 0new array index 1new array index 2new array index 3new array index 4
1new array of int with size 3-->new array index 0new array index 1new array index 2
2null
3null


Now in loop this is what happens:

  • Remember, arguments.length = 4  means i loop try to execute 4 times.
  • When i = 0 so arguments[i].length = 5 means j loop executes 5 times.
  • When i = 1 so arguments[i].length = 3 means j loop executes 3 times.
  • default value of int is 0 so value at [0][0], [0][1], [0][2], [0][3], [0][4] = 0 and [1][0], [1][1], [1][2] = 0
  • But [2][0] is null.

  • iji < arguments.length?j < arguments[i].length ?Value at [i][j]
    0---0<4? = true------
    00---0<5? = true[0][0] = 0
    01---1<5? = true[0][1] = 0
    02---2<5? = true[0][2] = 0
    03---3<5? = true[0][3] = 0
    04---4<5? = true[0][4] = 0
    05---5<5? = false---
    Now i++ and i become 1
    1--1<4? = true----
    10---0<3? = true[1][0] = 0
    11---1<3? = true[1][1] = 0
    12---2<3? = true[1][2] = 0
    13---3<3? = false---
    Now i++ and i becomes 2
    2--2<4? = true----
    20---0 < [null].length causes NullPointerException---

    Final output: 8 zeros plus NullPointerException.
     
    Campbell Ritchie
    Marshal
    Posts: 55748
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:. . . Final output: 8 zeros plus NullPointerException.
    I thought nine 0s; I was mistaken. Eight 0s is correct, if you read the code properly.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Ganesh Patekar wrote:. . . Final output: 8 zeros plus NullPointerException.
    I thought nine 0s; I was mistaken. Eight 0s is correct, if you read the code properly.
    Yes I executed that code on paper then IDE, I meant same, 8 zeros and then NullPointerException, I think use of word plus created confusion
     
    Jason Attin
    Ranch Hand
    Posts: 234
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks a lot for clarifying it guys, it's crystal clear now!
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome Jason
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!