IT十八掌作业_java基础第十天_集合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IT十八掌作业_java基础第十天_集合相关的知识,希望对你有一定的参考价值。
Student :
--------------
判断学生类对象内容是否相同,重写equals方法。需要三个条件同时满足name + age + sex都相同才相同。
练习Vector向量类。
---------------------
作业:
-------------
remove(int index); //删除指定位置的元素
remove(Object o); //删除指定对象,考查删除对象的规则是什么?
removeAll(Collection col);//删除指定集合中的所有元素。
contains(Object o); //是否包含
containsAll(Collection col);//是否包含另一个集合中所有元素。
------------------------------------------------------------------------------
解答:
1,
package com.it18zhang.stringdemo;
/**
* Student :
--------------
判断学生类对象内容是否相同,重写equals方法。需要三个条件同时满足name + age + sex都相同才相同。
* @author Liubx
*
*/
public class Student {
private String name;
private int age;
private char sex;
public Student() {
super();
}
public Student(String name, int age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public boolean equals(Object obj)
{
if(obj == null)
return false;
if(this == obj)
return true;
//判断name
boolean nameEqu = false;
if(obj.getClass() == Student.class){
Student s = (Student)obj;
if(this.name == null){
if(s.name == null){
nameEqu = true;
}
else{
nameEqu = false;
}
}
else{
nameEqu = this.name.equals(s.name);
}
//判断age
boolean ageEqu = (this.age == s.age);
//判断sex
boolean sexEqu = (this.sex == s.sex);
return nameEqu && ageEqu && sexEqu;
}
return false;
}
}
package com.it18zhang.stringdemo;
public class StudentDemo {
public static void main(String[] args) {
Student s1 = new Student("lucy",34,‘M‘);
Student s2 = new Student("tom",34,‘M‘);
Student s3 = new Student("lucy",35,‘M‘);
Student s4 = new Student("lucy",34,‘F‘);
Student s6 = new Student(null,34,‘M‘);
Student s5 = null;
Person p = new Person("lucy",34,‘M‘);
Object obj = null;
//调用equals方法判断
System.out.println("1--------"+s1.equals(s2)); //name
System.out.println("2--------"+s1.equals(s3));
System.out.println("3--------"+s1.equals(s4));
System.out.println("4--------"+s1.equals(s5));
System.out.println("5--------"+s1.equals(s6));
System.out.println("6--------"+s1.equals(p));
System.out.println("7--------"+s1.equals(obj));
}
}
2,
package com.it18zhang.code;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
List<Student> vector = new Vector<Student>();
Student s = new Student("lucy",34,‘M‘);
Student s1 = new Student("tom",24,‘F‘);
Student s2 = new Student("tomas",23,‘M‘);
Student s3 = new Student("tomasLee",25,‘F‘);
Student s4 = new Student("tomason",32,‘F‘);
Student s5 = new Student("tomasYang",28,‘M‘);
vector.add(s);
vector.add(s1);
vector.add(s2);
vector.add(s3);
vector.add(s4);
vector.add(s5);
System.out.println("vector的长度为"+vector.size());
System.out.println("------------------------------");
//使用迭代器遍历打印Vector
Iterator<Student> it = vector.iterator();
while(it.hasNext())
{
System.out.println(it.next().toString());
}
System.out.println("--------------------方法练习-------------------");
System.out.println("-------revove(0)------------");
// remove(int index); //删除指定位置的元素
vector.remove(0);
Iterator<Student> it1 = vector.iterator();
while(it1.hasNext())
{
System.out.println(it1.next());
}
System.out.println("-------remove(obj)-----------");
//remove(Object o); //删除指定对象,考查删除对象的规则是什么?
vector.remove(s5);
Iterator<Student> it2 = vector.iterator();
while(it2.hasNext())
{
System.out.println(it2.next().toString());
}
System.out.println("----------contains(obj)----------");
// contains(Object o); //是否包含
System.out.println("vector集合中contains : "+vector.contains(s1));
System.out.println("----------containsAll(col)-----------");
// contains(Collection col);//是否包含集合。
List<Student> v2 = new Vector<Student>();
v2.add(s1);
v2.add(s2);
System.out.println("vector集合是否包含v2集合: "+vector.containsAll(v2));
System.out.println("----------removeAll(obj)-------");
// removeAll(Collection col);//删除指定集合中的所有元素。
vector.removeAll(vector);
System.out.println("vector的长度为"+vector.size());
}
}
package com.it18zhang.code;
public class Student {
private String name;
private int age;
private char sex;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name, int age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String toString(){
return name+" age :"+age+" sex:"+sex;
}
public boolean equals(Object obj)
{
if(obj == null)
return false;
if(this == obj)
return true;
//判断name
boolean nameEqu = false;
if(obj.getClass() == Student.class){
Student s = (Student)obj;
if(this.name == null){
if(s.name == null){
nameEqu = true;
}
else{
nameEqu = false;
}
}
else{
nameEqu = this.name.equals(s.name);
}
//判断age
boolean ageEqu = (this.age == s.age);
//判断sex
boolean sexEqu = (this.sex == s.sex);
return nameEqu && ageEqu && sexEqu;
}
return false;
}
}
本文出自 “菜鸟成就数据之路” 博客,转载请与作者联系!
以上是关于IT十八掌作业_java基础第十天_集合的主要内容,如果未能解决你的问题,请参考以下文章