0

Basically what I want to do is create a certain number of arraylists based on user input and compare them to see if they are the same. My idea for creating the arraylists would be to do something like:

int numArrays = sc.nextInt();

for(int i = 0; i < numArrays; i++) {
List<Integer> i = new ArrayList<Integer>();
}

But I know this doesn't work in Java. Is there any way for me to get the same or a similar result?

Ben Zhang
  • 3
  • 1
  • 2
    Create a list of lists. – Oliver Charlesworth Jan 05 '18 at 03:21
  • 3
    This is an [XY Problem](http://xyproblem.info/). Each list will go out of scope and cease to exist as soon as the loop reaches the next iteration. Consider adding them to a list of lists instead. – shmosel Jan 05 '18 at 03:22
  • This is not the "way of Java". Variable names are not as important as you think that they are, and they almost don't exist in compiled code. What matters much more are references to objects, such as using a `List>` as suggested by @OliverCharlesworth. – Hovercraft Full Of Eels Jan 05 '18 at 03:28
  • Would I still have to create all the lists and add them to the list of lists, or is there a shorter way to do this? I found `listOfLists.add(new ArrayList());` works to make new lists, but I can't figure out how to reference these created lists. Is it just through the index of the list in the list of lists? – Ben Zhang Jan 05 '18 at 15:33

0 Answers0