zeemy23
zeemy23

Reputation: 681

Naming Arraylists in a loop - Java

I need to create an Arraylist in a while loop with a name based on variables also in the loop. Here's what I have:

while(myScanner.hasNextInt()){    

    int truster = myScanner.nextInt();
    int trustee = myScanner.nextInt();
    int i = 1;
    String j = Integer.toString(i);
    String listname = truster + j;

    if(listname.isEmpty()) {
        ArrayList listname = new ArrayList();
    } else {}
    listname.add(truster);

    i++;
}

The variable truster will show up more than once while being scanned, so the if statement is attempting to check if the arraylist already exists. I think I might have done that out of order, though.

Thanks for your help!

Upvotes: 1

Views: 1900

Answers (3)

RobEarl
RobEarl

Reputation: 7912

Store the ArrayLists in a Map:

Map<String, List<String> listMap = new HashMap<String,List<String>>();
while (myScanner.hasNextInt()){    
    // Stuff
    List<String> list = new ArrayList<String>();
    list.add(truster);
    listMap.put(listname, list);
}

Note the use of generics (the bits in <>) to define the type of Object the List and Map can contain.

You can access the values stored in the Map using listMap.get(listname);

Upvotes: 6

Michael Allen
Michael Allen

Reputation: 5828

Really not sure what you mean at all but you have some serious fundamental flaws with your code so I'll address those.

//We can define variables outside a while loop 
//and use those inside the loop so lets do that
Map trusterMap = new HashMap<String,ArrayList<String>>();

//i is not a "good" variable name, 
//since it doesn't explain it's purpose
Int count = 0;

while(myScanner.hasNextInt()) {    
    //Get the truster and trustee
    Int truster = myScanner.nextInt();
    Int trustee = myScanner.nextInt();

    //Originally you had:
    // String listname = truster + i;
    //I assume you meant something else here 
    //since the listname variable is already used

    //Add the truster concated with the count to the array
    //Note: when using + if the left element is a string 
    //then the right element will get autoboxed to a string

    //Having read your comments using a HashMap is the best way to do this.
    ArrayList<String> listname = new ArrayList<String>();
    listname.add(truster);
    trusterMap.put(truster + count, listname);
    i++;
}

Further, you are storing in myScanner a stream of Ints that will get fed in to the array, but which each have very different meanings (truster and trustee). Are you trying to read these in from a file, or user input? There are better ways of handling this and if you comment below with what you mean I'll update with a suggested solution.

Upvotes: 1

Akber Choudhry
Akber Choudhry

Reputation: 1785

If I understand you correctly, create a list of lists or, better yet, create a map in which the key is the dynamic name you want and the value is the newly created list. Wrap this in another method and call it like createNewList("name").

Upvotes: 1

Related Questions