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;

    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章