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

Thread: Returning ArrayList via user input

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Returning ArrayList via user input

    Lets say I have multiple ArrayLists

            ArrayList<String> list01 = new ArrayList<String>();
    	ArrayList<String> list02 = new ArrayList<String>();
    	ArrayList<String> list03 = new ArrayList<String>();
    	ArrayList<String> list04 = new ArrayList<String>();

    How would I be able to return a list that the user selects? I tried something like the following but it doesn't compile as I get an incompatible types error.

    Scanner scanner = new Scanner(System.in);
    	int one = scanner.nextInt();
    	int two = scanner.nextInt();
     
    	ArrayList test = "list" + one + two;
     
    	System.out.println( test.size() );


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Returning ArrayList via user input

    ArrayList test = "list" + one + two;

    In Java - unlike PHP, say - you can't create variables like that.

    Instead of (or as well as) using variables list01, list02 etc, put the lists into another collection: another list for instance. Then you can get the desired index in this list-of-lists from the user and extract the actual list they want to use.

    You can hold the lists in any collection you find useful. Another alternative is a map whose keys can be used to identify the list the user wants to work with:

    Map<String,List<String>> lists = new HashMap<String,List<String>>();
     
    lists.put("name", new ArrayList<String>());
    lists.put("address", new ArrayList<String>());
    lists.put("phone", new ArrayList<String>());
     
    System.out.println("Which list do you want? (name/address/phone)");
    Scanner scanner = new Scanner(System.in);
    String choice = scanner.nextLine().toLowerCase();
    List<String> list = lists.get(choice);

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Returning ArrayList via user input

    I see.

    Is it possible to create a 2D array of ArrayLists? So the array item array[0][1] would return list01?

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Returning ArrayList via user input

    Or to avoid bringing in another container, just use a switch statement, and return an ArrayList based on the control variable.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Returning ArrayList via user input

    Is it possible to create a 2D array of ArrayLists?
    Try it and see. It turns out you can't create what the compiler calls a "generic array" of ArrayList. That's why I used a collection (list or map).

Similar Threads

  1. User Input with a Do Loop
    By RadiantChaos in forum Loops & Control Statements
    Replies: 4
    Last Post: March 13th, 2012, 08:14 PM
  2. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 05:53 PM
  3. Valid user input
    By ChristopherLowe in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 05:53 PM
  4. User Input File Name
    By PineAppleKing in forum Java Theory & Questions
    Replies: 12
    Last Post: June 3rd, 2011, 11:23 AM
  5. url input stream returning blank
    By yo99 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: June 2nd, 2010, 09:14 PM