Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-14-2008, 09:55 PM
Member
 
Join Date: Jun 2007
Posts: 14
bluekswing is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-15-2008, 06:06 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 112
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
It seems like you just need a for loop.
Code:
for(int i = 0; i < array[0].length; i++){ //use array[0][i] //use array[1][i] }
The problem with this is that you have to ensure that your arrays are the same size.

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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-15-2008, 06:57 PM
Member
 
Join Date: Jun 2007
Posts: 14
bluekswing is on a distinguished road
Thanks for the response! I ended up just doing the following:

Quote:
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 07:13 PM. Reason: fixed code
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array Help bluegreen7hi New To Java 2 03-28-2008 03:25 AM
can anyone help... 2d Array Mark1989 New To Java 2 03-12-2008 09:59 PM
Would appreciate your help with 2d Array.. cloudkicker New To Java 1 02-11-2008 03:34 PM
Bounded Array bugger New To Java 4 01-04-2008 10:41 AM
Help with Array susan New To Java 1 08-07-2007 05:32 AM


All times are GMT +3. The time now is 04:05 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org