2

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?

Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180
  • 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 to ArrayList or change the add only objects of Info to arraylist – AJJ Jan 10 '14 at 07:54
  • sorry my fault, i wrote the false code in here. I edit it. i have everywhre ` –  Jan 10 '14 at 07:56
  • @aut_silvia Still you are getting the same error? and what is logInfo in your code? – AJJ Jan 10 '14 at 07:59
  • I did this, and no error anymore: `ArrayList[] listForLg = (ArrayList[])new ArrayList[4];` –  Jan 10 '14 at 08:02
  • @aut_silvia: casting ArrayList[] onto ArrayList is bad programming. Did you ever solve this issue? – Justin Kyle Parton Feb 11 '15 at 16:07

3 Answers3

2

Use arrays

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

Then access them like

list[0] = "123"; 
  • hm ok but I have a problem. I have a ArrayList of own object. `ArrayList list = new ArrayList();` and then it says me an error that i cant create a generic array of `Info`. Can u help me? –  Jan 10 '14 at 07:39
  • You could read the generics tutorial [here](http://docs.oracle.com/javase/tutorial/extra/generics/fineprint.html) or just read [this](http://stackoverflow.com/questions/5662394/java-1-6-creating-an-array-of-listt) question. – Ceiling Gecko Jan 10 '14 at 07:45
  • If `list` is an array of `List`s, then you can't assign a string to it. – chrylis -cautiouslyoptimistic- Jan 10 '14 at 07:52
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

nitesh
  • 4,582
  • 3
  • 25
  • 44
  • hm ok but I have a problem. I have a ArrayList of own object. ArrayList list = new ArrayList(); and then it says me an error that i cant create a generic array of Info. Can u help me? –  Jan 10 '14 at 07:42
  • @aut_silvia i think you have different type of objects to be added to the ArrayList. When you declare ArrayList, 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 – AJJ Jan 10 '14 at 07:50
  • no i just have object of info. that should not be the problem –  Jan 10 '14 at 07:51
  • You code in your question says that you have an object of InfoForLog as listForLg[i] = new ArrayList(); – AJJ Jan 10 '14 at 07:52
  • @aut_silvia your Generic Class are Different..i.e InfoForLog and Info – nitesh Jan 10 '14 at 07:58
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);
keshav
  • 3,167
  • 1
  • 14
  • 22