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!

6
  • 6
    I'm not sure why you would want to dynamically name an object. Can you elaborate more on what drives this request? Commented Nov 19, 2012 at 20:47
  • Thanks for the reply. I am trying to create an arraylist of arraylists out of data from a txt file. thus the scanner. Commented Nov 19, 2012 at 20:51
  • 1
    This would never compile. I'm guessing you're wanting a HashMap of sorts to track what truster has been entered? Commented Nov 19, 2012 at 20:51
  • Don't use raw ArrayLists, please Commented Nov 19, 2012 at 20:52
  • 2
    Maybe I'm reading this wrong, but won't "i" always be 1? Commented Nov 19, 2012 at 20:54

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.