集合的有關例題
阿新 • • 發佈:2020-10-26
集合測試
Collection
Test_1 ScannerTest
/*** * 使用Scanner從鍵盤讀取一行輸入,去掉其中重複字元, 打印出不同的那些字元 */ public class ScannerTest { public static void main(String[] args) { //有序:HashSet<Character> hs = new LinkedHashSet<>(); HashSet<Character> hs = new HashSet<>(); Scanner sc = new Scanner(System.in); String line = sc.nextLine();//接受獲得的字串 char[] arr = line.toCharArray();//字串轉換成字元陣列 //把接收到的字串遍歷新增到集合 for (char c : arr) { hs.add(c); } //遍歷HashSet集合 for (Character h : hs) { System.out.print(h + "\t"); } System.out.println(); } } 輸出結果: aadddccccbbbbbggf a b c d f g
Test_2 ArrayListTest
/** * 需求: 將ArrayList集合中的重複元素去掉 */ public class ArrayListTest { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("aaa"); list.add("aaa"); list.add("abc"); list.add("bbc"); list.add("bbc"); list.add("bbc"); list.add("world"); list.add("world"); list.add("hello"); for (String s : list) { System.out.print(s + "\t"); } System.out.println(); getList(list); } private static void getList(ArrayList<String> list) { HashSet<String> hs = new LinkedHashSet<>();//無序: HashSet<>() hs.addAll(list); //for (String s : list) { // hs.add(s); //} list.clear(); list.addAll(hs); System.out.println(list); } } 輸出結果: aaa aaa abc bbc bbc bbc world world hello [aaa, abc, bbc, world, hello]
Test_3 RandomTest1
/** * 需求:編寫一個程式,獲取10個1至20的隨機數,要求隨機數不能重複。 * 並把最終的隨機數輸出到控制檯。 */ public class RandomTest { public static void main(String[] args) { HashSet<Integer> hs = new HashSet<>(); Random random = new Random(); int count = 0; while (hs.size() < 10) { //獲取隨機數 int num = random.nextInt(20) + 1;//隨機數上限 : 20 count++; hs.add(num); } System.out.println("一共獲得了" + count + "隨機數"); for (Integer i : hs) { System.out.print(i + "\t"); } System.out.println(); } } 輸出結果: 一共獲得了12隨機數 16 17 1 2 19 6 9 12 13 15
Test_4 RandomTest2
/**
* 用一個大集合存入20個隨機數字,然後篩選其中的偶數元素,放到小集合當中然後進行遍歷輸出
*/
public class RandomList {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
List<Integer> list1 = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add((int) (Math.random()*100));
}
for (Integer i : list) {
System.out.print(i + " ");
}
System.out.println();
for (Integer i : list) {
if (i % 2 == 0) {
list1.add(i);
}
}
for (Integer i : list1) {
System.out.print(i + " ");
}
System.out.println();
}
}
輸出結果:
12 34 85 14 28 58 16 45 40 27 74 88 58 58 41 39 0 3 80 10
12 34 14 28 58 16 40 74 88 58 58 0 80 10
Test_5 ReceiveTest
/***
* 程式啟動後, 可以從鍵盤輸入接收多個整數,
* 直到輸入quit時結束輸入. 把所有輸入的整數倒序排列列印
*/
public class TreeSetTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int num = o2 - o1;//倒敘排列二叉樹. 預設正序排列為從小到大
return num == 0 ? 1 : num;
}
});
while (true) {
String str = sc.nextLine();
if ("quit".equals(str)) break;
try {
int num = Integer.parseInt(str);//將數字字串轉換成數字
ts.add(num);
} catch (Exception e) {
System.out.println("error");
}
}
for (Integer t : ts) {
System.out.println(t);
}
}
}
輸出結果:
12
123
222
1
quit
222
123
12
1
Test_6 AddListTest
/***
* 需求:我有一個集合,請問,我想判斷裡面有沒有"world"這個元素
* 如果有,我就新增一個"javaee"元素,請寫程式碼實現
*/
public class ListTest {
public static void main(String[] args) {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("world");
list.add("d");
list.add("e");
// == 判斷是否指向同一個記憶體空間
// .equals 判斷所指向的記憶體空間的值是否相同
//使用iterator 一邊通過迭代器遍歷list, 一邊修改list,會出現併發異常
//Iterator it = list.iterator();
ListIterator it = list.listIterator();
while (it.hasNext()) {
String str = (String) it.next();
if ("world".equals(str)) {
it.add("javaee");
//list.add("javaee");
}
}
System.out.println(list);
/*
for (int i = 0; i < list.size(); i++) {
//Object --> String
String str = (String) list.get(i);
if ("world".equals(str)) {
list.add("javaee");
}
}
System.out.println(list);*/
/*
boolean f = list.contains("world");
if ("true".equals(f)) {
list.add("javaee");
}
System.out.println(list);*/
}
}
輸出結果:
[a, b, world, javaee, d, e]
Test_7 SortTest
/**
* 1-10個數字
* 奇數在後偶數在前
* 偶數降序,奇數升序
*/
public class TreeSetSort2 {
public static void main(String[] args) {
Set<Integer> set = new TreeSet<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 % 2 == 0) {
if (o2 % 2 == 0) {
return o2 - o1;
}else {
return -1;
}
}else {
if (o2 % 2 != 0) {
return o1 - o2;
}else {
return 1;
}
}
}
});
for (int i = 1; i <= 10; i++) {
set.add(i);
}
System.out.println(set);
}
}
輸出結果:
[10, 8, 6, 4, 2, 1, 3, 5, 7, 9]
Map
Test_1 CountTest1
/***
* 需求:統計字串中每個字元出現的次數 HashMap實現
*/
public class CountTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] arr = str.toCharArray(); //將輸入的字串轉化成字元陣列
HashMap<Character, Integer> map = new HashMap<>();
for (char c : arr) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
}else {
map.put(c, 1);
}
}
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
輸出結果:
aabbccddd123321
a : 2
1 : 2
b : 2
2 : 2
c : 2
3 : 2
d : 3
/**
* 統計每個字串出現的次數 TrreeMap實現
* String s = "aabbddccaefdd";
*/
public class TreeMapCount {
public static void main(String[] args) {
Map<Character, Integer> map = new TreeMap<>();
String s = "aabbddccaefdd";
char[] array = s.toCharArray();
for (char c : array) {
if (!map.containsKey(c)) {
map.put(c, 1);
}else {
map.put(c, map.get(c) + 1);
}
}
for (Character c : map.keySet()) {
System.out.println(c + " : " + map.get(c));
}
}
}
輸出結果:
a : 3
b : 2
c : 2
d : 4
e : 1
f : 1
Test_2 CountTest2
/***
* 在控制檯輸入一句英文:The hard part is not making the decision but it is living with it
* 每個單詞之間用空白隔開。
* 使用HashMap統計該句英文中每個單詞出現的次數
*/
public class TestWork1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//把輸入的字串統一轉成小寫單詞處理
String line = sc.nextLine().toLowerCase();
String[] arr = line.split(" ");
Map<String, Integer> map = new HashMap();
for (String s : arr) {
if (!map.containsKey(s)) {
map.put(s, 1);
}else {
map.put(s, map.get(s) + 1);
}
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
輸出結果:
The hard part is not making the decision but it is living with it
the: 2
but: 1
with: 1
not: 1
making: 1
living: 1
decision: 1
part: 1
is: 2
hard: 1
it: 2
Test_3 DouDiZhu
/**
* 鬥地主
*/
public class DouDiZhu {
public static void main(String[] args) {
String[] color = {"♠", "♥", "♣", "♦"};
String[] number = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
Map<Integer, String> map = new HashMap<>();
List<Integer> list = new ArrayList<>();
//把顏色和數字組合放進map集合中
int index = 0;
for (String s : color) {
for (String s1 : number) {
map.put(index, s.concat(s1));
list.add(index);
index++;
}
}
//index++;
//存大小王
map.put(index, "小王");
list.add(index);
index++;
map.put(index, "大王");
list.add(index);
//System.out.println(map);
System.out.println(map.size());
//洗牌
Collections.shuffle(list);
//System.out.println(list);
//發牌 將list集合隨機排列後放進三個玩家集合和一個底牌集合中 TreeSet在輸出時會自動按照自然數排序 【中序遍歷】
TreeSet<Integer> play1 = new TreeSet<>();
TreeSet<Integer> play2 = new TreeSet<>();
TreeSet<Integer> play3 = new TreeSet<>();
TreeSet<Integer> UnderCard = new TreeSet<>();
/*for (Integer i : list) {
System.out.print(i + " ");
}
System.out.println();*/
for (int i = 0; i < list.size(); i++) {
if (i >= list.size()-3) {
UnderCard.add(list.get(i));
}else if (i % 3 == 0) {
play1.add(list.get(i));
}else if (i % 3 == 1) {
play2.add(list.get(i));
}else
play3.add(list.get(i));
}
//玩家看牌
LookPoker(play1, map, "player1");
LookPoker(play2, map, "player2");
LookPoker(play3, map, "player3");
LookPoker(UnderCard, map, "UnderCard");
}
private static void LookPoker(TreeSet<Integer> player, Map<Integer, String> map, String playerName) {
System.out.print(playerName + ": ");
for (Integer i : player) {
System.out.print(map.get(i) + "\t");
}
System.out.println();
}
}
輸出結果[隨機性]:
54
♠3 ♣3 ♠4 ♠6 ♣6 ♠7 ♦7 ♥8 ♦8 ♥9 ♣10 ♥J ♦Q ♣A ♥2 ♦2 大王
♥3 ♠5 ♥5 ♦5 ♥6 ♦6 ♥7 ♠8 ♦10 ♠J ♣J ♠Q ♣Q ♠K ♥K ♠A ♦A
♦3 ♣4 ♦4 ♣5 ♣7 ♣8 ♦9 ♠10 ♥10 ♦J ♥Q ♣K ♦K ♥A ♠2 ♣2 小王
♥4 ♠9 ♣9