不行的那样会显示MessageBoardBean cannot be resolved to a variable
remove后面不是索引吗,直接把索引放进去不完了,索引设成变量的形式,整形的
int java.util.List.indexOf(Object o) Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
笨点可以这样写
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub MessageBoardBean bean1 = new MessageBoardBean("红楼梦","红楼梦的相关故事","曹雪芹");
MessageBoardBean bean2 = new MessageBoardBean("红楼梦1","红楼梦的相关故事1","曹雪芹1");
MessageBoardBean bean3 = new MessageBoardBean("红楼梦2","红楼梦的相关故事2","曹雪芹2");
List<MessageBoardBean> list= new ArrayList<MessageBoardBean>();
list.add(bean1);
list.add(bean2);
list.add(bean3);
List<MessageBoardBean> newlist=removeObj(list,"红楼梦1","红楼梦的相关故事1","曹雪芹1");
}
public static List removeObj(List<MessageBoardBean> list,String title,String author,String content){
for(int i=0;i<list.size();i++){
MessageBoardBean bean = list.get(i);
if(bean.getTitle().equals(title)&&bean.getAuthor().equals(author)&&bean.getContent().equals(content)){
list.remove(i);
}
}
return list;
}
Returns the index of the first occurrence of the specified element in this list,
or -1 if this list does not contain the element. More formally,
returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))),
or -1 if there is no such index.
是这样的
indeOf是查找o这个对象在list中存不存o.equals(get(i)),用的是equals
除非你重写equals方法,否则你新构建的对象肯定和list中的对象不相等(即你不可能得到你要删除对象在list中索引)
2、他目前这种情况只知道3个变量的值,并不知道索引