Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Enhanced for loop through a 2D array
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chunnn_li




PostPosted: Thu May 26, 2011 9:30 pm   Post subject: Enhanced for loop through a 2D array

I'm trying to simply set all the values of a 2D array with the enhanced for loop. Here is my code so far:

code:

public static void main (String[] args) {
                int[][] map = new int [3][3];
                for (int[] i : map) {
                        for (int j : i) {
                                j = 2;
                        }
                }
}


Although I could do this with a regular for loop, I'm rather curious on how to make this work. My guess on why my code doesn't work is that the enhanced for loop in java creates an iterator object to loop through the data structure. Since int is a primitive type, it creates a duplicate integer and when my code says j = 2, the duplicate variable j gets set to 2 which does not affect my 2D array at all.

Can anyone help me in getting this to work? Thanks in advance.

EDIT: changing the inner for loop to: for (Integer j : i) doesn't work either
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu May 26, 2011 9:59 pm   Post subject: RE:Enhanced for loop through a 2D array

I haven't used Java in a while, but couldn't you do the following:

code:
public static void main (String[] args) {
                int[][] map = new int [3][3];
                for (int[] i : map) {
                        for (int j[] : i) {
                                j = 2;
                        }
                }
}


Since j really is just a counter of an array.
chrisbrown




PostPosted: Thu May 26, 2011 10:34 pm   Post subject: Re: RE:Enhanced for loop through a 2D array

chunnn_li: You're correct about the duplication; primitives don't have references. Interestingly enough, using Integer objects produces a similarly nasty problem by initializing the elements to null.
I have a feeling you might have to fall back to a classic for loop, at least on the inner one. The alternative is to use a List, but that might be beyond your scope for now and/or incompatible with your compiler if you're using Ready to Program.

@Insectoid: I might be misinterpreting, but I think you're confusing Java- and C-style arrays. Java abstracts all memory management away from the programmer, so arrays aren't necessarily contiguous.
Insectoid




PostPosted: Fri May 27, 2011 6:06 am   Post subject: RE:Enhanced for loop through a 2D array

Ah, my bad. I thought Java had primitive arrays and provided other data types for higher-level data storage.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: