3

I want to know how I can name a variable dynamically.

If there are 3 items in a list then it should create 3 different object for each item. Like:

ArrayList<String> list1;

ArrayList<String> list2;

ArrayList<String> list3;

So it should count up or anything like that.

if there are 4 items in a list it should create 4 variables.

How can I achieve that?

Edit:

I tried this but it says me an error that I can't create a generic array of Info.

ArrayList<Info> listForLg[] = new ArrayList<Info>[];
for (int i = 0; i < sizeOfKfzList; i++) {
    listForLg[i] = new ArrayList<Info>();
    listForLg[i].add(logInfo);
}

Can you help me?

5
  • You are trying to add the object InfoForLog to the ArrayList listForLg which will accept only object of Info as in Line 1. Either change the line1 to ArrayList<Info> to ArrayList<InfoForLog> or change the add only objects of Info to arraylist Commented Jan 10, 2014 at 7:54
  • sorry my fault, i wrote the false code in here. I edit it. i have everywhre <Info Commented Jan 10, 2014 at 7:56
  • @aut_silvia Still you are getting the same error? and what is logInfo in your code? Commented Jan 10, 2014 at 7:59
  • I did this, and no error anymore: ArrayList<Info>[] listForLg = (ArrayList<Info>[])new ArrayList[4]; Commented Jan 10, 2014 at 8:02
  • @aut_silvia: casting ArrayList<Info>[] onto ArrayList<Info> is bad programming. Did you ever solve this issue? Commented Feb 11, 2015 at 16:07

3 Answers 3

2

Use arrays

ArrayList<String> list[] = new ArrayList<String>[3]; //or 4 or n

Then access them like

list[0] = "123"; 
Sign up to request clarification or add additional context in comments.

3 Comments

hm ok but I have a problem. I have a ArrayList of own object. ArrayList<Info> list = new ArrayList<Info>(); and then it says me an error that i cant create a generic array of Info. Can u help me?
You could read the generics tutorial here or just read this question.
If list is an array of Lists, then you can't assign a string to it.
2

Use Array for this...

ArrayList<String> yourlist[] = new ArrayList<String>([your size here])

Based on index use your variable..

yourlist[0] yourlist[1]....and so on

Hope this could help

5 Comments

hm ok but I have a problem. I have a ArrayList of own object. ArrayList<Info> list = new ArrayList<Info>(); and then it says me an error that i cant create a generic array of Info. Can u help me?
@aut_silvia i think you have different type of objects to be added to the ArrayList. When you declare ArrayList<Info>, then you can only add the object of Info to the above ArrayList. To add different objects to the ArrayList, declare the ArrayList as ArrayList<Object>
no i just have object of info. that should not be the problem
You code in your question says that you have an object of InfoForLog as listForLg[i] = new ArrayList<InfoForLog>();
@aut_silvia your Generic Class are Different..i.e InfoForLog and Info
1

Create list or arraylist

ArrayList<ArrayList<String>> group = new ArrayList<ArrayList<String>>(4);

or

List<List<String>> group1 = new ArrayList<List<String>>(4);

Edit:

    ArrayList<String> list1 = new ArrayList<String>();
    ArrayList<String> list2 = new ArrayList<String>();

To add (u can add items dynamically by loop)

    group.add(list1);

and to get

    list2= group.get(0);

2 Comments

Ok and what else. how can I get the 4 variables then?
@aut_silvia see edits