list remove泛型问题-----毕业设计遇到的问题综合总结
来源:互联网 发布:如何安装excel软件 编辑:程序博客网 时间:2024/05/18 21:50
public class Cat {
String name; //名字
String master; //主人名
public Cat(String name) {
this.name = name;
}
public Cat(String name,String master){
this.name = name;
this.master = master;
}
public String getName() { return name;}
public String getMaster() {return master;}
public String toString() {return name + " " + master;} //重写 toString()方法;
public boolean equals(Cat cat) {
if(cat.name.equals(this.name)&&cat.master.equals(this.master)) return true;
else return false;
} //重写equals()方法
}
import java.util.*;
public class Test3 {
public static void main(String[] args) {
Collection c = new ArrayList();
Cat n1 = new Cat("mimi","Mary");
c.add("abc");
c.add(new Integer(900));
c.add(n1); //添加元素
System.out.println(c); //打印 :[abc, 900, mimi Mary]
c.remove("abc");
c.remove(new Integer(900));//删除两个元素
System.out.println(c); //打印 :[mimi Mary]
System.out.println(n1.equals(new Cat("mimi","Mary")));//测试equals()方法 打印 : true
Cat n2= new Cat("mimi","Mary");
c.remove(n2); //删除n1
System.out.println(c); //打印 :[mimi Mary]这里的原因
}
}
=========================涉及到一个泛型问题====================================================
会调用object的类型的equals方法 因此需要这样做
ArrayList<Cat> list=new ArrayList<Cat>();这样才会调用Cat的equals方法。
String name; //名字
String master; //主人名
public Cat(String name) {
this.name = name;
}
public Cat(String name,String master){
this.name = name;
this.master = master;
}
public String getName() { return name;}
public String getMaster() {return master;}
public String toString() {return name + " " + master;} //重写 toString()方法;
public boolean equals(Cat cat) {
if(cat.name.equals(this.name)&&cat.master.equals(this.master)) return true;
else return false;
} //重写equals()方法
}
import java.util.*;
public class Test3 {
public static void main(String[] args) {
Collection c = new ArrayList();
Cat n1 = new Cat("mimi","Mary");
c.add("abc");
c.add(new Integer(900));
c.add(n1); //添加元素
System.out.println(c); //打印 :[abc, 900, mimi Mary]
c.remove("abc");
c.remove(new Integer(900));//删除两个元素
System.out.println(c); //打印 :[mimi Mary]
System.out.println(n1.equals(new Cat("mimi","Mary")));//测试equals()方法 打印 : true
Cat n2= new Cat("mimi","Mary");
c.remove(n2); //删除n1
System.out.println(c); //打印 :[mimi Mary]这里的原因
}
}
=========================涉及到一个泛型问题====================================================
会调用object的类型的equals方法 因此需要这样做
ArrayList<Cat> list=new ArrayList<Cat>();这样才会调用Cat的equals方法。
- list remove泛型问题-----毕业设计遇到的问题综合总结
- List使用remove(index);遇到的问题;
- 毕业设计综合问题
- 记一次处理 list 的 remove 方法遇到的问题
- java list remove的问题
- List遇到的问题
- 今天遇到的对List,ArrayList的remove方法的一个小问题
- 软件综合实习遇到的问题
- 做毕业设计遇到的关于fgets()函数的问题
- 毕业设计-基于JAVAEE的B2B2C网上商城-中遇到的问题
- List 集合remove问题
- list.remove(object)问题
- 毕业设计使用SSH框架遇到的问题及解决方法
- 毕业设计开发过程遇到的问题与解决方案记录
- java List remove 时的小问题
- List remove中要注意的问题
- java List remove 时的小问题
- List remove中要注意的问题
- Timestamp与java.util.date进行比较的问题
- Google Map API使用详解(四)——Google Map基本常识(中)
- jsessionid 对JAVA WEB jsessionid的剖析
- C#抽象方法与虚拟方法的区别
- mysql数据库取得某一段时间内的纪录
- list remove泛型问题-----毕业设计遇到的问题综合总结
- 毕业设计综合问题
- C# Winform 修改app.config文件
- c语言常用数学函数
- selenium webdriver------环境搭建(一)
- Java jdbc介绍
- Google Map API使用详解(五)——Google Map基本常识(下)
- Google Map API使用详解(六)——Google Map API Key的解读和使用技巧
- 艺术画