Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic ArrayList in Java

Tags:

java

arraylist

I have one doubt in java, can we create dynamic ArrayList or String[] inside a for-loop. For example in my app there are categories field is there (not static, obtaining from server), I want to create ArrayList or String[] object based on size of categories. How can I create a dynamic ArrayList in side a loop?

My code is as follows:

for(int i =0; i<20; i++)
{
    ArrayList <String> list(i) = new ArrayList<String>();
}

can we create an object like this? based on category position, I can call the list items like list(positon), can we do like this java?

like image 985
Ravikumar11 Avatar asked Sep 01 '26 05:09

Ravikumar11


1 Answers

ArrayList is a dynamic array, i.e. you can add new elements like,

ArrayList <String> list = new ArrayList<String>();
for(int i =0; i<20; i++)
{
 list.add("string"+i);
}

i can call the list items like list(positon),can we do like this java?

ArrayList have get(position) method to get Object from position.

String str1 = list.get(0);//0th position 
like image 98
Samir Mangroliya Avatar answered Sep 03 '26 20:09

Samir Mangroliya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!