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 9 of 9

Thread: How do you make a list that contains another list?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default How do you make a list that contains another list?

    Hi! I am finding it hard to make a list that contains another list of ints, like this;

     
    int[] xList = new int[500]; // Initialize xList
    int[] xListNr2 = new int[500]; //Another intList
    ?[] listOfLists = new ? [2]; // List made to contain several int[]
     
    for[...] // For-loop to declare xListsValue
     
     
    /* Since I don't know how to initialize "listOfLists" problems occure here */
    listOfLists[0] = xList;
    listOfLists[1] = xListNr2;

    I don't know what to put at the '?' marks, help?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you make a list that contains another list?

    Are you asking how to define a two dimensional array?
      int[][] twoD = new int[2][];
      twoD[0] = new int[6];      //  6 elements for first one
      twoD[1] = new int[123];   // 123 elements for second one
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How do you make a list that contains another list?

    Quote Originally Posted by Norm View Post
    Are you asking how to define a two dimensional array?
    Nope. I am asking how to define a array/list (or whatever it is called) that contains other lists;

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you make a list that contains another list?

    For an ArrayList read the API doc: Java Platform SE 7
    An ArrayList can contain a list of any type of object.

    Also see the tutorial:
    http://docs.oracle.com/javase/tutori...aces/list.html
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How do you make a list that contains another list?

    I looked into that a bit, didn't help me much; in-fact, not at all.

    Maybe explaining what I want to use it for will make things easier.

    I have a loop with a 'drawLine' in it, and for anyone who doesn't know drawLine just draws a line between the two x- and the two y-values that you set.
    In this loop i draw a line between every single point in the two lists (x- and y-list) and now I want to easily draw multiple lines (like, more than thousand).

    An example for how the list(s) can look and how I want draw the line.
     
         for(int i = 1; i<xList.length; i++){
    		xList[i] = i;
    		yList[i] = (int) (1.1*xList[i]);
         }
     
     
    //....In paint(Graphics g)...//
     
    	for(int i = 1; i<xList.length; i++){
     
    	        g.drawLine(x + xList[i-1], y + yList[i-1], x + xList[i], y + yList[i]);
     
    	}


    Now instead of having to initate a thousand different x- and y-lists and then using g.drawLine for each different list I want to make some sort of list or array that can hold these different lists.
    I wanted my words to turn into action,
    so I become a programmer

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you make a list that contains another list?

    If you don't want a 2 dimensional array or an arraylist, I have no idea what will solve your problem.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How do you make a list that contains another list?

    I think I'll just go with making it into an object and making a list of object references.

    Cheers!
    I wanted my words to turn into action,
    so I become a programmer

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do you make a list that contains another list?

    ArrayList<ArrayList<Integer>> listofLists=new ArrayList<ArrayList<Integer>>();

    ArrayList<Integer> singleLists=new ArrayList<Integer>();

    singleLists.add(1);
    singleLists.add(2);
    singleLists.add(3);
    listofLists.add(singleLists);

    --- Update ---

    ArrayList<ArrayList<Integer>> listofLists=new ArrayList<ArrayList<Integer>>();

    ArrayList<Integer> singleLists=new ArrayList<Integer>();

    singleLists.add(1);
    singleLists.add(2);
    singleLists.add(3);

    ArrayList<Integer> anotherlist=new ArrayList<Integer>();
    anotherlist.add(5);
    anotherlist.add(6);
    anotherlist.add(7);

    listofLists.add(singleLists);
    listofLists.add(anotherlist);

    for(ArrayList<Integer> i:listofLists)
    System.out.println(i);

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do you make a list that contains another list?

    @india, I'm unclear of the purpose of your post. You have posted code with absolutely no explanation, which leads me to believe you are trying to help through spoonfeeding, in which case please read http://www.javaprogrammingforums.com...n-feeding.html

Similar Threads

  1. Knapsack problem , check if matrix can fill list of smaller matrix list.
    By ofirattia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 8th, 2012, 01:20 PM
  2. Replies: 1
    Last Post: October 25th, 2012, 02:03 PM
  3. [SOLVED] How to make a List containing integers.
    By mwebb in forum Java Theory & Questions
    Replies: 1
    Last Post: October 12th, 2011, 07:39 AM
  4. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  5. SpinnerListModel setList(List<?> list)
    By roy epperson in forum Collections and Generics
    Replies: 4
    Last Post: November 29th, 2010, 10:30 AM