java 持有对象
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 }
运行结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAecAAAAbCAIAAADJSNDVAAAAA3NCSVQICAjb4U/gAAAJ+klEQVR4Xu1ce1BU1xm/y9vdBVyWZWF5LhAIIqIpERRS0iBKqERoZ2IStbGWQKLBNrUzSR21iTFaxooxY0Wnto0RStMklUp9JCalhpcPxAhZlRoeykNkXVh57vv28lgg7rfrPXtWBvDcv5hz7/f7vvM7v/vt5d4zPw5N0xQ5CAOEAcIAYWCaMGA3TeokZRIGCAOEAcLAEAMWuraq5jdSJ9Gc+HWftutHyBq4uHXZHLEjhyPOrhpA5U8vP7M5JcLLlcvleka98MGlHsRn/N6yjTGS2XwejyeMSN9V3mVALWDoelpZ+pofhxP/Nzli9oHKLDHHnuc2dIjidsrUqNk1Nz/9VWKAG4/PF8zdUNGPFD5QtTHSYzi1mxvPnsOJyb85uiLsYOgB2aHVC/x9AwIkvtEv5df1o03e0F2xc3m4j4+vjyTyp3sv9aJFUxQoG4OyamfqYz4SiSQiI6/GNphgInYUDV0FhOOKFsKkcJUM1Dk6SavlDdZJND8pmtc2H1m9KFzg+Pj2qxq2YmXekJg5Bi9tCo/aflU9flrTWnbmXN3nrwX5Z1X2m4kyO6yTVxQV13SqDYa+urw4fujmb1Rmr4VOaLsamru1zBl1c8FzotA3Lw9CV1kcMyjPvpGQ8pNg7uLCToPFK01O9le8Iv3B+41D+a051PW7Yzxifleu0NH6nqbrd63FMcj/meazKL8ZKV7fUZDoPn9PPbOSg7XvhLsmfSJHmb2qdkuY548LW3W07k7Jy0FRb3+LtnA0JJuBi28ES1463qnXtRWleUVsq7MBJpgIYbWAOnFFC5aEq2SgzuFZYsgbrJNoftI0P3Dxl6Fz35FN6LUWdWvhWdu07zv6JiyJDfFw4pieevCIvefiF1YsEDlxOLyQp2KF95q6t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dyuzRin2sAXCZyftWZJIBimgxKIaRtbJVNnomlkqwTyQgYJA4SBh8kAbte2iS8SOEGrzZJgNEwHJRAU31bJxJnIgq0SWAIZJAwQBh41BnC7tg18kUDKMcySQDxcByUQFNNWCXImsmCrBJZABgkDhIFHjgHzX1JN95CAtjWYvkggJp5ZEmTZQ2M6KMGYWLZKZpyJzNkqIZsVmF9ZcoYwQBiYxgzYwj0KnD6myQvBNGWA7Pwz5YSMEAYePQYs+JAQ96gp9I+XFRYzU6h6UgphgDBgOwYsdG3bJSFIhAHCAGGAMGAjBnC/RtqoDAJDGCAMEAYIA6wY+D/xYEHfj9KGVAAAAABJRU5ErkJggg==" alt="" />
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 }
运行结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYsAAAB1CAIAAAAwbVoBAAAAA3NCSVQICAjb4U/gAAAXJElEQVR4Xu2de0AU5frHd1lwuXjhfhNkWUBuR0FFLpp4SVHRnxkomGmgp5NlxzwdT5lWp9vvmP6s49HUzPJSaEn8Su2gZ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MyuHODwrXnMNi1+qDchhpubKjEqImloVNPvSrfNhk4RIoh5FgmX8uITUrtEqgHO7nEeNDv8qHTR4wgXKYB6hWZdIalMMWoGOkevG1sDKghDIgQAmFkgXUAeWwBQwCdAEIAAEaAqAcpgEDxUAACFgAAYhQFjAI0AUgAARoCECEogEDxUAACFgAAYhQFjAI0AUgAARoCECEogEDxUAACFgAAYhQFjAI0AUgAARoCECEogEDxUAACFgAgf8CCBUaeJ4ehWAAAAAASUVORK5CYII=" alt="" />
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 }
List常用方法
运行结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPQAAABWCAIAAAB3mSxMAAAAA3NCSVQICAjb4U/gAAAM6ElEQVR4Xu1de1RTRxq/EcIrJAgYkAASCI8gBUulgqKi4hFKBWXXttrV+mp9bmstfViq7tFjrZ4iW/t0W7erBy1Lq0eLtdTG1lWK2iK0R4RWxXWrKAoYEEN4JPdmb8K7mXu5MQ/vTb75i/PN3Jnf9/t+GSZzv8zwdDodBgUYsEcGh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bdzPI4nUZMmDsrXuzC4wlkkxJ9715VOs7vtSlyS6jpH1iDPlyKd6VgU3HE7uoz00SEuqFRp393T3OK1R+G6k/bWiEsPNCTthVu3vt/a+BkxpBprayKk1CW7jqEZe99BH0HnWlIudHaLHGjD5cSIK7qM40MS6dtWQunaV4N3dqKODsuffLM8qpFRYoUL8d522yWuMlwoQ6XQlzVN/iYKrowG9K2LpJpW6kJWMsRMm0rtS9tS9fV0qAkvPx83ExfTFkcJ40PrMOpuVb4bNZHsl2KjYkix5G2mT8QRh8uhbqqTy8FZqdY0aRt3ft+QUhARlEDQSMsZJU1cCIH6jayC6f25qEVj21x316yI03MfI6hcY9DVRRfdhnulmjqv9mQIQ8YGRgUIImY+toJJXn3amftx3NiRgWHhARJQifnfFWv7R0CbzqekxQcIo9LyMy7YPjOrq5Y97CPp5ueLldP3/jcynbSiDeXbUmXicViv8hZeedayR67i6Y2P85jzDYm92ob7UJYAycSPAmVXTjJQAaT55YKhN0lMLtY2Uuozt53S8hjLJEFLW5kUxsZ8fqC1MjFiuY+sdtoXFOH4QpO0i97FzfV6pXn6uvfXrQoY+WhevIlDhvKsJHzj1/8dPpwti8aOYJTe23/0sdXHcNGeg/ciGVDoC2HAVJeLccl9MQyBqhmbpbBBDjAgOkMgLhN5wye4AgDVOIenDhlcMYhUm04EjaAyYQBKnGTz7rKXzh44tM5kt7NUb504b7v9y80XIkDBRhgPwM04mY/eEAIDNAxAOKmYwfqOM0AiJvT4QPwdAyAuOnYgTpOMwDi5nT4ADwdAyBuOnagjtMMgLg5HT4AT8cAlbgRiVOOkGpDRxXUcY0BSJziWsQAL2MGqGZuxh1AQ2CArQyAuNkaGcBlNgMgbrMphA7YygCIm62RAVxmMwDiNptC6ICtDIC42RoZwGU2AyBusymEDtjKAIibrZEBXGYz8H/Ga3yvRnfLEwAAAABJRU5ErkJggg==" alt="" />
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 }
LinkedList
运行结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALMAAABYCAIAAAAx9VHlAAAAA3NCSVQICAjb4U/gAAAZ7ElEQVR4Xu1dd0ATSRfPEkJJIPSOUpUmIuVAml1RsCAqiA0VBHv5PD17x34qnmIXGzbsiigeYqMpoggoIIpIF2kJJSFlv9kEEMhuDA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/738YyG3OtHr+e2upUKEU3H+bvzexuBR19xh6AW+H08Q1CNxHSdYwHs3yadM794lt/VAmLP+F1XrqvlFntGV1v4d51f7Bm/68p1tdz/B+zoDIO+OV9vAAAAAElFTkSuQmCC" alt="" />
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 }
运行结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoQAAAAPCAIAAADyPUkqAAAAA3NCSVQICAjb4U/gAAAKtklEQVR4Xu1ce1QTVx6eEFFMApiEEAiCBBEE6gMXDQqVXZWW9VHF7qK29ri2qNtq2+3q1pW11rau1lq19Vhx93g8bkXt1j2VFVvtsbuuiuCjKA9Z5SAPFUVBHpqQ1ySZnYSAOLl3cieT4DnbO//lN/O73/f77nfvnbkzIKAoisAHVgArgBXACmAFsAJPTwG/pweNkbECWAGsAFYAK4AVsCuAF2PsA6wAVgArgBXACjxlBVgXY0Pl5udi1Ulprx6+a7XztHWWbpw+IlylUiVkb7ushe9va8++laIa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7+G6/Q1tFJ/ACmAFsAJYAazA/50C9D/9+M387TUhsz/Zt35KCOSDLGjVrIsxNAufwApgBbACWAGsAFbAawpwXby9BowbwgpgBbACWAGsAFagW4H/AYKIOLzEC6jVAAAAAElFTkSuQmCC" alt="" />
TreeMap(可以对自动排序):
TreeMap<Integer,String> tm = new TreeMap<Integer,String>();
tm.put(1,"apple");
tm.put(3,"orange");
tm.put(2,"banana");
tm.put(4,"pear");
tm.put(-1,"pineapke");
System.out.println(tm);
运行结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAW8AAAAQCAIAAABWXWnjAAAAA3NCSVQICAjb4U/gAAAKKUlEQVR4Xu1aeVhTVxZ/IURCAsgSZBVCorIpiFVAFhfKotAq4rC4MKA4KOPSdjZXrGuVjtZptdSFigo6ItWigo6iRasoKEMRBQsWAZHFAgEMgYQsb25eQNHcF15Mxk+/790/8j7uO/ec3+93zrvv3vugoCiKkI1UgFSAVEBjBXT6PfSWpwSPcnD1XZzdJMV12nM3OcTFgkahWCy93YNrpfKG9Ml3XvbxNwQqjTS8KX160MsyIp+voZtXh2uBOxE84rqjC30njDGiuX1VIyYygICNtDV/3QznEYYMBoM1Luab/z7Hf388vxg2wv9EK74BgXBvw4R/Y9VEa2MDJpNp5hy+4yZP9jaCvoUYaGdBki2F4vu2UyCuO7LQ25ntFJ76qE8Dmv2zSV/d2Yyq6Zl3Cg9HWlNx3dGsQ/6efjUvia2HazLUDaplxPc5yeP1h7J75+5rgTsRTjR2XGbhjfQoG10i1gRtKEy3pBMV7QJB67XF9etjUspFBAe+s2b6Yz89XdnWLRB0lG5A9yz6slz4zkJVAxjadWPLxjovDkONMdoxpbHjM4uKD7gWpV9pwV9NDBmrfzZBJSJU34SBP5Fgjmg2foFeXNNhlKHcduUGG1qHzA6dGew33jMu/ZGienvurA+e9IHHhJCtZb0DHviX57A4UfGRYQEe7NHhqVUKS8mzS8mhrmy2A5vtFvn1L3z5u1JcnxnvOYbD5XI5TtM/y3mK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otwZxHGC4Mrg5ZD0nBM3+M4IH8U2c/3Tqbz1bnSYIMTBC8u/+uRwZRMz1jFLPsgo8Pj9Mx+bYOMhTc/COC967ObaDoOA7Wdi5OtdPEZKg3G5q2FJKB3S9sJdcz8OaxShUh1Lv6TMI6F4Lzwdi4j9+65Fz+IekCI0TvTBkzNMcV6NxMHr2senbSqY62m/w4wzydWWSZOv4KBFi/EmxEhJIZUdymUDqxDcRwbHNwrW+xTFPgHHYshuxWmLuPabCZYRubw3P4B55dQGnC+aTsluf6OTHLUGaR5I2pzx4ZhF+R1aYo6DHooT2onjAN79dsDDY/9/et8PRjKRABzbgo8EHVcWj5q897FYhRhaZ6R52cDRSltOBLD8M1s0eBL61ya6tn/YNP/8hmm+P6zPTnvTXdOQM9e7aKBjufBK1cJ3ERkBTO81eCi/94ORoOivU5dcE9GpUl2XlYfj2ar+Oei9YAT+ey0+Zk8VK+nLEHOctRs0Xa91UsA8RcSOtCEVIBUgFVCtgOrDAtVjybukAqQCpAIvFSBnE7IaSAVIBbSjADmbaEdH0gupAKkAOZuQNUAqQCqgHQX+B7U3ZIAMAZMjAAAAAElFTkSuQmCC" alt="" />
12、Queue
队列是一个先进先出(FIFO)的容器。LinkedList实现了Queue接口。
代码实现:
1 /**
2 * java Queue的使用
3 * 2016/6/8
4 **/
5 package cn.Java_7;
6
7 import java.util.*;
8
9 public class Think_11_11 {
10
11 public static void printQueue(Queue queue){
12 while(queue.peek() != null){
13 System.out.print(queue.remove() + " ");
14 }
15 System.out.println();
16 }
17
18 public static void main(String[] args) {
19 Random rand = new Random(47);
20 Queue<Integer> queue = new LinkedList<Integer>();
21 for(int i = 0;i < 10;i++){
22 queue.offer(rand.nextInt(i+10));
23 }
24 printQueue(queue);
25 Queue<Character> qc = new LinkedList<Character>();
26 for(char c: "Brontosaurus".toCharArray()){
27 qc.offer(c);
28 }
29 printQueue(qc);
30 }
31 }
运行结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALoAAAAjCA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YmL8jh/nxaC35uoeC4ORP4MBQkHSu2/Hl99X14F4XoTEnGFFZZ4ROrNu368md7KBufn+vpuPQn/qe6J2VCWmZBiaZN205QELX0SfBJAZBCRYOjEtCBfcKwlLmPxWqPnmsnumWMIB6H3B4K2X1XmBHA8JyggbEfsn0GL5UUEsyfXvJL/f4Vk1j1O/NSHjrYBtp1tKs3L95se+GbmtqamlNbMthubLQE5uCpw2NF3Ygb7HWzO0k+yWKSrgiYfT8DoVwgpPPSiUZmVBSPnBzAAYNjZyYajnz1K/ZeM10+dNw8fkZMENibvAVmcjjuLr2FeO5GIbq7V9vYE5o8c9F/Nq7P5Wp/6wbJBTF32vgJY8KYENJ2an+tmSBnpjCa21mvd0Bob115o3d298ljkq6IoIJbJvyTQWhv+cNyERtGUH7q0g/3zREMflqOOBhDsuD9nd8XzFPuRqBARcEHB3OEw4W8ETAkY/VS9InJSdvFcY/tPbQ6CVeEFKK7DKdemb+x2gyjTobikXc3jQFtxrPT/vtq0sIHsysUIrZIKOMYwQQEj1v5UoJqXkbV2LgYHsc7G8/kMUlXBC7gtoXeYiTDEu1zkwHilxFNE83AHQzQcqEFQYEBWi4UyKJdabnQGqDAAC0XCmTRrrRcaA1QYICWCwWyaNe/AC1hOleuDHtFAAAAAElFTkSuQmCC" alt="" />
13、PriorityQueue
优先级队列,申明下一个弹出元素是最需要的元素,(具有最高优先级)
代码实现:
1 /**
2 * java PriorityQueue的使用
3 * 2016/6/8
4 **/
5 package cn.Java_7;
6
7 import java.util.*;
8
9 public class Think_11_11_1 {
10
11 public static void printQueue(Queue queue){
12 while(queue.peek() != null){
13 System.out.print(queue.remove() + " ");
14 }
15 System.out.println();
16 }
17
18 public static void main(String[] args) {
19 Random rand = new Random(47);
20 PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>();
21 for(int i = 0;i < 10;i++){
22 priorityQueue.offer(rand.nextInt(i+10));
23 }
24 printQueue(priorityQueue);
25
26 PriorityQueue<Character> qc = new PriorityQueue<Character>();
27 for(char c: "whoareyou".toCharArray()){
28 qc.offer(c);
29 }
30 printQueue(qc);
31
32 PriorityQueue<String> str = new PriorityQueue<String>();
33 str.offer("数学");
34 str.offer("english");
35 str.offer("book");
36 str.offer("语文");
37 str.offer("政治");
38 printQueue(str);
39
40
41 }
42 }
运行结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAANUAAABFCAIAAABSRDSXAAAAA3NCSVQICAjb4U/gAAATBElEQVR4Xu1deUATxxdOgMiRgJxyBQMBuRFQ7kNEQSSKgApUKGLBCxSr1qverWi9qth6YLXWCy+kBRGRSxSRQ44qCIqKAQMCBiSQACHnbxMQNeym8VdrwOz8I5l9M++9b76d7Ox8GZFcLhcBFxgBMSEgM+i3kdQgphhgt5KLABKe/yR38IdB5lLDIAY4BMlFAOaf5I79cMgc5t9wGAXJjQHmn+SO/XDIHObfcBgFyY1hKP84lKJdhHHaOjo6ZoEHKqhC3g72lG7xMddEIZGaS4p6hEI4Qiypd1fY6Shj0Gi0mlnATwVvOJBJ9RQu1kRKo5V4RcNpV3UfpCW3p/r417Z6umPH6uhahx6r6oYGFBolLiUvGotEul4gQ7eGjGB4XwDev3xQekpX4XVCr71ms5ou+o0x21pFF7QY/MxovJtdXJUZra+3uLAb0op3YYRYMt/U1XcwgXj76s/P0jBa/3cvVFbd9xYZTIx/wbMVXtgt5z1G2/xc28fl9lb+YKI4NYnMgWoBhRKHcmeV2/TZeAWXxNeQjaE6Heb1gvNfX13aje5JUZNVGCzN6YvtWlMySUyoGwil6+blaKg6CgllMFg/QixlVPA4ZeCNPJfDYjDZTDrzE8w2SGlpRB+VBjCVQaVyVQ005SDhAkeJSy2M20havIOgIf2POI88A0H+McnPKEr6iHQ/Nc15+XIGih3PyJD8G3nZ/nPE9Ic/uukpyxstpyw7s2WCvJAGPRWbHbVV1PRdF52sokESVWpM4MljjuedVFWUVDxvEk7tccUI6XPoJS7t/s7vqsL2BGPf7VQNtRq5NYL842XCRapOO5qXc8ANg4DEdeRm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/GcbHidNDund2a6ibU3Rm++dehowI/R8tP9pj4e0+OWm2jHn0qVOxNhjRnvw+7k6ErcWAwIjgnxhwgV1+HgTAnv8+j2fYC4wArD+FOSBeBOD5T7z4S7p3mH+SzgDx5g/zT7z4S7p3mH+SzgDx5g/zT7z4S7p3mH+SzgDx5g/zT7z4S7p3mH+SzgDx5g/zT7z4S7p3mH+SzgDx5g/zT7z4S7p3mH+SzgDx5g/zT7z4S7p3mH+SzgDx5v8/WQj4LRnrPs0AAAAASUVORK5CYII=" alt="" />
从运行结果可以看出,PriorityQueue不仅可以自动对数字进行排序,还可以自动对字符串甚至汉字进行自动排序。
14、总结:
先看容器分类图:
点线为接口,实线为具体类,空心箭头指实现接口,实心箭头指某个类可以生成箭头所指向的类的对象。
1、数组可以存放对象,存放基本类型的数据,可以多维,就是容量不能改变。
2、Collection保存单一的元素,Map保存键值对。
3、List和数组类似,但是List可以自动扩充容量。大量随机访问使用ArrayList,经常进行插入删除操作,用LinkedList。
4、Map,HashMap用来快速访问,TreeMap保持键的排序,速度没HashMap快,LinkedHashMap保持元素排序,也通过散列也能快速访问,于两者中间。
5、Set元素不重复,TreeSet,HashSet,LinkedHashSet与Map的类似。
java 持有对象的更多相关文章
- java 持有对象总结
java提供了大量的持有对象的方式: 1)数组将数字和对象联系起来,它保存类型明确的对象,查询对象时,不需要对结果做类型转换,它可以时多维的,可以保存基本数据类型的数据,但是,数组一旦生成,其容量就不 ...
- Thinking In Java持有对象阅读记录
这里记录下一些之前不太了解的知识点,还有一些小细节吧 序 首先,为什么要有Containers来持有对象,直接用array不好吗?——数组是固定大小的,使用不方便,而且是只能持有一个类型的对象,但当你 ...
- java持有对象【2】ArrayList容器续解
此为JDK API1.6.0对ArrayList的解释. ArrayList 使用java泛型创建类很复杂,但是应用预定义的泛型很简单.例如,要想定义用来保存Apple对象的ArrayList,可以声 ...
- java持有对象【1】容器类及ArrayList
如果一个程序只包含固定数量的且其生命周期都是已知的对象,那么这是一个非常简单的程序. ----------java编程思想第十一章引言 java有许多方式引用对象,例如学过的数组,他是编译器支持的类 ...
- java持有对象-集合类
面阿里的时候,面试就让我说一下集合 当时由于条件原因没听清面试官的问题,后来面试后,面试官让问他问题的时候,才说明白是什么 下面就接受一下我了解的集合类 集合类中大致可以分为两个体系 一.collec ...
- java 持有对象 ListIterator用法
package ch07; import java.io.*; import java.util.Iterator; import java.util.LinkedList; import java. ...
- Java基础 -- 持有对象(容器)
一 容器的用途 如果对象的数量与生命周期都是固定的,自然我们也就不需要很复杂的数据结构. 我们可以通过创建引用来持有对象,如 Class clazz; 也可以通过数组来持有多个对象,如 Class[] ...
- 工作随笔—Java容器基础知识分享(持有对象)
1. 概述 通常,程序总是运行时才知道的根据某些条件去创建新对象.在此之前,不会知道所需对象的数量,甚至不知道确切的类型,为解决这个普遍的编程问题:需要在任意时刻和任意位置创建任意数量的对象,所以,就 ...
- 走进Java中的持有对象(容器类)之一 容器分类
Java容器可以说是增强程序员编程能力的基本工具,本系列将带您深入理解容器类. 容器的用途 如果对象的数量与生命周期都是固定的,自然我们也就不需要很复杂的数据结构. 我们可以通过创建引用来持有对象,如 ...
随机推荐
- ubuntu 修改root密码
重新安装过ubuntu 14.04后,切换到root权限,需要输入密码,输入n次都不对. 也没有记得设置过密码. 百度一下,得到方法如下,已经验证测试成功. 命令为: sudo passwd 提示输入 ...
- iwinfo 的使用
以前使用iwconfig来配置和获取wireless的信息,目前openwrt已经废弃这种方式,采用 iwinfo 和 iw 命令来进行替代. iwinfo的使用方法如下: root@hbg:/# i ...
- Java 相关注意事项小结
程序是一系列有序指令的集合: Java主要用于开发两类程序: 1)桌面应用程序2)Internet应用程序1,Java程序:三步走,编写--编译--运行:2,使用记事本开发:1)以.java为后缀名保 ...
- 使用freerdp远程连接Windows桌面
之前使用的是rdesktop,但是由于其不支持NLA认证,便不能登录公司的电脑.为此,现在使用freerdp——这是package的名字,实际的可执行程序是xfreerdp.使用如下的命令行即可实现远 ...
- 使用rdesktop远程连接Windows桌面
之前使用的是KDE下的krdc.该程序的Grab Keys功能存在bug,导致Alt+TAB大多数时候不能被捕捉,从而无法使用键盘切换窗口.不过,其全屏功能是正常的,在多显示器的情况下,全屏只在一个屏 ...
- hdu 1407 测试你是否和LTC水平一样高
Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上! 你的任务是: 计算方程x^2+y^2+z^2= num的一个正整数解. Inpu ...
- 不完善的css怦然心动,有待改进...
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- html5 读写sqlite数据库
var db = openDatabase('MyData','','My Database',102400); //首先它创建一个数据库表,里面有3个字段 db.transaction(functi ...
- MySQL DATE_FORMAT
MySQL DATE_FORMAT(date,format) 根据format字符串格式化date值 (在format字符串中可用标志符: %M 月名字(January……December) %W ...
- dom入门
当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). //显示,改变html内容 document.getElementById("p1" ...