首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java 中的LIST接口 报错解决方法

2013-01-20 
java 中的LIST接口 报错ListString contentList new ArrayListString()for (int i 0 i rowCou

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)的值然后再封装到你要的对象里面把
[解决办法]

引用:
用一个String来接收list.get(i)的值然后再封装到你要的对象里面把

+1.

热点排行