java 中的LIST接口 报错
List<String> contentList = new ArrayList<String>();
for (int i = 0; i < rowCount; i++){
String rowContent = rowsContents[i];
String[] timeandcontent = rowContent.split(" ",2);
TimeAndContent timeContent=new TimeAndContent();
timeContent.setTime(timeandcontent[0]);
timeContent.setContent(timeandcontent[1]);
contentList.add(0, timeContent.getTime());
contentList.add(1, timeContent.getContent());
//contentList.add(timeContent);
}
//去重
Iterator<String> it=contentList.iterator();
while(it.hasNext()){
String a=it.next();
if(contentList.contains(a)){
it.remove();
}else{
contentList.add(a);
}
}
//排序
Collections.sort(contentList);
//.sort(contentList, new UserComparator(SortType.desc));
StringBuilder builder = new StringBuilder();
for (int i = 0; i < contentList.size(); i++){
TimeAndContent timeContent = contentList.get(i);
builder.append(timeContent.getTime());
builder.append(" ");
builder.append(timeContent.getContent());
builder.append("\r");
}
其中TimeAndContent timeContent = contentList.get(i); 报错
Type mismatch: cannot convert from String to ResortByDel.TimeAndContent,List里面明明有get()这个方法的呀?请问如何解决
------解决方案--------------------cannot convert from String to ResortByDel.TimeAndContent,不能转换。
contentList.get(i);得到是字符串,不是TimeAndContent对象
------解决方案--------------------contentList 你定义的是String类型的,拿出来赋值给TimeAndContent对象,当然报错啦
------解决方案--------------------用一个String来接收list.get(i)的值然后再封装到你要的对象里面把
------解决方案--------------------
+1.