简体   繁体   中英

Extract String arrays from List

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?

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);

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

    /*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()]);

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

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM