• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding 2D ArrayLists

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My background is over 50 years of programming 3rd generation languages so much of what I code is colored by that experience. Now I am coding java & many things I don't understand. Can someone help me to understand the following problem.

I need a 2-d array, array, of !0 rows by 4 columns. The members of the array are booleans & I need to be able to set array(row, column) to TRUE or FALSE


Case 1:
In Java define a 2-D array:

Next define an array of booleans and initialize 4 elements to false:

Finally set up 10 rows of 4 columns of booleans:

To set an element at (2,3) to true

But what happens is every row's column 3 is set to true;

In order to get the array set to work properly the construction of the rowOFBooleans must be put inside the 'for loop'.

Case 2:

My surmise is that in the first case Java doesn't store the values of rowOfBooleans in the twoDArray but rather stores a pointer to the ArrayList rowOfBooleans. Thus when ever an element is changed each row is pointing to the same rowOfBooleans.

In the second case each row is pointing to a different rowOfBooleans.

Can anyone help me understand what is happening?

Thank you.


 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First a small question:

Do you need a ArrayList<ArrayList<Boolean>>(10) or could a boolean[10][4] do the trick as well?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Mittleman wrote:My surmise is that in the first case Java doesn't store the values of rowOfBooleans in the twoDArray but rather stores a pointer to the ArrayList rowOfBooleans. Thus when ever an element is changed each row is pointing to the same rowOfBooleans.


Correct

Richard Mittleman wrote:In the second case each row is pointing to a different rowOfBooleans.


Correct again

Richard Mittleman wrote:Can anyone help me understand what is happening?


You appear to understand it pretty well.


Note however that an array is not the same thing as an ArrayList. So if you do need a 2-D array, you may want to follow Wouter's hint.
 
Richard Mittleman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I do need ArrayLists as the number of rows & columns can increase as the program is running.
 
Richard Mittleman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may seem that I understand it from the outside but from the inside of my head the question keeps coming up "Why is it done that way?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or change your iteration to create a new list for each row:


Note that the pointers to the false value from the first list will be used (it is a shallow copy) but we are protected since Booleans are immutable and you will need to supply a new instance rather than changing the value of the instance.
I also prefer Boolean.valueOf(false) but that is outside the scope of the question
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic