1.引用數(shù)據(jù)類型數(shù)組
1.在其它包下創(chuàng)建一個(gè)學(xué)生類
public class Student {
//兩個(gè)私有成員變量
private String name;
private int age;
//空參構(gòu)造
public Student() {
super();
}
//有參構(gòu)造
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;
}
//重寫toString方法
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
#2.使用這個(gè)學(xué)生類創(chuàng)建引用數(shù)據(jù)類型數(shù)組
//int[] arr = new int[5];//創(chuàng)建基本數(shù)據(jù)類型數(shù)組
//這種創(chuàng)建數(shù)組的方式和這個(gè)iOS的差別還是蠻大的
Student[] arrs = new Student[5];//創(chuàng)建引用數(shù)據(jù)類型數(shù)組
arrs[0] = new Student("tmac",23);//創(chuàng)建一個(gè)學(xué)生對(duì)象薄嫡,存儲(chǔ)在數(shù)組中的第一個(gè)位置
arrs[1] = new Student("kobe",21);
arrs[2] = new Student("cluo",22);
arrs[3] = new Student("meixi",24);
arrs[4] = new Student("jjtmac",26);
for (int i = 0; i < arrs.length; i++){
//這里打印引用時(shí)栓拜,重寫toString方法
System.out.println(arrs[i]);
}
#注:弄懂這個(gè)數(shù)組在內(nèi)存中的存儲(chǔ)圖解
2.集合和數(shù)組的區(qū)別
123.png
#集合中兩個(gè)繼承者
List: 有序(存和取的順序一致)腮出,有索引,可以存儲(chǔ)重復(fù)值
Set: 無序(存和取的順序不一致)拍摇,無索引理张,不可以存儲(chǔ)重復(fù)值
1233.png
3. Collection接口的學(xué)習(xí)
//Collection學(xué)習(xí)
//1.Collection是個(gè)接口不能直接初始化。但是可以用實(shí)現(xiàn)了這個(gè)接口的子類
//利用父類引用指向子類對(duì)象
Collection c = new ArrayList();
//add方法,可以添加任意對(duì)象
boolean b1 = c.add("abc");
boolean b2 = c.add(true);//可以直接放boolean類型的唁奢,因?yàn)闀?huì)自動(dòng)裝箱
boolean b3 = c.add(100);//自動(dòng)裝箱
boolean b4 = c.add(new Student("tmac",5));
System.out.println(b4);//其實(shí)返回的都是true,但是還是要這樣設(shè)計(jì)的原因是:Set中存儲(chǔ)了重復(fù)的就會(huì)返回false
System.out.println(c);
System.out.println("1-------------------");
//2.刪除方法
Collection c1 = new ArrayList();
c1.add("tmac");
c1.add("jj");
c1.add("99");
c1.add("kobe");
System.out.println(c1);
//remove方法
c1.remove("99");//刪除指定元素
System.out.println(c1);
System.out.println("2-------------------");
//3.contains:是否包含窝剖,返回boolean
System.out.println(c1.contains("jj"));
System.out.println("3----------------------");
//4. clear清空
c1.clear();
System.out.println(c1);
System.out.println("4---------------------");
//5.isEmpty:判斷是否為空
System.out.println(c1.isEmpty());
System.out.println("5---------------------");
//6. int size():獲取集合中元素的個(gè)數(shù)
c1.add("我愛你");
int num = c1.size();
System.out.println(num);
4. 集合的遍歷之集合轉(zhuǎn)數(shù)組遍歷
//1.
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("c");
c.add("d");
//toArray方法將集合轉(zhuǎn)成數(shù)組,這個(gè)方法返回的是Object[]的數(shù)組
Object[] strArr = c.toArray();
for (int i = 0; i < strArr.length; i++){
System.out.println(strArr[i]);
}
//存自定義對(duì)象
Collection c1 = new ArrayList();
c1.add(new Student("jj",89));
c1.add(new Student("jhj",78));
c1.add(new Student("jfjdk",165));
Object[] arr = c1.toArray();
for (int i = 0; i < arr.length; i++){
System.out.println(arr[i]);
}
5. 帶All方法功能演示
//1. boolean addAll(Collection c):集合中添加一個(gè)集合
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("c");
//創(chuàng)建第二個(gè)集合
Collection c1 = new ArrayList();
c1.add("d");
c1.add("e");
c1.add("f");
c.addAll(c1);
//注意這種情況麻掸,自己試試就知道什么效果了 c.add(c1);
System.out.println(c);
//2. boolean removeAll(Collection c):移除一個(gè)集合
c.removeAll(c1);//刪除的是交集
System.out.println(c);
//3. boolean containAll(Collection c):判斷是否包含這個(gè)集合
boolean result = c.containsAll(c1);
System.out.println(result);
//4. boolean retainAll(Collection c):保留交集
Collection c2 = new ArrayList();
c2.add("j");
c2.add("e");
c2.add("f");c1.retainAll(c2);//保留交集,當(dāng)c1改變了赐纱,就是true脊奋,如果c1沒有變,就返回false
System.out.println(c1);
7. 集合迭代器遍歷
//需要查看集合中的每個(gè)元素疙描,用迭代
//1.對(duì)集合中的元素進(jìn)行迭代-遍歷
Collection c = new ArrayList();
c.add("ta");
c.add("j");
c.add("dd");
c.add("h");
//獲取迭代器-iterator:迭代器
Iterator it = c.iterator();
//獲取到迭代器后诚隙,可以用迭代器中的方法了
//2.判斷集合中是否有元素
//boolean b = it.hasNext();
// System.out.println(b);
for (int i = c.size(); i > 0; i--){
//3.通過next()方法獲取其中的元素
boolean b = it.hasNext();
if (b){
Object ob = it.next();
System.out.println(ob);
}else{
i = 0;
System.out.println("集合中已經(jīng)沒有元素了");
}
}
System.out.println("1-----------------");
//4.我們自己的類放在集合中,如何迭代(上面是放的String,系統(tǒng)的類)
Collection c1 = new ArrayList();
c1.add(new Student("tmac",45));//我們?cè)谔砑舆@個(gè)Student對(duì)象時(shí)起胰,會(huì)做一個(gè)自動(dòng)類型提升
c1.add(new Student("kobe",23));// Object ob = new Student()
c1.add(new Student("jj",26));
c1.add(new Student("jjtmac",34));
Iterator it1 = c1.iterator();
while (it1.hasNext()){
Object ob = it1.next();
Student st = (Student)ob;
System.out.println(st.getName());
//我們?cè)谶@里想直接調(diào)用Student對(duì)象的方法久又,需要將ob向下轉(zhuǎn)型
System.out.println(ob);
}