Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-04-2008, 12:23 AM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default [SOLVED] Creating an Array of Arrays?
The only way I can figure out how to do this problem is to have an array of Servers and each Server has an array of Children. My co-worker says he doesn't think it's possible to have an array of arrays in Java and recommended linked lists but I have never used those before so I'd like to know if it's possible to do it my way first?

In my setup each Server has 3 children. The children are identical in every aspect except 1 attribute(directory) so my accesser methods always return the default attributes. I have setup the code to copy the children of each server into their own Target array. I'm doing this rough idea before I head out but I want to know if its even possible before I devote too much time.

Code:
System.out.println(servers.length);
Target[] targets = null;
//TODO display directory path
for(int i = 0; i < servers.length; i++){
	  targets = (Target[]) servers[i].getChildren().clone();   
}
for(int i = 0; i < targets.length; i++){
         System.out.println(targets[i].getDirectory());
}
All I get is
3
null
null
null
each time I click on a node in the TreeViewer. Advice?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-04-2008, 12:43 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
possible to have an array of arrays in Java
Yes it definitely is. The problem is: what types are the elements in the arrays. If they are all the same, no problem. Muliti dimensional arrays in java are arrays of arrays.
int[][] anArray = new int[10][]; // define a 2 dim array
anArray[0] = new int[5]; // define an array as the first element

If the elements are different types, then using the type Object will allow you to save anything, but this is NOT a recommended way to code.
Object[][] someObjects = new Object[10][]; // 1st dim is 10, other dims sized as created
someObjects[0] = new String[5]; // first array is an array of Strings

someObjects[1] = new Integer[20]; // 2nd is Integers

someObjects[0][2] = "third string"; // put something in array
someObjects[1][1] = new Integer(33); // ditto

It be better to have a class for the server that defines a container(could be an array) that holds its children. The servers would be stored in their own container(could be an array).
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-04-2008, 04:50 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
Well the Server class has an ArrayList<Target> that stores all its children(Target), and ServerGroup has ArrayList<Server> that stores all the children(Server). I guess I just need to work with arraylist unless you think it would be more effective to convert the lists toArray() and have an array of servers and an array of targets. I just foresee myself having to use pointers which would mean having to go back and use a list instead of an array. Think I'm better off just sucking it up and using linked lists?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-04-2008, 06:28 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
An ArrayList is much more flexible than an array. And with Generics, the compiler can help you to keep types straight and do the casting for you.

Where do pointers and linked lists come in?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-04-2008, 07:00 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
I was under the assumption that an array list was similar enough to a linked list that it would use pointers. LinkedLists were what my co-worker suggested I use instead of arrays.


I ended up storing the directories of each target into an ArrayList so when server1 was called it would also call the list to display the directories of the children. Now I just need to find a way to implement that with the event listener since selections are just based on String types >_<
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-04-2008, 07:01 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
fail I can't raise your rep without raising someone elses first lol.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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

BB 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
[SOLVED] Help with arrays, printing highest and lowest value of the array. Sophiie New To Java 21 11-05-2008 03:31 PM
Creating an Array of Objects int80 New To Java 2 07-12-2008 04:35 PM
Creating an array of nonprimitive objects Java Tip java.lang 0 04-14-2008 09:46 PM
Creating Array of LinkedList sasikumardr New To Java 1 12-11-2007 11:25 AM
creating array at runtime javaplus New To Java 4 11-08-2007 11:06 PM


All times are GMT +2. The time now is 03:32 PM.



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