概念
java中的集合類:是一種工具類,就像是容器,儲存任意數量的具有共同屬性的對象
集合的作用
集合框架的類型:
collection和map 都是接口,不能實例化
list和queue有序、可重復,set無序、不可重復
list添加元素兩種add方法
1、直接添加,元素添加在隊尾;
對象存入集合都變成object類型,取出時需要類型轉換
2、指定位置添加,指定的位置(從0開始)不能超過隊列的長度,否則報錯(數組下標越界)。
list的兩種addall方法:添加類的數組
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public void testadd(){ //add方法一 course cr1 = new course( "1" , "課程一" ); coursestoselect.add(cr1); course temp = (course)coursestoselect.get( 0 ); system.out.println( "添加了課程:" +temp.id+ ":" +temp.name); //add方法二,添加到指定位置 course cr2 = new course( "2" , "課程二" ); coursestoselect.add( 0 , cr2);; course temp2 = (course)coursestoselect.get( 0 ); system.out.println( "添加了課程:" +temp2.id+ ":" +temp2.name); //addall數組添加方法一 course[] cr34 = { new course( "3" , "課程三" ), new course( "4" , "課程四" )}; coursestoselect.addall(arrays.aslist(cr34)); //添加數組的方法 course temp3 = (course)coursestoselect.get( 2 ); course temp4 = (course)coursestoselect.get( 3 ); system.out.println( "添加了兩門課程:" +temp3.id+ ":" +temp3.name+ ";" +temp4.id+ ":" +temp4.name); //addall數組添加方法二,添加到指定位置 course[] cr56 = { new course( "5" , "課程五" ), new course( "6" , "課程六" )}; coursestoselect.addall( 2 , arrays.aslist(cr56)); course temp5 = (course)coursestoselect.get( 2 ); course temp6 = (course)coursestoselect.get( 3 ); system.out.println( "添加了兩門課程:" +temp5.id+ ":" +temp5.name+ ";" +temp6.id+ ":" +temp6.name); } |
遍歷list
1.for循環遍歷
1
2
3
4
5
6
7
8
9
10
11
12
|
/** * 取得list中的元素的方法 * @param args */ public void testget(){ int size = coursestoselect.size(); system.out.println( "有如下課程待選:" ); for ( int i= 0 ; i<size;i++){ course cr = (course)coursestoselect.get(i); system.out.println( "課程:" +cr.id+ ":" +cr.name); } } |
2.通過迭代器來遍歷list,迭代器只是用來遍歷集合中元素的,本身不具有存儲元素的功能。可以說它是依賴某個集合存在的,不能獨立存在。
1
2
3
4
5
6
7
8
9
10
11
12
|
/** * 通過迭代器來遍歷list * @param args */ public void testiterator(){ iterator it = coursestoselect.iterator(); system.out.println( "有如下課程待選(迭代器):" ); while (it.hasnext()){ course cr = (course)it.next(); //iterator的next方法 system.out.println( "課程:" +cr.id+ ":" +cr.name); } } |
3.通過for each 方法訪問集合元素
1
2
3
4
5
6
7
8
9
10
11
|
/** * 通過for each 方法訪問集合元素 * @param args */ public void testforeach(){ system.out.println( "有如下課程待選(for each):" ); for (object obj:coursestoselect){ course cr = (course)obj; //取出的元素一致都為object類型,需要強轉 system.out.println( "課程:" +cr.id+ ":" +cr.name); } } |
修改list中的元素。list中有個set方法
1
2
3
4
5
6
7
|
/** * 修改list中的元素 * @param args */ public void testmodify(){ coursestoselect.set( 4 , new course( "7" , "課程七" )); } |
刪除list中的元素。與add相似,有remove和removeall兩種
1
2
3
4
5
6
7
8
9
10
11
|
/** * 刪除list中的元素 * @param args */ public void testremore(){ course cr = (course)coursestoselect.get( 4 ); system.out.println( "我是課程:" +cr.id+ ":" +cr.name+ ",我即將被刪除了" ); coursestoselect.remove(cr); system.out.println( "刪掉了。。" ); testforeach(); //方法內部調用方法 } |
上述remove()中也可直接放入索引下標,即可直接刪除。如remove(4)
removeall是從一個集合中將另一個集合中的所有元素全部刪除。
1
2
3
4
5
6
7
|
public void testremore(){ course[] courses={(course)coursestoselect.get( 3 ),(course)coursestoselect.get( 4 )}; system.out.println( "我是課程:3,4,我即將被刪除了" ); coursestoselect.removeall(arrays.aslist(courses)); system.out.println( "刪掉了。。" ); testforeach(); //方法內部調用方法 } |
**實際編寫代碼中最好每個類中的屬性都私有(private),需要使用時再用getxx或setxx
1
2
3
4
5
6
7
|
private string id; public string getid(){ return id; } public string setid(){ this .id=id; } |
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://www.cnblogs.com/atingjia/p/6479888.html