Java 如何从ArrayList中获取值
我的Java列表如下所示:Java 如何从ArrayList中获取值,java,android,arraylist,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: Toki
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
ArrayList al=new ArrayList();
城市:柏林,文档guid:08c14773-db81-4c43-8b66-51E5D39A4,内容:null
城市:阿姆斯特丹,文档guid:28c14773-db81-4c43-8b66-51E5D39A4,内容:null
城市:东京,文档guid:18c14773-db81-4c43-8b66-51E5D39A4,内容:null
我需要从内容中获得价值。下面的代码显示了所有值,但如何从内容中获取字符串
for (int i=0;i < al.size();i++){
Log.i("Value of element "+i, String.valueOf(al.get(i)));
}
for(int i=0;i
您可以这样做:
String[] splitString = al.get(0).split(": ");
System.out.println(splitString[splitString.length - 1]);
也就是说,您可能应该创建一个对象
来存储所有这些数据,然后将对象
放入数组列表
。for(int i=0;i for (int i=0;i < al.size();i++){
String currentValue = al.get(i);
String[] partsOfCurrentValue = currentValue.split(",");
for (String part : partsOfCurrentValue) {
If (part.trim().startsWith("Content")) {
String[] partsOfContent = part.split(":");
String valueOfContent = partsOfContent[1];
Log.i("Value of element "+i, valueOfContent);
break;
}
}
}
字符串currentValue=al.get(i);
字符串[]partsOfCurrentValue=currentValue.split(“,”);
用于(字符串部分:partsOfCurrentValue){
If(part.trim().startsWith(“内容”)){
字符串[]partsOfContent=part.split(“:”);
内容的字符串值=内容的部分[1];
Log.i(“元素值”+i,内容值);
打破
}
}
}
为此定义您自己的类:
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>();
你能重新表述一下信息吗。。。什么是“城市:柏林,文档guid:08c14773-db81-4c43-8b66-51e5d39a4b2d,内容:null”,一个对象?一个json?您的对象看起来怎么样?然后您的列表保留内容对象??是的,但我找不到它的定义。在您的示例中,如果(part.startsWith(“Content”){loop从未进入,可能是因为在“Content:…”部分之前有一个“”(空格),那么可能是创建
类的某个子字符串或匹配项?@user5523912。请查看上面编辑的代码。
System.out.println(infList.get(0).getCity());