简体   繁体   English

从列表中提取String []元素

[英]Extract String[] elements from a list

at my work I've got the following source code: 在我的工作中,我有以下源代码:

import java.util.ArrayList;
import java.util.List;

public class Temporaer
{
    public static void main(String[] args)
    {
        List stringArrayList = new java.util.ArrayList();
        stringArrayList.add(fillStringArrayElement("a", "b"));
        stringArrayList.add(fillStringArrayElement("c", "d"));

        String[] listElement;

        /*
         * I'm stuck here, because I don't know what I have to do
         */

        System.out.println(listElement.length);
    }

    //Just a method to fill a list easily
    private static String[] fillStringArrayElement (String firstElem, String secondElem)
    {
        String[] stringArrayListElement = new String[2];
        stringArrayListElement[0] = firstElem;
        stringArrayListElement[1] = secondElem;
        return stringArrayListElement;
    }
}

My goal is it to extract each list item and work with those. 我的目标是提取每个列表项并使用它们。

I tried to use the toArray[T[]) method as mentioned here . 我尝试使用此处提到的toArray[T[])方法。 Though it generates an java.lang.ArrayStoreException . 虽然会生成java.lang.ArrayStoreException Note: I cannot change the type of the list because the list is filled by an extern service. 注意:由于列表是由外部服务填充的,因此我无法更改列表的类型。 Maybe I have to convert the list first... 也许我必须先转换列表...

Can someone show me a way to achive my goal? 有人可以告诉我达到目标的方法吗? Thanks in advanced. 提前致谢。

Iterator is an interface in java used to iterate over a Collection like ArrayList or other Collection framework classes. Iterator是Java中的一个interface ,用于iterate ArrayList类的Collection或其他Collection框架类。 Before reading ArrayList make sure values are available using the size() method. 在读取ArrayList之前,请使用size()方法确保值可用。

Here a sample working snippet for your problem. 这里是您的问题的示例工作片段。

    String [] myArray ;
    if (stringArrayList.size()>0){
        Iterator<String [] > i = stringArrayList.iterator();
        while(i.hasNext()){
                  myArray =  i.next();
                  for(String s : myArray)
                      System.out.println(s);
                }
                }

        }     

Don't use Raw ArrayList , instead use Generics 不要使用Raw ArrayList ,而要使用Generics

Use this: 用这个:

    List<String[]> stringArrayList = new java.util.ArrayList<String[]>();
    stringArrayList.add(new String[]{"a", "b"});
    stringArrayList.add(new String[]{"c", "d"});

    //if you cant to convert the stringArrayList to an array:
    String[][] listElement = stringArrayList.toArray(new String[0][]);
    for (String[] inArr : listElement){
        for (String e : inArr){
            System.out.print(e + " ");
        }
        System.out.println();
    }
String[] listElement;

Above statements is incorrect because you are keeping arrays in list so your listElement must contain String[] . 上面的语句是不正确的,因为您将arrays保留在list因此listElement 必须包含String[]

String [][] listElement

Something like below. 像下面这样。

listElement=stringArrayList.toArray(new String[stringArrayList.size()][]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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