1

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!

RobEarl
  • 7,862
  • 6
  • 35
  • 50
zeemy23
  • 693
  • 3
  • 9
  • 16

3 Answers3

6

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);

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
RobEarl
  • 7,862
  • 6
  • 35
  • 50
  • 1
    Try to program to interfaces: [What does it mean to “program to an interface”?](http://stackoverflow.com/q/383947/1065197). – Luiggi Mendoza Nov 19 '12 at 22:27
1

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").

Akber Choudhry
  • 1,755
  • 16
  • 24
1

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.

Michael Allen
  • 5,712
  • 3
  • 38
  • 63
  • Thanks for the reply. Yep, I'm familiar with those first few lessons, but I'm trying to create the arraylist inside the loop, and then put each arrayList in one large arrayList. I'm using the scanner to read a large file of ints. – zeemy23 Nov 19 '12 at 21:03
  • Too harsh with that, sorry. Best way to go is with a HashMap. Then you can use trusterMap.get([trustername+count]) to get the lists out. Or use an Iterator, since you won't know the keys exactly. – Michael Allen Nov 19 '12 at 21:08