java基礎第十天_集合


練習vector 定義student類 name age sex 重寫equals

1、remove(int index);//刪除指定位置的元素

2、remove(Object o);//刪除指定對象,考查刪除對象的規則是什麼?

3、removeAll(Collection col);//刪除指定集合中的所有元素。

4、contains(Object o);//是否包含

5、contains(Collection col);//是否包含集合。


=================================================================================================

練習vector 定義student類 name age sex 重寫equals

重寫equals:先判斷是否對象爲空,然後判斷三個屬性相同才相同

public class Student {

private String name ;

private int age ;

private char sex ;

public Student(){

}

//重載構造函數

public Student(String name,int age,char sex){

this.name = name ;

this.age = age ;

this.sex = sex ;

}

//構造函數重載

public Student(String name, int age) {

super();

this.name = name;

this.age = age;

}


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;

}

/**

* 重寫equals方法.

*/

public boolean equals(Object obj) {

if(obj == null){

return false ;

}

if(this == obj){

return true ;

}

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 ;

}

}

//name不爲空

else{

nameEqu = this.name.equals(s.name);

}

//處理age

boolean ageEqu = (this.age == s.age) ;

//處理sex

                        

                    boolean sexEqu = false ;

//性別爲空

if(String.valueOf(this.sex)== null ){

if(String.valueOf(s.sex) == null){

sexEqu = true ;

}

else{

sexEqu = false ;

}

}

//性別不爲空

else{

sexEqu = String.valueOf(this.sex).equals(s.sex);

}

return nameEqu && ageEqu&&sexEqu ;

}

return false ;

}

}



import java.util.Vector;


public class VectorDemo {

public static void main(String[] args) {

Vector<Student> vector = new Vector<Student>();

Student s1 = new Student("tom", 10);

vector.add(s1);

vector.add(new Student("tom2",11));

vector.add(0, new Student("tom3", 12));

vector.remove(0);

vector.add(10, new Student());

System.out.println();

}

}

1、remove(int index);//刪除指定位置的元素

2、remove(Object o);//刪除指定對象,考查刪除對象的規則是什麼?

3、removeAll(Collection col);//刪除指定集合中的所有元素。

4、contains(Object o);//是否包含

5、contains(Collection col);//是否包含集合。

import java.util.ArrayList;

import java.util.List;


public class CollectionDemo {


public static void main(String[] args) {

List<Student> list = new ArrayList<Student>();

Student s1 = new Student("student1");

Student s2 = new Student("student2");

Student s3 = new Student("student3");

Student s4 = new Student("student4");

Student s5 = new Student("student5");

list.add(s1);

list.add(s2);

list.add(s3);

list.add(s4);

list.add(s5);

getlist(list);

System.out.println("刪除第一個學生=============");

list.remove(0);

getlist(list);

System.out.println("通過remove(object o)刪除對象");

list.remove(s2);

getlist(list);

System.out.println("list 是否包含 s1 " + list.contains(s1));

System.out.println("list 是否包含 s3 " + list.contains(s3));

List<Student> list2 = new ArrayList<Student>();

list2.add(s2);

list2.add(s3);

System.out.println("list 是否包含 list2 : " + list.containsAll(list2));

List<Student> list3 = new ArrayList<Student>();

list3.add(s3);

list3.add(s4);

System.out.println("list 是否包含 list3 : " + list.containsAll(list3));

list.removeAll(list3);

getlist(list);


}

public static void getlist(List<Student> list) {

for (int i = 0; i < list.size(); i++) {

Student s = list.get(i);

System.out.println(s.getName());

}

}


}


class Student {

private String name;

public Student(String name) {

this.name = name;

}


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}

}

問題:

心得:

1.List :列表 接口          實現  ArrayList()

contains()   //包含對象

containsAll()   //包含list、collection

remove() //移除對象

removeAll() //移除collection  可以從一個collection刪除包含交集的另一個collection

2.[ ]數組

---------------------

length //長度屬性

3.String

---------------------------

length() //長度方法

4.interface collection

---------------------------

size() //方法

isEmpty() //判斷集合爲空集合 運算

List   //有序,可重複

Set //無序,不重複

Map //Key-Value


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