1、泛型和类型安全的容器
ArrayList,可以自动扩充大小的数组,add插入对象,get访问对象,size查看对象数目。
1 /**
2 * 泛型和类型安全的容器
3 * 2016/5/6
4 **/
5 package cn.Java_7;
6
7 import java.util.ArrayList;
8
9 class Dog{
10 Dog(int num){
11 System.out.println("new Dog num :"+num);
12 }
13 public void add(){
14 System.out.println("又买了一条小狗,我为什么要说又?");
15 }
16 }
17 public class Think_11 {
18
19 public static void main(String[] args) {
20 ArrayList<Dog> list= new ArrayList<Dog>();
21 for(int i = 0; i <= 3; i++){
22 list.add(new Dog(i));
23 }
24 ((Dog)list.get(1)).add();
25 for(Dog u: list){
26 System.out.println("list:"+u);
27 }
28 }
29 }
结果:
new Dog num :0
new Dog num :1
new Dog num :2
new Dog num :3
又买了一条小狗,我为什么要说又?
list:cn.Java_7.Dog@15db9742
list:cn.Java_7.Dog@6d06d69c
list:cn.Java_7.Dog@7852e922
list:cn.Java_7.Dog@4e25154f
注意这里调用Dog类中add()时所用的方法:((Dog)list.get(1)).add();
后面:list:cn.Java_7.Dog@15db9742,这个输出是从Object默认的toString()方法中产生的,类名加该对象的散列码。
2、容器基本概念:
1) Collection:List按照插入的顺序保存元素 Set不能有重复的元素 Queue按照排队列规则来确定对象产生的顺序。(注意Collection是一个接口,不能直接创建对象)
2) Map : 一组成对的“键值对”对象。
3、添加一组元素
ArrauList.asList()方法接受一个数组或是一个用逗号分割的元素列表,
Collection.addAll()方法接受一个Collection对象,以及一个数组或是用逗号分割的列表
1 /**
2 * java持有对象 添加一组元素
3 * 2016/5/6
4 **/
5 package cn.Java_7;
6 import java.util.*;
7
8 public class Think_11_3 {
9 public static void main(String[] args) {
10 Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5));
11 Integer[] moreInts = {6, 7, 8, 9, 10};
12 collection.addAll(Arrays.asList(moreInts));
13
14 Collections.addAll(collection, 11,12, 13, 14, 15);
15 Collections.addAll(collection, moreInts);
16 System.out.println(collection);
17 List<Integer> list = Arrays.asList(16, 17,18, 19, 20);
18 //List.set(1, 99); //运行时错误 Cannot make a static reference to the non-static method set(int, Object) from the type List
19 System.out.println(list);
20 }
21 }
运行结果:
4、容器的打印
容器都有自己默认的打印行为,是使用toString()方法实现的。
1 /**
2 * 容器的打印
3 * 2016/5/6
4 **/
5 package cn.Java_7;
6
7 import java.util.*;
8 public class Think_11_4 {
9
10 public static Collection fill(Collection<String> collection){
11 collection.add("rat");
12 collection.add("dog");
13 collection.add("pig");
14 collection.add("cat");
15 return collection;
16 }
17
18 public static Map fill(Map<String,String> map){
19 map.put("rat","Rat_Name");
20 map.put("dog","Dog_Name");
21 map.put("pig","Pig_Name");
22 map.put("cat","Cat.Namt");
23 return map;
24 }
25
26
27 public static void main(String[] args) {
28 System.out.println(fill(new ArrayList<String>()));
29 System.out.println(fill(new LinkedList<String>()));
30 System.out.println(fill(new HashSet<String>()));
31 System.out.println(fill(new TreeSet<String>()));
32 System.out.println(fill(new LinkedHashSet<String>()));
33 System.out.println(fill(new HashMap<String, String>()));
34 System.out.println(fill(new TreeMap<String, String>()));
35 System.out.println(fill(new LinkedHashMap<String,String>()));
36 }
37
38 }
运行结果:
5:List
List有两种:ArrayList,随机访问元素快,中间插入和删除操作慢。
LinkedList,随机访问慢,但是中间插入和删除快,类似链表。
List常用方法:
1 /**
2 * java 容器List常用方法
3 * 2016/5/6
4 **/
5 package cn.Java_7;
6
7 import java.util.*;
8 class Test{
9 int num = 0;
10 Test(int num){
11 this.num = num;
12 }
13 public String toString(){
14 return "Test"+num;
15 }
16 }
17 public class Think_11_5 {
18
19 public static void main(String[] args) {
20
21 List<Test> test = new ArrayList<Test>();
22 Test test_x = new Test(100);
23 //添加元素
24 test.add(test_x);
25 test.add(new Test(10));
26 test.add(new Test(11));
27 test.add(new Test(12));
28 System.out.println(test);
29
30 //判断是否为空
31 System.out.println(test.isEmpty());
32
33 //判断容器是否包含该元素
34 System.out.println(test.contains(test_x));
35
36 //显示索引
37 System.out.println(test.indexOf(test_x));
38
39 //移除元素
40 test.remove(test_x);
41 System.out.println(test);
42
43 //删除所有元素
44 test.removeAll(test);
45 System.out.println(test);
46
47 }
48
49 }
运行结果:
6、迭代器 Iterator
在没有迭代器之前,遍历需要这样:
1 for(int i = 0; i < newList.size(); i++){
2 System.out.println(newList.get(i));
3 }
迭代器写法(迭代器被称为轻量级对象,创建代价比较小)
1 Iterator<Member> iterator = members.iterator();
2 while(iterator.hasNext()){
3 System.out.println(iterator.next());
4 }
7、更强大的迭代器 ListIterator
ListIterator比Iterator更加强大,ListIterator既可以向前移动,又可以向后移动。
1 ListIterator<Member> iterator = members.listIterator();
2 while(iterator.hasNext()){
3 System.out.println(iterator.next());
4 }
5
6 while(iterator.hasPrevious()){
7 System.out.println(iterator.previous());
8 }
8、LinkedList
LinkedList和ArrayList类似,实现了基本的List接口,但是LinkedList的插入和移除操作比ArrayList快,但是随机访问方面要差一些。
1 /**
2 * LinkdList常用方法
3 * 2016/5/8
4 **/
5 package cn.Java_7;
6
7 import java.util.Iterator;
8 import java.util.LinkedList;
9
10 class Book{
11 int num;
12 String name;
13 Book(int num,String name){
14 this.num = num;
15 this.name = name;
16 }
17 }
18
19 public class Think_11_7 {
20 public static void main(String[] args) {
21 Book book_1 = new Book(1,"English");
22 Book book_2 = new Book(2,"Chinese");
23 Book book_3 = new Book(3,"Math");
24 Book book_4 = new Book(4,"History");
25
26 LinkedList<Book> book_List = new LinkedList<Book>();
27
28 book_List.add(book_1);
29 book_List.addFirst(book_2); //在列表开头添加一个数据
30 book_List.addLast(book_3); //在列表结尾添加一个数据
31 book_List.add(book_4);
32
33 Iterator<Book> iterator = book_List.iterator();//迭代器
34 while(iterator.hasNext()){
35 Book bk = iterator.next();
36 System.out.println("书名:"+bk.name+" 数量:"+bk.num);
37 }
38
39 book_List.removeLast(); //移除列表最后一个元素
40 book_List.removeFirst();
41 System.out.println(book_List.get(1));//返回指定位置的元素
42
43 }
44 }
运行结果:
9、Stack
栈,后进先出(LIFO)
用LinkedList就可以实现栈的功能了,push,进的时候,只需要addFirst,pop,出的时候,只需要removeFirst,这样就达到了先进后出
10、Set
set不保存重复的元素。(HashSet,没有重复元素,顺序也无规律,其实是使用了散列。)
1 /**
2 * Set的使用方法
3 * 2016/5/8
4 **/
5 package cn.Java_7;
6 import java.util.*;
7 public class Think_11_9 {
8
9 public static void main(String[] args) {
10 Random rand = new Random(400);
11
12 Set<Integer> intset = new HashSet<Integer>();
13 for(int i = 0;i < 30;i++){
14 intset.add(rand.nextInt(30));
15 }
16 System.out.println(intset);
17 }
18 }
11、Map
1 /**
2 * java Map的使用
3 * 2016/6/8
4 **/
5 package cn.Java_7;
6
7 import java.util.*;
8
9 public class Think_11_10 {
10
11 public static void main(String[] args) {
12 Random rand = new Random(400);
13 Map<Integer,Integer> m = new HashMap<Integer,Integer>();
14 for(int i = 0;i < 30;i++){
15 int r = rand.nextInt(20);
16 Integer freq = m.get(r);
17 m.put(r,freq == null? 1:freq+1);
18 }
19 System.out.println(m);
20 }
21 }
运行结果:
TreeMap(可以对自动排序):
1 TreeMap<Integer,String> tm = new TreeMap<Integer,String>(); 2 tm.put(1,"apple"); 3 tm.put(3,"orange"); 4 tm.put(2,"banana"); 5 tm.put(4,"pear"); 6 tm.put(-1,"pineapke"); 7 System.out.println(tm);