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

Thread: ArrayList in Arralist

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList in Arralist

    I am adding an arraylist "cList" in another arrayList "userList" but "userList" only show's last element i.e. Dell and 8GB. Also it's showing the size of the userList=3 but it should be 2.

    Here is the chunk of my code:

    if (uList.get(0).equalsIgnoreCase("Apple")
    {
    Computer cConfig = new Compute("Apple", "4GB");
    cList.add(cConfig); // adding cConfig's elements in an arrayList "cList"
    userList.addAll(cList); // adding an arrayList "cList" in arrayList "userList"


    }

    if (uList.get(0).equalsIgnoreCase("Dell")
    {
    Computer cConfig1 = new Compute("Dell", "8GB");
    cList.add(cConfig1); // adding cConfig1's elements in an arrayList "cList"
    userList.addAll(cList); // adding an arrayList "cList" in arrayList "userList"

    }

    System.out.println("UserList Size: " + userList.size()); // here it's showing the size of userList=3 but it should be 2


    for (int i = 0; i < userList.size(); i++) {

    System.out.println(userList.get(i).brand);
    System.out.println(userList.get(i).memory);

    }

    Output:
    UserList Size: 3

    Dell
    8GB

    Dell
    8GB

    Dell
    8GB


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: ArrayList in Arralist

    Please provide your code without conditional statements (if) and with the information about the initial state of all the variables (particularly contents of lists). It produces ambiguity. And use [code=Java] /* your code here */ [//code] (with one closing slash) to turn highlighting on.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList in Arralist

    Thanks for you help. Here is the Java code:

    public class Computer {
     
    	public static String brand;
    	public static String memory;
     
            public static ArrayList<String> userChoiceList = new ArrayList<String>();
    	public static ArrayList<Computer> cList = new ArrayList<Computer>();
    	public static ArrayList<Computer> userList = new ArrayList<Computer>();
     
    	public Computer(String compBrand, String compMemory) {
    		brand = compBrand;
    		memory = compMemory;
    	}
     
            public void userPreferences() {
     
                   System.out.println("\nEnter your preferences from above menu: ");
                   userChoice = user_input.nextLine();
                   userChoiceList.add(userChoice);
     
                   printUserIntendedConfiguration(userChoiceList)
           }
     
          public void printUserIntendedConfiguration(String userChoiceList) {
     
                    Brand br = new Brand();
                    Memory mem = new Memory();
     
                    if (userChoiceList.get(0).equalsIgnoreCase("Apple") {
     
                             Computer cConfig = new Computer ("Apple", "4GB");
                             cList.add(cConfig);       /* adding cConfig's elements in an arrayList "cList" */
                             userList.add(cList);   
     
                      }
     
                      if (userChoiceList.get(0).equalsIgnoreCase("Dell") { 
     
                              Computer cConfig1 = new Compute("Dell", "8GB");
                              cList.add(cConfig1);     /* adding cConfig1's elements in an arrayList "cList"  */
                              userList.add(cList);    /* adding an arrayList "cList" in arrayList "userList"   */
     
                      }
     
                      for (int i = 0; i < userList.size(); i++) {
     
                                System.out.println(userList.get(i).brand);
                                System.out.println(userList.get(i).memory);
     
                        }



    Output:

    Dell
    8GB

    Dell
    8GB

    Dell
    8GB
    Last edited by techflyer; June 6th, 2013 at 04:51 PM. Reason: fixed code tags

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: ArrayList in Arralist

    We still have no idea, what uList is. And, hence, can't resolve conditionals in your code. Please, provide SSCCE, so that we can replicate the problem. And please, wrap the code into code tags I've mentioned previously.

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList in Arralist

    thanks. I have edited the above post and removed the if condition. My only question is that why the 'userList' arrayList showing only the last object of cList. Also, showing the size of userList=3 instead of 2. the output should be

    output:
    userList size = 2

    Apple
    4GB

    Dell
    8GB

    but its showing only Dell and 8GB

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: ArrayList in Arralist

    Looking at the code in post #3:

    The casts (both of them) in the constructor are not necessary.
    The declaration of cConfig1 appears to have a syntax error.

    You first add an item to cList and call userList.addAll(cList);
    userList now has one element.
    You add a second item to cList and call userList.addAll(cList);
    userList now has three elements. What makes you think it should have two?

  7. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList in Arralist

    What my program is about that from the menu user will choose the brand, for example "Apple". It will show all the Apple brand computer with technical detail. Then i will ask to narrow down the search, if user choose 4GB memory. It will show all the computer with Apple and 4GB memory.

    In this above code, I am creating an arraylist cList that storing all the brand and memory of computer. In first search, when user choose Apple it will show all the apple computer and at the same time I am storing these result in "userList" arrayList. I will use this userList to narrow down search. But this userList only showing the last item. Why is that? Am I doing something wrong?

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: ArrayList in Arralist

    Quote Originally Posted by techflyer View Post
    But this userList only showing the last item. Why is that? Am I doing something wrong?
    It is hard to say without seeing code that can compile and show the problem. We can only guess if/when/how many times the methods are being called and what the value(s) of parameters are. Post a SSCCE or be (much) more descriptive about what leads up to the problem.
    Print out the full length of the array lists and verify they hold what you think they hold, every step of the way if necessary.
    Post the revised version of the code any time you make a change and still have a question. Include the full text of any error messages.

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. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  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. [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
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM