-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]);
        }
7
  • 1
    I'm not sure what's being asked here, do you just want to convert the array to a list? Commented May 26, 2016 at 21:36
  • i want to use Strings in the Array combiTop , to create new Objects(ArrayLists) with these names . Commented May 26, 2016 at 21:39
  • I still don't understand, can you give an example of the desired result? Commented May 26, 2016 at 21:40
  • private String Names[] ={"max","tom","paul"}; ArrayList<Integer> Names[1] = new ArrayList<>(); // it should create an new ArraList<Integer> tom Commented May 26, 2016 at 21:41
  • 1
    You can not define variables with dynamic variable names though, which seems to be what you're trying to do here. Commented May 26, 2016 at 21:46

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.