Results 1 to 6 of 6
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    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.

    Java 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?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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).

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    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?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    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?

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    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 >_<

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    fail I can't raise your rep without raising someone elses first lol.

Similar Threads

  1. Creating an Array of Objects
    By int80 in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 01:40 PM
  2. Replies: 21
    Last Post: 11-05-2008, 03:31 PM
  3. Creating an array of nonprimitive objects
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:46 PM
  4. Creating Array of LinkedList
    By sasikumardr in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 11:25 AM
  5. creating array at runtime
    By javaplus in forum New To Java
    Replies: 4
    Last Post: 11-08-2007, 11:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •