慕函数1733451
2017-04-13 11:55
public boolean equals(Object obj){ if(this==obj){ System.out.print("你好1"); return true; } if(obj==null){ System.out.print("你好2"); return false; } if(!(obj instanceof subjects)){ System.out.print("你好3"); return false; } subjects subject=(subjects)obj; if(this.name==null){ if(subject.name==null){ System.out.print("你好4"); return true;} else return false; }else{ if(this.name==subject.name){ System.out.print("你好5"); return true;} else return false; }
运行结果:
subjectsToList得到一个课程数据:1 语文 王老师
subjectsToList得到一个课程数据:2 数学 刘老师
subjectsToList得到一个课程数据:3 政治 陈老师
取得课程:政治
你好1备选课程中是否包含课程:政治,true
新创建课程:政治
你好5备选课程中是否包含课程:政治,true
你好5备选课程中是否包含课程:语文,true
package test; public class Student { String name; public Student(String name) { this.name = name; } @Override public boolean equals(Object obj) { Student stu = (Student) obj; if (this.name==stu.name) return true; // a // if (this.name.equals(stu.name)) return true; // b return false; } public static void main(String[] args) { String name0 = "ddd"; String name1 = "ddd"; // name0 = new String("ddd"); // c // name1 = new String("ddd"); // d Student stu0 = new Student(name0); Student stu1 = new Student(name1); System.out.println(stu0.equals(stu1)); } /* * 现在简化模拟了你的写法, 控制台会打印true; * 如果解开c,d两行的注释, 控制台会打印false; * 如果注释a行, 解开b行注释, * 则无论c,d两行是否注释, 结果都会是true, * 这也是我们期待的结果, 所以应该使用equals比较字符串. * 为什么会这样, 去百度"Java字符串池". */ }
//测试list的contains方法 public void testContains(){ subjects subject=subjectsToList.get(0); System.out.println("取得课程:"+subject.name); System.out.println("备选课程中是否包含课程:"+subject.name+","+subjectsToList.contains(subject)); subjects subject2=new subjects(subject.id,subject.name,subject.teacher); System.out.println("新创建课程:"+subject2.name); System.out.println("备选课程中是否包含课程:"+subject2.name+","+subjectsToList.contains(subject2)); subjects sub=new subjects("12","语文","王老师"); System.out.println("备选课程中是否包含课程:"+sub.name+","+subjectsToList.contains(sub)); } public static void main(String[] args){ ListTest list=new ListTest(); list.subjectsAdd(); list.testContains(); /*list.testInt(); list.listAddArray(); //list.stringTest(); list.listAddIdArray(); list.modifyTest(); list.addChildSubjects(); list.testForEach1();*/ //list.testForEach(); //list.removeTest(); //list.removeIdTest(); //list.removeArray(); }
这个是部分的!可能你们更想看这个
import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class ListTest { public List<subjects> subjectsToList; public ListTest(){ this.subjectsToList=new ArrayList(); this.list=new ArrayList(); } public List<Integer> list; //测试添加整型变量 public void testInt(){ list.add(1); System.out.println("基本类型数据必须用包装类作"+list.get(0)); } public void subjectsAdd(){ //为subjectsToList添加数据! subjects ar=new subjects("1","语文","王老师"); subjectsToList.add(ar); //从subjectsToList中得到一个数据 subjects ar1=(subjects)subjectsToList.get(0); System.out.println("subjectsToList得到一个课程数据:"+ar1.id+" "+ar1.name+" "+ar1.teacher); ar1=new subjects("2","数学","刘老师"); //再往subjectsToList中添加一个数据,之前的数据往后推 subjectsToList.add(ar1); ar1=(subjects)subjectsToList.get(1); System.out.println("subjectsToList得到一个课程数据:"+ar1.id+" "+ar1.name+" "+ar1.teacher); ar1=new subjects("3","政治","陈老师"); //在指定位置添加数据,该位置上原来的数据往后移动 subjectsToList.add(0,ar1); ar1=(subjects)subjectsToList.get(0); System.out.println("subjectsToList得到一个课程数据:"+ar1.id+" "+ar1.name+" "+ar1.teacher); int size=subjectsToList.size(); //遍历方式一 /*for(int i=0;i<size;i++){ System.out.println("这是List中第"+i+"位的课程信息"); ar1=(subjects)subjectsToList.get(i); System.out.println("课程:"+ar1.id+" "+ar1.name+" "+ar1.teacher); } */ //遍历方式二 /*Iterator it=subjectsToList.iterator(); System.out.println("有如下课程信息:"); while(it.hasNext()){ ar1=(subjects)it.next(); System.out.println("课程:"+ar1.id+" "+ar1.name+" "+ar1.teacher); }*/ } //遍历方式三 for each public void testForEach(){ for(Object ar1:subjectsToList){ //subjects ar1=(subjects)obj; System.out.println("课程:"+ar1.id+" "+ar1.name+" "+ar1.teacher); } } //数组内信息整个添加 public void testForEach1(){ for(subjects obj:subjectsToList){ System.out.println("课程:"+obj.id+" "+obj.name+" "+obj.teacher); } } public void listAddArray(){ subjects[] subject={new subjects("4","英语","翟老师"),new subjects("5","地理","魏老师")}; subjectsToList.addAll(Arrays.asList(subject)); } //添加字符串测试 /*public void stringTest(){ String test="I love china!"; subjectsToList.add(5,test); String test1=(String)subjectsToList.get(5); System.out.println(test1); } */ //添加数组到指定位置 public void listAddIdArray(){ subjects[] subject={new subjects("6","地理","王老师"),new subjects("7","社会","刘老师")}; subjectsToList.addAll(0, Arrays.asList(subject)); } public void modifyTest(){ subjectsToList.set(2, new subjects("h","heike","hhhhh")); } public void removeTest(){ subjects move=(subjects)subjectsToList.get(0); System.out.println("我即将被删除,但你删错了,我是"+move.name+" "+move.teacher); subjectsToList.remove(move); testForEach(); } public void removeIdTest(){ subjectsToList.remove(0); System.out.println("我再删除0号!!"); testForEach(); } public void removeArray(){ subjects[] st={(subjects)subjectsToList.get(0),(subjects)subjectsToList.get(1),(subjects)subjectsToList.get(2)}; subjectsToList.removeAll(Arrays.asList(st)); System.out.println("这真是大清除呀!!只剩下这些了!"); testForEach(); } //添加子类对象测试 public void addChildSubjects(){ childSubjects child=new childSubjects("100","儿子来了","妈妈是谁?"); subjectsToList.add(0, child); } //测试list的contains方法 public void testContains(){ subjects subject=subjectsToList.get(0); System.out.println("取得课程:"+subject.name); System.out.println("备选课程中是否包含课程:"+subject.name+","+subjectsToList.contains(subject)); subjects subject2=new subjects(subject.id,subject.name,subject.teacher); System.out.println("新创建课程:"+subject2.name); System.out.println("备选课程中是否包含课程:"+subject2.name+","+subjectsToList.contains(subject2)); subjects sub=new subjects("12","语文","王老师"); System.out.println("备选课程中是否包含课程:"+sub.name+","+subjectsToList.contains(sub)); } public static void main(String[] args){ ListTest list=new ListTest(); list.subjectsAdd(); list.testContains(); /*list.testInt(); list.listAddArray(); //list.stringTest(); list.listAddIdArray(); list.modifyTest(); list.addChildSubjects(); list.testForEach1();*/ //list.testForEach(); //list.removeTest(); //list.removeIdTest(); //list.removeArray(); } }
这个是完整的!
亲把完整代码贴出来吧,看看。如果用==就可以的话就没必要重写equals方法了,this.name是String型的,一般是比较其值是否相等,用equals
把你的主方法main()里的代码贴出来看看
Java入门第三季
409792 学习 · 4340 问题
相似问题