Results 1 to 3 of 3
Thread: 2D array
- 01-14-2008, 08:55 PM #1
Member
- Join Date
- Jun 2007
- Posts
- 14
- Rep Power
- 0
2D array
I have a two dimensional array where each row of the array holds string values that represent attributes of an object. I'm trying to figure out how to iterate over each row setting each element in a row equal to a corresponding object's instance variables.
So I want to Instaniate an object, set that objects instance variables equal to the corresponding values in a row of my 2d array - (then either do something with the object or store it in a data structure for later use) and repeat. Either way, I"m getting stumped at how to iterate appropriately over each row and set an object's instance variables accordingly.
Thanks for the help as always!
- 01-15-2008, 05:06 PM #2
It seems like you just need a for loop.
The problem with this is that you have to ensure that your arrays are the same size.Java Code:for(int i = 0; i < array[0].length; i++){ //use array[0][i] //use array[1][i] }
I would look into using a HashMap. You can get an enumeration object from them and then use the value and key parameters to get the data that you need.
- 01-15-2008, 05:57 PM #3
Member
- Join Date
- Jun 2007
- Posts
- 14
- Rep Power
- 0
Thanks for the response! I ended up just doing the following:
row = 5;
col = 10;
rowCount=0;
while (row > 0) {
String[][] array = new String[row][col];
String[] array1 = new String[col];
for (int i = rowCount; i<=rowCount; i++) {
for (int j = 0; j<array[i].length; j++) {
array1[j] = array[i][j];
} //end of inner for loop
} //end of outer for loop
//instantiate object - pass array1 into the constuctor
//build object
//do stuff with object
rowCount++;
row--;
} //end of while
Last edited by bluekswing; 01-15-2008 at 06:13 PM. Reason: fixed code
Similar Threads
-
Array Help
By bluegreen7hi in forum New To JavaReplies: 2Last Post: 03-28-2008, 02:25 AM -
can anyone help... 2d Array
By Mark1989 in forum New To JavaReplies: 2Last Post: 03-12-2008, 08:59 PM -
Would appreciate your help with 2d Array..
By cloudkicker in forum New To JavaReplies: 1Last Post: 02-11-2008, 02:34 PM -
Bounded Array
By bugger in forum New To JavaReplies: 4Last Post: 01-04-2008, 09:41 AM -
Help with Array
By susan in forum New To JavaReplies: 1Last Post: 08-07-2007, 04:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks