集合框架

集合框架包含的內容:

集合框架的接口:

List接口實現類

ArrayList

 1 package com.jredu.ch01;
 3 import java.util.ArrayList;
 5 import java.util.List;
 7 public class ArrayListTest {
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         // 創建一個list集合對象
12         // list是有序但不唯一的一組集合數據
13         List list = new ArrayList<>();
14         // 開始的集合長度為0
15         // list.size() 返回給定集合的長度
16         System.out.println("添加數據前的集合長度" + list.size());
17         // list.add("aaaa");
18         // list.add("dddd");
19         // list.add("cccc");
20         // list.add("aaaa");
21         list.add(1);
22         list.add("two");
23         list.add('c'); // 字符型
24         list.add(true);
25         // list.add(index, element);
26         // 在指定索引處添加了一個元素,但索引位置必須介于0——集合的元素個數之間
27         list.add(1, "啦啦啦");
28         System.out.println(list);
29         System.out.println("添加數據后的集合長度" + list.size());
30         // get方法通過傳入指定元素的索引獲取該元素,取出來的類型Object類型
31         // 使用前需要進行類型強制轉換
32         // 索引范圍內是0——集合長度減一 范圍
33         System.out.println(list.get(3));// 因為add添加,要+1得到c
34     }
35 }
 1 package com.jredu.ch01;
 3 import java.util.ArrayList;
 5 public class ArrayListTest2 {
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         ArrayList list = new ArrayList<>();
10         list.add(1);
11         list.add(2);
12         list.add(3);
13         list.add(2);
14         // contains判斷列表中是否存在該元素
15         if (list.contains(3)) {
16             System.out.println("找到了");
17         } else {
18             System.out.println("未找到");
19         }
20         // remove在列表中刪除指定元素,刪除成功返回true,失敗返回false
21         // 如果有多個元素值相同,只刪掉第一個
22         if (list.remove(Integer.valueOf(2))) { // 包裝類
23             System.out.println("刪除成功");
24         } else {
25             System.out.println("刪除失敗");
26         }
27         System.out.println(list);
28         // 刪除的是對應下標的那個元素的值
29         // list.remove(1);
30         System.out.println(list.remove(2));
32     }
34 }
 1 package com.jredu.ch03;
 3 import java.util.ArrayList;
 4 import java.util.Arrays;
 6 /**
 7  * list擴展:常見的幾個擴展方法11  */
12 public class ListDemo {
13     public static void main(String[] args) {
14         ArrayList list = new ArrayList<>();
15         ArrayList list2 = new ArrayList<>();
16         ArrayList list3 = new ArrayList<>();
17         list.add(1);
18         list.add(2);
19         list.add(3);
20         list2.add(1);
21         list2.add(2);
22         list2.add(3);
23         // 向集合中添加一個集合
24         list3.addAll(list);
25         list3.addAll(list2);
26         // System.out.println(list);
27         // System.out.println(list2);
28         // System.out.println(list3);
30         ArrayList list4 = new ArrayList<>();// ArrayList list4 =null;
31         list4.add(1);
32         // 判斷集合中是否有元素
33         if (list4 != null && !list4.isEmpty()) {
34             System.out.println("集合中有數據");
35         } else {
36             System.out.println("集合為空");
37         }
39         ArrayList list5 = new ArrayList<>();
40         // Object [] o={1,2,3};
41         list5.add(1);
42         list5.add(2);
43         list5.add(3);
44         // 數組和集合相互轉換
45         // 集合轉數組
46         Object[] o = list5.toArray();
47         System.out.println(Arrays.toString(o));
48         // 數組轉集合
49         ArrayList list6 = new ArrayList<>();
50         list6.addAll(Arrays.asList(o));
51         System.out.println(list6);
53         // for(int i=0;i<list.size();i++){
54         // list2.add(list.get(i));
55         // }
56         // System.out.println(list2);
57     }
58 }

LinkedList

 1 package com.jredu.ch01;
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Random;
 6 import java.util.Scanner;
 8 public class Random1 {
10     //存儲所有的姓名
11     List list=new ArrayList<>();
12     //接收控制臺信息
13     Scanner in=new Scanner(System.in);
14     //隨機數
15     Random r=new Random();
17     public void addNames() {
18         //循環添加姓名
19         while(true) {
20             //提示信息
21             System.out.print("請輸入姓名");
22             //接收姓名
23             list.add(in.next());
24             //提示信息
25             System.out.print("是否繼續添加?(y/n)");
26             String code=in.next();
27             if(!code.equals("y")) {
28                 break;
29             }
30         }
31         System.out.println(list);
32     }   
34     public void getRandomName() {
35         while(true){
36             //得到隨機數
37             int i=r.nextInt(list.size());
38             //得到對應的名字
39             System.out.println(list.get(i));
40             System.out.print("是否繼續點名?(y/n)");
41             String code=in.next();
42             //判斷如果不是y,就退出
43             if(!code.equals("y")) {
44                 return;
45             }
46         }
47     }
49     public static void main(String[] args) {
50         Random1 test=new Random1();
51         //添加姓名
52         test.addNames();
53         //獲得隨機的姓名
54         test.getRandomName();
55     } 
58 }
 1 package com.jredu.ch02;
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Scanner;
 7 public class StudentManager {
 9     ArrayList list = new ArrayList<>();
10     Scanner sc = new Scanner(System.in);
12     public void addStu() {
13         System.out.println("**********錄入學生信息,當錄入學生的編號為0時結束錄入************");
14         while (true) {
15             System.out.print("請輸入學員學號:");
16             int num = sc.nextInt();
17             Student stu = null; 
18             if (num == 0) {
19                 System.out.println("結束錄入!");
20                 show();
21                 break;
22             } else {
23                 System.out.print("請輸入學員姓名:");
24                 String name = sc.next();
25                 System.out.print("請輸入學員年齡:");
26                 int age = sc.nextInt();
27                 stu = new Student(num, name, age);
28                 list.add(stu);
29             }
30         }
32     }
34     public void show() {
35         System.out.println("學員的信息如下:");
36         System.out.println("學號\t姓名\t年齡");
37         // Student s = null;
38         // for (int i = 0; i < list.size(); i++) {
39         // s = (Student) list.get(i);
40         // System.out.println(s.getId() + "\t" + s.getName() + "\t" +
41         // s.getAge());
42         // }
43         for (Object o : list) {
44             Student stu = (Student) o;
45             System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge());
46         }
48     }
50     public void update() {
51         System.out.print("請輸入需要修改的學員學號:");
52         int num = sc.nextInt();
53         for (int i = 0; i < list.size(); i++) {
54             Student stu = (Student) list.get(i);
55             if (stu.getId() == num) {
56                 System.out.print("請輸入需要修改的學員姓名:");
57                 stu.setName(sc.next());
58                 System.out.print("請輸入需要修改的學員年齡:");
59                 stu.setAge(sc.nextInt());
60                 System.out.println("修改成功");
61                 // 展示數據
62                 show();
63                 return;// 跳出方法
64             }
65         }
66         System.out.println("沒有該學員");
67     }
69     public void remove() {
70         System.out.print("請輸入要刪除的編號");
71         int no = sc.nextInt();
72         for (int i = 0; i < list.size(); i++) {
73             if (no == ((Student) list.get(i)).getId()) {
74                 list.remove(i);
75                 System.out.println("刪除成功");
76                 show();
77                 return;
78             }
79         }
80         System.out.println("對不起,沒有此學員");
81     }
83     public static void main(String[] args) {
84         StudentManager sm = new StudentManager();
85         sm.addStu();
86         sm.update();
87         sm.remove();
88     }
90 }

迭代器Iterator

如何遍歷List集合?

1、通過for循環和get()方法配合實現遍歷

2、通過迭代器Iterator實現遍歷

所有集合接口和類都沒有提供相應遍歷的方法,而是由Iterator實現集合遍歷

Collection接口的iterate()方法返回一個Iterator,然后通過Iterator接口的兩個方法可實現遍歷

boolean hasNext():判斷是否存在另一個可訪問的元素

Object next()返回要訪問的下一個元素

 1 package com.jredu.ch03;
 3 import java.util.Iterator;
 4 import java.util.LinkedList;
 6 public class IteratorTest {
 7     public static void main(String[] args) {
 8         LinkedList list=new LinkedList<>();
 9         list.add(1);
10         list.add(2);
11         list.add(3);
12         //獲取集合中的每一個元素
13         //使用迭代
14         Iterator iter=list.iterator();
15         //判斷是否存在下一個元素
16         while(iter.hasNext()){
17             //獲取下一個元素
18             System.out.print(iter.next()+" ");        
19         }
20         System.out.println("\n***********");    
21         //for循環
22         for(int i=0;i<list.size();i++){
23             System.out.print(list.get(i)+" ");
24         }
25         System.out.println("\n***********");
26         //foreach循環
27         for(Object o:list){
28             System.out.print(o+" ");
29         }
30 //        for(int i=0;i<5;i++){
31 //            list.add(new Student(i,"name"+i,i));
32 //        }
33 //        Iterator iter=list.iterator();
34 //        while(iter.hasNext()){
35 //            Student stu=(Student)iter.next();
36 //            System.out.println(stu.getId());
37 //            System.out.println(stu.getName());
38 //            System.out.println(stu.getAge());            
39 //        }    
40     }
41 }

HashSet

 1 package com.jredu.ch03;
 2 import java.util.HashSet;
 3 import java.util.Iterator;
 4 public class SetTest {
 5     public static void main(String[] args) {
 6         HashSet hs=new HashSet<>();
 7         //唯一
 8 //        hs.add(1);
 9 //        hs.add(2);
10 //        hs.add(1);
11 //        hs.add(true);
12 //        hs.add(true);
13 //        Dog dog=new Dog("陳", "單身貓");
14 //        Dog dog2=new Dog("周", "單身貓");
15 //        hs.add(dog2);
16 //        hs.add(dog);
17 //        System.out.println(hs.size());
18         //無序
19         hs.add(8);
20         hs.add(5);
21         hs.add(3);
22         hs.add(1);
23         Iterator iter=hs.iterator();
24         while(iter.hasNext()){
25             int i=(int)iter.next();
26             System.out.println(i);
27         } 
29     }
30 }

HashMap

 1 package com.jredu.ch04;
 3 import java.util.Collection;
 4 import java.util.HashMap;
 5 import java.util.Iterator;
 6 import java.util.Set;
 7 import java.util.Map.Entry;
 9 public class Ch01 {
11     public static void main(String[] args) {
12         HashMap map = new HashMap<>();
13         map.put(1, "哈哈");
14         map.put(2, "吼吼");
15         map.put(3, "嗚嗚");
16         // keySet獲得鍵集合
17         Set set = map.keySet();
18         Iterator iter = set.iterator();
19         while (iter.hasNext()) {
20             int i = (int) iter.next();
21             System.out.println(i);
22         }
23         // values獲得值集合
24         Collection col = map.values();
25         Iterator iter1 = col.iterator();
26         while (iter1.hasNext()) {
27             String str = (String) iter1.next();
28             System.out.println(str);
29         }
30         // entrySet獲得鍵和值的集合
31         Set entrySet = map.entrySet();
32         Iterator iter2 = entrySet.iterator();
33         while (iter2.hasNext()) {
34             Entry entry = (Entry) iter2.next();
36             System.out.println(entry.getKey());
37             System.out.println(entry.getValue());
38         }
39     }
41 }

?

package com.jredu.ch04;
public class Goods {  private String name;private double price;private int count;  public Goods(String name, double price, int count) {super();this.name = name;this.price = price;this.count = count;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}@Overridepublic String toString() {return "Goods [name=" + name + ", price=" + price + ", count=" + count + "]";}
}package com.jredu.ch04;
import java.util.Comparator;
/*** 數量比較*/
public class CountComparator implements Comparator{@Overridepublic int compare(Object o1, Object o2) {// TODO Auto-generated method stubGoods g1=(Goods) o1;Goods g2=(Goods) o2;//按照數量規則進行比較判斷if(g1.getCount()>=g2.getCount()) {//滿足規則返回1return 1;}return -1;}
}package com.jredu.ch04;
import java.util.ArrayList;
import java.util.Collections;
public class GoodsManager {ArrayList list=new ArrayList<>();public static final int PRICE=1;public static final int COUNT=2;public GoodsManager() {list.add(new Goods("手機", 999, 2));list.add(new Goods("電視", 4000, 5));list.add(new Goods("筆記本電腦", 6000, 10));list.add(new Goods("鼠標", 50, 30));list.add(new Goods("鍵盤", 40, 7));}    public void sort(int type) {//價格排序/數量排序switch (type) {case PRICE:Collections.sort(list,new PriceComparator());
//            Collections.reverse(list);
            show();break;case COUNT:
//            Collections.sort(list,new CountComparator());
//            Collections.reverse(list);
            show();break;}}    public void show() {System.out.println(list);}public static void main(String[] args) {GoodsManager gm=new GoodsManager();gm.sort(GoodsManager.PRICE);
//        gm.sort(GoodsManager.COUNT);
    }
}

?

轉載于:https://www.cnblogs.com/a5513633/p/6719357.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/371045.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/371045.shtml
英文地址,請注明出處:http://en.pswp.cn/news/371045.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

使用CSS實現無滾動條滾動

我們都知道&#xff0c;擼頁面的時候當我們的內容超出了我們的div&#xff0c;往往會出現滾動條&#xff0c;影響美觀。 尤其是當我們在做一些導航菜單的時候。滾動條一出現就破壞了UI效果。 我們不希望出現滾動條&#xff0c;也不希望超出去的內容被放逐&#xff0c;就要保留…

Java中的類型安全的空集合

我之前曾在Java Collections類的實用程序上進行過博客撰寫&#xff0c;并且特別地在使用Usings Collections Methods上的博客emptyList&#xff08;&#xff09;&#xff0c;emptyMap&#xff08;&#xff09;和emptySet&#xff08;&#xff09;上進行了博客撰寫。 在本文中&a…

php cpu mac,PHP 獲得計算機的唯一標識[CPU,網卡 MAC地址]

//獲取電腦的CPU信息function OnlyU(){$a ;$b array();if(function_exists(exec)){if(mailto:!exec( /all",$b)){return false;}}elseif(function_exists(system)){ob_start();if(mailto:!system( /all")){return false;}else{}$b ob_get_contents();ob_end_clean…

劍指offer二十二之從上往下打印二叉樹

一、題目 從上往下打印出二叉樹的每個節點&#xff0c;同層節點從左至右打印。 二、思路 二叉樹的層次遍歷&#xff0c;可以借助隊列實現。具體思路詳見注釋。 三、代碼 import java.util.ArrayList; import java.util.LinkedList; /** public class TreeNode {int val 0;Tree…

arduino i2c 如何寫16位寄存器_arduino入門

硬件&#xff1a;Arduino Uno是基于ATmega328P(數據表)的微控制器板。它具有14個數字輸入/輸出引腳(其中6個可用作PWM輸出)&#xff0c;6個模擬輸入&#xff0c;工作電壓5v&#xff0c;輸入電壓7-12v。串行&#xff1a;0(RX)和1(TX)用于接收(RX)和發送(TX)TTL串行數據。這些引腳…

原生JS實現banner圖的滾動與跳轉

HTML部分&#xff1a; <div id"banner"><!--4張滾動的圖片--><div id"inside"><img src"../../img/14072415363339_0.jpg"><img src"../../img/14072415383924_0.jpg" id"img2" /><img sr…

Java中的緊湊堆外結構/組合

在上一篇文章中&#xff0c;我詳細介紹了代碼對主內存的訪問方式的含義。 從那時起&#xff0c;我對使用Java可以做什么以實現更可預測的內存布局有很多疑問。 有些模式可以使用數組支持的結構來應用&#xff0c;我將在另一篇文章中討論。 這篇文章將探討如何模擬Java中非常缺少…

java字符集編碼是,java字符集與編碼有關問題

java字符集與編碼問題沒想到自己的第一篇javaeye博客就是讓人頭痛的java字符集轉碼問題&#xff0c;下面是我個人的一些認識與網上收集的代碼。在java中String在JVM里是unicode的&#xff0c;任何byte[]到String以及String到byte[]都涉及到字符集編碼轉換。基本規則是&#xff…

mysql序列號生成_一文看懂mycat的6種全局序列號實現方式

概述在實現分庫分表的情況下&#xff0c;數據庫自增主鍵已無法保證自增主鍵的全局唯一。為此&#xff0c;MyCat 提供了全局sequence&#xff0c;并且提供了包含本地配置和數據庫配置等多種實現方式。下面對這幾種實現方式做一下介紹。1、本地文件方式原理&#xff1a;此方式 My…

android.graphics.Paint方法setXfermode (Xfermode x...

[java] view plaincopymPaint new Paint(); mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN)); 常見的Xfermode&#xff08;SRC為原圖&#xff0c;DST為目標圖&#xff09;&#xff0c;把代碼中的SRC_IN換成下圖指定的模式就會出現對應的效果圖…

從零開始的全棧工程師——html篇1

全棧工程師也可以叫web 前端 H5主要是網站 app 小程序 公眾號這一塊 HTML篇 html(超文本標記語言&#xff0c;標記通用標記語言下的一個應用。) “超文本”就是指頁面內可以包含圖片、鏈接&#xff0c;甚至音樂、程序等非文字元素。 超文本標記語言的結構包括“頭”部分&am…

二分+樹的直徑 [Sdoi2011]消防

問題 D: [Sdoi2011]消防 時間限制: 1 Sec 內存限制: 512 MB 提交: 12 解決: 6 [提交][狀態][討論版] 題目描述 某個國家有n個城市&#xff0c;這n個城市中任意兩個都連通且有唯一一條路徑&#xff0c;每條連通兩個城市的道路的長度為zi(zi<1000)。 這個國家的人對火焰…

使用MRUnit測試Hadoop程序

這篇文章將略微繞開使用MapReduce實現數據密集型處理中的模式&#xff0c;以討論同樣重要的測試。 湯姆?惠勒 &#xff08; Tom Wheeler&#xff09;在紐約2012年Strata / Hadoop World會議上參加的一次演講給了我部分啟發。 處理大型數據集時&#xff0c;想到的并不是單元測試…

android之 TextWatcher的監聽

以前用過android.text.TextWatcher來監聽文本發生變化&#xff0c;但沒有仔細去想它&#xff0c;今天興致來了就發個瘋來玩玩吧&#xff01; 有點擔心自己理解錯&#xff0c;所以還是先把英文API解釋給大家看看 1、什么情況下使用了&#xff1f; When an object of a type is a…

php 秒殺并發怎么做,PHP實現高并發下的秒殺功能–Laravel

namespace App\Http\Controllers\SecKill;use App\Http\Controllers\Controller;use Exception;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Redis;class SecKillController extends Controller{/*** 往redis的隊列中添加庫存(用於測試的數據)**/public…

蘋果mp3軟件_優秀的Apple音樂轉換器,將任何iTunes M4P,AAX,AA轉換為MP3

Macsome iTunes Converter是一款優秀的音頻轉換工具&#xff0c;這款音頻轉換軟件能夠幫助大家快速進行音頻格式轉換&#xff0c;使得您可以自由的播放和分享自己喜愛的音頻文件。同時這款軟件與大多數音頻轉換軟件一樣&#xff0c;將受到保護DRM的Apple音樂轉換轉換成MP3, AAC…

Vuejs開發環境搭建及熱更新

一、安裝NPM 1.1最新穩定版本&#xff1a; npm install vue 二、命令行工具安裝 國內速度慢&#xff0c;使用淘寶鏡像&#xff1a; npm install -g cnpm --registryhttps://registry.npm.taobao.org 注意&#xff1a;以后使用npm的地方就替換成cnpm 1、全局安裝vue-vli ? …

線索二叉樹的C語言實現

#include "string.h"#include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h" #define OK 1#define ERROR 0#define TRUE 1#define FALSE 0 #define MAXSIZE 100 /* 存儲空…

發送帶有接縫的活動邀請

這些天來&#xff0c;我的一位同事在使用帶有接縫&#xff08;2.x版&#xff09;的郵件模板發送事件邀請時遇到了問題。 從根本上講&#xff0c;這不是一個艱巨的任務&#xff0c;因此我將簡要說明使用接縫郵件模板發送事件邀請需要做什么。 發送郵件邀請時&#xff0c;您需要發…

Oracle內存管理(之二)

Oracle內存管理&#xff08;之二&#xff09; 【深入解析--eygle】 學習筆記 1.2.2 UGA和CGA UGA&#xff08;用戶全局區&#xff09;由用戶會話數據、游標狀態和索引區組成。在共享server模式下&#xff0c;一個共享服務進程被多個用戶進程共享&#xff0c;此時UGA是Shared Po…