Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Specify array size?

anyone knows how to specify array size?

public String[] videoNames = {""}

the array above can accomodate only one value. i want to extend how many values this array can accomodate.

like image 624
Kris Avatar asked Apr 19 '26 04:04

Kris


2 Answers

You should write it like this.

String[] videoNames = new String[5]; // create videoNames array with length = 5
for(int i=0;i<videoNames.length();i++)
{
   // IMPORTANT : for each videoName, instantiate as new String.
   videoNames[i] = new String(""); 
}
like image 189
Rudy Avatar answered Apr 21 '26 18:04

Rudy


if you want dynamic size then use arraylist. something like:

public ArrayList<String> myList = new ArrayList<String>();

...

myList.add("blah");

...

for(int i = 0, l = myList.size(); i < l; i++) {
  // do stuff with array items
  Log.d("myapp", "item: " + myList.get(i));
}
like image 25
wired00 Avatar answered Apr 21 '26 17:04

wired00



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!