如何从ArrayList获取值

时间:2015-11-17 15:19:27

标签: java android arraylist

我的Java列表如下所示:

ArrayList<String> al = new ArrayList<String>();

City: Berlin, Document guid: 08c14773-db81-4c43-8b66-51e5d39a4b2d, Content: null
City: Amsterdam, Document guid: 28c14773-db81-4c43-8b66-51e5d39a4b2d, Content: null
City: Tokio, Document guid: 18c14773-db81-4c43-8b66-51e5d39a4b2d, Content: null

我需要从内容中获取价值。下面的代码显示了所有值,但我如何从内容

中获取字符串
 for (int i=0;i < al.size();i++){
   Log.i("Value of element "+i, String.valueOf(al.get(i)));
 }

3 个答案:

答案 0 :(得分:1)

你可以这样做:

String[] splitString = al.get(0).split(": ");
System.out.println(splitString[splitString.length - 1]);

话虽如此,您应该创建一个Object来存储所有这些数据,然后将Object放入ArrayList ...

答案 1 :(得分:0)

List<HistoricProcessInstance> finishedProcessInstances = historyService.createHistoricProcessInstanceQuery().finished().processDefinitionKey(processDefinitionName).list();

答案 2 :(得分:0)

为此定义自己的类:

public class INfo {

    private String city;
     private String uuid;
     private String content;
     /**
      * @param city
      * @param uuid
      * @param content
      */
     public INfo(String city, String uuid, String content) {
         this.city = city;
         this.uuid = uuid;
         this.content = content;
     }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }

    /**
     * @return the uuid
     */
    public String getUuid() {
        return uuid;
    }

    /**
     * @param uuid the uuid to set
     */
    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    /**
     * @return the content
     */
    public String getContent() {
        return content;
    }

    /**
     * @param content the content to set
     */
    public void setContent(String content) {
        this.content = content;
    }

}

然后定义你的列表

 List<INfo> infList = new ArrayList<INfo>();

最后List的每个elemet实际上都是Info类的一个实例,所以你可以通过调用getter获取对象的属性......如:

System.out.println(infList.get(0).getCity());