There are few syntax errors in your code.
- First the
counter
did not have a data type.
userEntry
variable is not defined.
- no semicolon at
counter++
So, I editted your code snippet. Is this what you needed to do?
public static void main(String[] args) {
int counter = 0;
int userEntry = 4;
while (counter < userEntry){
LinkedList <String> list = new LinkedList<String> ();
counter++;
}
}
But as you are trying to create multiple linked lists, one option is to create an array of linked lists.
public static void main(String[] args) {
int counter = 0;
int userEntry = 4; // example
LinkedList[] listsArray = new LinkedList[userEntry];
while (counter < userEntry){
listsArray[counter] = new LinkedList<String> ();
counter++;
}
}
Or as you are familiar with LinkedLists, you can create a list of LinkedLists.
public static void main(String[] args) {
int counter = 0;
int userEntry = 4; // example
List<LinkedList<String>> listOfLinkedLists =
new LinkedList();
while (counter < userEntry){
LinkedList <String> list = new LinkedList<String> ();
listOfLinkedLists.add(list);
counter++;
}
}
I think what you are stucked at is this.
i'm not sure how to name the lists differently
If you are trying to create variable names at runtime, it will be not an easy task. You can take a look at this SO question too: Assigning variables with dynamic names in Java.
But, the better way is to use use an array or a collection like List
etc. as I mentioned above.