-2

I want to create an Arrayof Strings storing "names"; and these names should be used to create new ArrayLists named by the Strings in the Array.

It doesn't work this way. Is there a way to manage this or can you please explain why isn't that possible?

public String combiTop [] = {"1er","2er","3er","4er","5er","6er"};

topRowValue = new ArrayList<>();
        for (int i = 0; i < combiTop.length; i++) {
            ArrayList<Integer> combiTop[i] = new ArrayList<>();
            for (int j = 0; j < 6; j++) {
                combiTop[i].add(-1);
            }
            topRowValue.add(combiTop[i]);
        }
Nadim Baraky
  • 459
  • 5
  • 18
MaT
  • 1
  • 1
  • 1
    I'm not sure what's being asked here, do you just want to convert the array to a list? – pablochan May 26 '16 at 21:36
  • i want to use Strings in the Array combiTop , to create new Objects(ArrayLists) with these names . – MaT May 26 '16 at 21:39
  • I still don't understand, can you give an example of the desired result? – pablochan May 26 '16 at 21:40
  • private String Names[] ={"max","tom","paul"}; ArrayList Names[1] = new ArrayList<>(); // it should create an new ArraList tom – MaT May 26 '16 at 21:41
  • @MaT ArrayLists don't have _names_ though. You could use a ```Map>``` to map the names to the lists. – Jorn Vernee May 26 '16 at 21:43
  • why do you need the names? You are going to have all the arrayList inside another arraylist. For using the names you will need reflection only to loose the names when the for is ended. – Aimnox May 26 '16 at 21:44
  • 1
    You can not define variables with dynamic variable names though, which seems to be what you're trying to do here. – Jorn Vernee May 26 '16 at 21:46

1 Answers1

0

In Java, variables in the same function are of fixed type, so you can't assign an ArrayList value to a String array, or the values inside of it. Thus:

String combiTop; //you don't have to assign values in your demonstration

ArrayList<String> = new ArrayList<String>(combiTop.length);
for (String s : combiTop) { // this iterates through each value in the array
    topRowValue.add(s);
}
Nissa
  • 4,636
  • 8
  • 29
  • 37