Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: ArrayList of ArrayList?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList of ArrayList?

    Hello JavaProgrammingForums users!

    I come to you with this problem: I am attempting to create an ArrayList of ArrayLists for an assignment, but I'm hitting a roadblock when it comes to figuring out how to populate this construction. So far it seems like I'm able to make an arraylist and get it to print out a sample where all of the values are null, but I cannot figure out how to go further to changing those data values, add another row of data to the arraylist, etc.

    Here's what I've gotten so far:
    package test;
     
    import java.util.ArrayList;
     
     
    class SampleTableClass<Type> {
     
        private Type type;
        ArrayList<ArrayList<Type>> array;
     
     
        public void initializeArray()
        {
            array = new ArrayList<ArrayList<Type>>();
        }
     
        public void fillArray( int rows, int columns)
        {
            for (int r=0; r<rows; r++){
                array.add(r, new ArrayList<Type>());
                for (int c=0; c<columns; c++)         
                    array.get(r).add(c, type);                
            }
        }
     
        public void print5x5Array(int rows, int columns)
        {
            fillArray(rows, columns);
            Object[] arrayObjects = array.toArray();
            for (int r=0; r<rows; r++){
                for (int c=0; c<columns; c++){
                    System.out.println(arrayObjects[r]);
                }
            }
        }
    }
     
    public class Main {
           public static void main(String[] args){
           SampleTableClass array = new SampleTableClass();
           array.initializeArray();
           array.print5x5Array(1,5);
           }
    }

    I feel like I've probably made a simple mistake or failed to grasp one of the main concepts entirely, so feel free to tell me I've done everything wrong if I have, if that's what's necessary to fix it.

    Thanks!


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: ArrayList of ArrayList?

    Here's some code I wrote up in a couple seconds. I used Strings to show you an example without doing 100% of your homework. It's quite chivalrous of me to give you this much, but I'm assuming by your current knowledge that you're here to learn. so please do.

    import java.util.ArrayList;
     
    public class Main {
     
        private static ArrayList<ArrayList<String>> parent;
     
        public static void main(String[] args) 
        {  
            parent = new ArrayList<ArrayList<String>>();
        }
     
        public static void addChild(String[] childText)
        {
            ArrayList<String> child = new ArrayList<String>();
            for(String text : childText)
            {
                child.add(text);
            }
     
            parent.add(child);
        }
     
        public static void removeChild(int index)
        {
            if(index >= 0 && index < parent.size())
            {
                parent.remove(index);
            }
        }
     
    }

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: ArrayList of ArrayList?

    You could always take a look at this: http://www.javaprogrammingforums.com...t-example.html.

    It has samples of how to implement many common features of multi-dimensional ArrayLists (in that case, 2D).

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: ArrayList of ArrayList?

    Or I can get sniped by a mod. I should have used the search bar to look for that post.

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList of ArrayList?

    Okay, that helps quite alot, I had already re-examined my code and had seen that I needed to change the structure of how I had everything set up, and this helps some of the pieces fall into place.

    One other quick question though- I'm going to be implementing those functions with Object instead of String so that they can be filled up with whatever object that a user would want to do, but because of that I'm unsure how to put the syntax for that in my main called function, whenever I try to code a line such as

    parent.addChild(/*This is where I'm having trouble knowing what to put in here to correctly add a child*/);

    Any tips how to word this?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ArrayList of ArrayList?

    Quote Originally Posted by ben456123 View Post
    One other quick question though- I'm going to be implementing those functions with Object instead of String so that they can be filled up with whatever object that a user would want to do, but because of that I'm unsure how to put the syntax for that in my main called function, whenever I try to code a line such as
    You should almost definitely be using Generics instead of simply dealing with Objects- that way the user can still populate your data structure with anything, but you still have some semblance of type safety.

    Quote Originally Posted by ben456123 View Post
    parent.addChild(/*This is where I'm having trouble knowing what to put in here to correctly add a child*/);

    Any tips how to word this?
    Well, what do you want that method to take as a parameter? Another List? An array to be converted into a List? A single Object? Something else?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Arraylist help
    By TreeChopper in forum Collections and Generics
    Replies: 7
    Last Post: September 16th, 2010, 05:49 PM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM