黑馬程式設計師 Java基礎 Day15我的學習筆記

2021-07-14 15:27:16 字數 3230 閱讀 2470

b:陣列和集合的區別

區別2:

c:陣列和集合什麼時候用

* 1,如果元素個數是固定的推薦用陣列

* 2,如果元素個數不是固定的推薦用集合

d:集合繼承體系圖

boolean add(e e)

boolean remove(object o)

void clear()

boolean contains(object o)

boolean isempty()

int size()

c: 問題如下圖(editplus為例)

b:案例演示

collection coll = new arraylist();

coll.add(new student("張三",23));// 存在向上轉型

coll.add(new student("李四",24));

coll.add(new student("王五",25));

coll.add(new student("趙六",26));

object arr = coll.toarray(); //將集合轉換成陣列

for (int i = 0; i < arr.length; i++)

boolean addall(collection c)

boolean removeall(collection c)

boolean containsall(collection c)

boolean retainall(collection c)

b:案例演示

collection c = new arraylist();

c.add("a");

c.add("b");

c.add("c");

iterator it = c.iterator(); //獲取迭代器的引用

while(it.hasnext())

collection c = new arraylist();

c.add(new student("張三",23));

c.add(new student("李四",24));

c.add(new student("王五",25));

c.add(new student("趙六",26));

c.add(new student("趙六",26));

for(iterator it = c.iterator();it.hasnext();)

system.out

.println("------------------------------");

iterator it = c.iterator(); //獲取迭代器

while(it.hasnext())

b:迭代器原始碼解析

void add(int

index,e element)

e remove(int

index)

e get(int

index)

e set(int

index,e element)

list

list = new arraylist();

list.add(new student("張三", 18));

list.add(new student("李四", 18));

list.add(new student("王五", 18));

list.add(new student("趙六", 18));

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

list

list = new arraylist();

list.add("a");

list.add("b");

list.add("world");

list.add("d");

list.add("e");

/*iterator it = list.iterator();

while(it.hasnext())

}*/

c:解決方案

listiterator lit = list.listiterator();     

//如果想在遍歷的過程中新增元素,可以用listiterator中的add方法

while(lit.hasnext())

}

boolean hasnext()是否有下乙個

boolean hasprevious()是否有前乙個

object

next()返回下乙個元素

object previous();返回上乙個元素

public

void

addelement(e obj)

public e elementat(int index)

public enumeration elements()

vector v = new vector();                

//建立集合物件,list的子類

v.addelement("a");

v.addelement("b");

v.addelement("c");

v.addelement("d");

//vector迭代

enumeration en = v.elements(); //獲取列舉

while(en.hasmoreelements())

b:鍊錶

b:list有三個兒子,我們到底使用誰呢?

1. 查詢多用arraylist

2. 增刪多用linkedlist

3. 如果都多arraylist

set集合的特點:

黑馬程式設計師 java基礎 封裝

asp.net unity開發 net培訓 期待與您交流!封裝表示吧資料項和方法隱藏在物件內部,在類定義中用private關鍵字來實現封裝。封裝有什麼好處?用private把類的細節與外界隔離起來,從而實現資料項和方法的隱藏,而要訪問這些資料項和方法唯一的途徑就是通過類本身,類才有資格呼叫它所擁有的...

黑馬程式設計師 Java基礎 陣列

1.陣列的定義格式 1 int arry new int 5 2 int arry new int 5 int arry 3 常見問題 陣列越界異常 空指標異常。2.陣列常見操作 1 遍歷,查詢陣列,輸出陣列 int arry new int 3 for int i 0 i 3 i int a fo...

黑馬程式設計師 C 基礎1

asp.net unity開發 net培訓 期待與您交流!1 c 中的資料型別 c 的資料型別分為值型別和引用型別兩類。值型別有整型 int long 實數型別,也叫小數型別 double float decimal 字元型 char 布林型別 bool 結構型別 struct 列舉型別 enum ...