0

Developing a Java Android App but this is a straight up Java question i think.

I have List declared as follows;

List list= new ArrayList<String[]>();

I want to extract each String [] in a loop;

for(int i=0; i < list.size(); i++) {
   //get each String[]
   String[] teamDetails = (String[])list.get(i);
}

This errors, I am guessing it just doesn't like me casting the String[] like this.

Can anyone suggest a way to extract the String[] from my List?

Vojtech Ruzicka
  • 16,384
  • 15
  • 63
  • 66
Fearghal
  • 10,569
  • 17
  • 55
  • 97
  • my bad i changed code last sec for clarity, my effort failed :) editing now – Fearghal Sep 08 '15 at 10:20
  • What is the error? This should work (I'm guessing you missed `()` in the `size` call). – Tunaki Sep 08 '15 at 10:21
  • 7
    your declaration should be `List list= new ArrayList<>();`. This way you wouldn't need the cast – Blackbelt Sep 08 '15 at 10:21
  • Don't use rawtypes. Ever. If you don't know what a rawtype is, this read about Java generics before "dev'ing" any apps. – Boris the Spider Sep 08 '15 at 10:24
  • Possible duplicate of [What is a raw type and why shouldn't we use it?](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Tom Sep 21 '16 at 11:10

5 Answers5

7

Use a List<String[]> and you can use the more up-to-date looping construct:

    List<String[]> list = new ArrayList<String[]>();

    //I want to extract each String[] in a loop;
    for ( String[] teamDetails : list) {

    }

    // To extract a specific one.
    String[] third = list.get(2);
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
1

Try declaring the list this way

List<String[]> list = new ArrayList<>();

for(int i = 0; i < list.size(); i++) {
    //get each String[]
    String[] teamDetails = list.get(i);
}

Moreover the call of your size function was wrong you need to add the brackets

PKuhn
  • 1,338
  • 1
  • 14
  • 30
0
    /*ArrayList to Array Conversion */
            String array[] = new String[arrlist.size()];              
            for(int j =0;j<arrlist.size();j++){
              array[j] = arrlist.get(j);
            }

//OR
/*ArrayList to Array Conversion */
        String frnames[]=friendsnames.toArray(new String[friendsnames.size()]);
Androider
  • 3,833
  • 2
  • 14
  • 24
0

In for loop change list.size to list.size()

And it works fine Check this https://ideone.com/jyVd0x

Manoj Kumar
  • 745
  • 2
  • 8
  • 29
0

First of all you need to change declaration from List list= new ArrayList<String[]>(); to List<String[]> list = new ArrayList();

After that you can do something like

String[] temp =  new String[list.size];

for(int i=0;i<list.size();i++)`
{
temp[i] = list.get(i);
}
Raj Tandel
  • 19
  • 6