Skip to content

Commit 1d644cc

Browse files
committed
Java第十八天
1 parent 4bc7b98 commit 1d644cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1309
-0
lines changed
2.24 MB
Binary file not shown.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

day18/code/day18_Collections/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>day18_Collections</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eclipse.preferences.version=1
2+
encoding//src/cn/itcast_03/PokerDemo.java=UTF-8
3+
encoding//src/cn/itcast_04/PokerDemo.java=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cn.itcast_01;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
6+
/*
7+
* Collections:是针对集合进行操作的工具类。里面包含了排序和查找等方法。
8+
* 面试题?
9+
* Collections和Collection的区别?
10+
* Collections:是针对集合进行操作的工具类,包含了排序和查找等功能。
11+
* Collection:是单列集合的顶层接口,定义了单列集合的共性功能。
12+
*
13+
* 掌握的功能:
14+
* public static <T> void sort(List<T> list):排序
15+
* public static <T> int binarySearch(List<?> list,T key):二分查找
16+
* public static <T> T max(Collection<?> coll):最大值
17+
* public static void reverse(List<?> list):反转
18+
* public static void shuffle(List<?> list):随机置换
19+
*/
20+
public class CollectionsDemo {
21+
public static void main(String[] args) {
22+
// 创建集合对象
23+
ArrayList<Integer> array = new ArrayList<Integer>();
24+
25+
// 添加元素
26+
array.add(30);
27+
array.add(20);
28+
array.add(50);
29+
array.add(10);
30+
array.add(40);
31+
32+
System.out.println("array:" + array);
33+
34+
// public static <T> void sort(List<T> list):排序
35+
// Collections.sort(array);
36+
// System.out.println("array:" + array);
37+
// [10, 20, 30, 40, 50]
38+
39+
// public static <T> int binarySearch(List<?> list,T key):二分查找
40+
// int index = Collections.binarySearch(array, 30);
41+
// System.out.println(index);
42+
43+
// public static <T> T max(Collection<?> coll):最大值
44+
// Integer max = Collections.max(array);
45+
// System.out.println(max);
46+
47+
// public static void reverse(List<?> list):反转
48+
// Collections.reverse(array);
49+
// System.out.println("array:" + array);
50+
51+
// public static void shuffle(List<?> list):随机置换
52+
Collections.shuffle(array);
53+
System.out.println("array:" + array);
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cn.itcast_02;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
7+
/*
8+
* 用ArrayList存储自定义对象,并按照年龄进行排序
9+
*/
10+
public class ArrayListDemo {
11+
public static void main(String[] args) {
12+
// 创建集合对象
13+
ArrayList<Person> array = new ArrayList<Person>();
14+
15+
// 创建元素对象(蒋干盗书)
16+
Person p1 = new Person("曹操", 35);// 后知后觉
17+
Person p2 = new Person("蒋干", 33);// 不知不觉
18+
Person p3 = new Person("诸葛亮", 34); // 先知先觉
19+
20+
// 添加元素
21+
array.add(p1);
22+
array.add(p2);
23+
array.add(p3);
24+
25+
// 排序
26+
// Collections.sort(array);
27+
Collections.sort(array, new Comparator<Person>() {
28+
@Override
29+
public int compare(Person p1, Person p2) {
30+
return p2.getAge() - p1.getAge();
31+
}
32+
33+
});
34+
35+
// 遍历集合
36+
for (Person p : array) {
37+
System.out.println(p.getName() + "---" + p.getAge());
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cn.itcast_02;
2+
3+
public class Person implements Comparable<Person> {
4+
private String name;
5+
private int age;
6+
7+
public Person() {
8+
super();
9+
}
10+
11+
public Person(String name, int age) {
12+
super();
13+
this.name = name;
14+
this.age = age;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public int getAge() {
26+
return age;
27+
}
28+
29+
public void setAge(int age) {
30+
this.age = age;
31+
}
32+
33+
@Override
34+
public int compareTo(Person p) {
35+
return this.age - p.age;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package cn.itcast_03;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
6+
/*
7+
* 模拟斗地主洗牌和发牌
8+
*
9+
* 扑克牌:54
10+
* 小王
11+
* 大王
12+
* 黑桃A,黑桃2,黑桃3,黑桃4,黑桃...,黑桃10,黑桃J,黑桃Q,黑桃K
13+
* 红桃...
14+
* 梅花...
15+
* 方块...
16+
*
17+
* 分析:
18+
* A:造一个牌盒(集合)
19+
* B:造每一张牌,然后存储到牌盒里面去
20+
* C:洗牌
21+
* D:发牌
22+
* E:看牌
23+
*/
24+
public class PokerDemo {
25+
public static void main(String[] args) {
26+
// 造一个牌盒(集合)
27+
ArrayList<String> array = new ArrayList<String>();
28+
29+
// 造每一张牌,然后存储到牌盒里面去
30+
// 定义花色数组
31+
String[] colors = { "♠", "♥", "♣", "♦" };
32+
// 定义点数数组
33+
String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
34+
"J", "Q", "K" };
35+
for (String color : colors) {
36+
for (String number : numbers) {
37+
array.add(color.concat(number));
38+
}
39+
}
40+
array.add("小王");
41+
array.add("大王");
42+
43+
// 看牌
44+
// System.out.println(array);
45+
46+
// 洗牌
47+
Collections.shuffle(array);
48+
49+
// 发牌
50+
// 三个选手
51+
ArrayList<String> linQingXia = new ArrayList<String>();
52+
ArrayList<String> fengQingYang = new ArrayList<String>();
53+
ArrayList<String> liuYi = new ArrayList<String>();
54+
// 底牌
55+
ArrayList<String> diPai = new ArrayList<String>();
56+
57+
for (int x = 0; x < array.size(); x++) {
58+
if (x >= array.size() - 3) {
59+
diPai.add(array.get(x));
60+
} else if (x % 3 == 0) {
61+
linQingXia.add(array.get(x));
62+
} else if (x % 3 == 1) {
63+
fengQingYang.add(array.get(x));
64+
} else if (x % 3 == 2) {
65+
liuYi.add(array.get(x));
66+
}
67+
}
68+
69+
// 看牌
70+
lookPoker("林青霞", linQingXia);
71+
lookPoker("风清扬", fengQingYang);
72+
lookPoker("刘意", liuYi);
73+
lookPoker("底牌", diPai);
74+
75+
}
76+
77+
// 写一个功能实现遍历
78+
public static void lookPoker(String name, ArrayList<String> array) {
79+
System.out.print(name + "的牌是:");
80+
for (String s : array) {
81+
System.out.print(s + " ");
82+
}
83+
System.out.println();
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package cn.itcast_04;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.TreeSet;
7+
8+
/*
9+
* A:创建一个HashMap集合
10+
* B:创建一个ArrayList集合
11+
* C:装牌
12+
* D:洗牌
13+
* E:发牌
14+
* F:看牌
15+
*/
16+
public class PokerDemo {
17+
public static void main(String[] args) {
18+
// 创建一个HashMap集合
19+
HashMap<Integer, String> hm = new HashMap<Integer, String>();
20+
21+
// 创建一个ArrayList集合
22+
ArrayList<Integer> array = new ArrayList<Integer>();
23+
24+
// 存储牌
25+
// 定义花色数组
26+
String[] colors = { "♠", "♥", "♣", "♦" };
27+
// 定义点数数组
28+
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
29+
"K", "A", "2" };
30+
31+
// 定义一个索引
32+
int key = 0;
33+
34+
for (String number : numbers) {
35+
for (String color : colors) {
36+
String value = color.concat(number);
37+
hm.put(key, value);
38+
array.add(key);
39+
key++;
40+
}
41+
}
42+
43+
hm.put(key, "小王");
44+
array.add(key);
45+
key++;
46+
hm.put(key, "大王");
47+
array.add(key);
48+
49+
// 洗牌
50+
Collections.shuffle(array);
51+
52+
// 发牌
53+
TreeSet<Integer> caoCao = new TreeSet<Integer>();
54+
TreeSet<Integer> liuBei = new TreeSet<Integer>();
55+
TreeSet<Integer> sunQuan = new TreeSet<Integer>();
56+
TreeSet<Integer> diPai = new TreeSet<Integer>();
57+
58+
for (int x = 0; x < array.size(); x++) {
59+
if (x >= array.size() - 3) {
60+
diPai.add(array.get(x));
61+
} else if (x % 3 == 0) {
62+
caoCao.add(array.get(x));
63+
} else if (x % 3 == 1) {
64+
liuBei.add(array.get(x));
65+
} else if (x % 3 == 2) {
66+
sunQuan.add(array.get(x));
67+
}
68+
}
69+
70+
// 看牌
71+
lookPoker("曹操", caoCao, hm);
72+
lookPoker("刘备", liuBei, hm);
73+
lookPoker("孙权", sunQuan, hm);
74+
lookPoker("底牌", diPai, hm);
75+
}
76+
77+
// 写一个功能实现遍历
78+
public static void lookPoker(String name, TreeSet<Integer> ts,
79+
HashMap<Integer, String> hm) {
80+
System.out.print(name + "的牌是:");
81+
for (Integer key : ts) {
82+
String value = hm.get(key);
83+
System.out.print(value + " ");
84+
}
85+
System.out.println();
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>day18_Map_HashMap_TreeMap</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)