Skip to content

Commit 4bc7b98

Browse files
committed
Java第十七天
1 parent 36c0c37 commit 4bc7b98

File tree

66 files changed

+1556
-0
lines changed

Some content is hidden

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

66 files changed

+1556
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
1:Collection集合体系结构
2+
Collection
3+
|--List(元素有序,可重复)
4+
|--ArrayList
5+
底层数据结构是数组,查询快,增删慢
6+
线程不安全,效率高
7+
|--Vector
8+
底层数据结构是数组,查询快,增删慢
9+
线程安全,效率低
10+
|--LinkedList
11+
底层数据结构是链表,查询慢,增删快
12+
线程不安全,效率高
13+
|--Set(元素无序,唯一)
14+
|--HashSet
15+
底层数据结构是哈希表。
16+
保证元素的唯一性?
17+
依赖两个方法hashCode()和equals()。
18+
|--LinkedHashSet
19+
底层数据结构是哈希表和链表
20+
由哈希表保证元素唯一
21+
由链表保证元素有序
22+
|--TreeSet
23+
底层数据结构是二叉树(红黑树)
24+
保证元素的唯一性?
25+
根据比较的返回值是否是0
26+
保证元素的排序?
27+
自然排序
28+
比较器排序
29+
30+
2:针对Collection,你准备使用谁?
31+
元素唯一吗?
32+
是:Set
33+
要排序吗?
34+
是:TreeSet
35+
否:HashSet
36+
37+
不知道,就用HashSet
38+
否:List
39+
要安全吗?
40+
要:Vector(其实也不用,有更好的方式,后面讲)
41+
不要:ArrayList或者LinkedList
42+
查询多:ArrayList
43+
增删多:LinkedList
44+
45+
不知道,就用ArrayList
46+
47+
不知道,就用ArrayList
48+
49+
3:集合中的数据结构问题
50+
ArrayXxx:底层数据结构是数组,查询快,增删慢
51+
LinkedXxx:底层数据结构是链表,查询慢,增删快
52+
HashXxx:底层数据结构是哈希表。依赖两个方法hashCode()和equals(),根据情况选择是否自动重写
53+
TreeXxx:底层数据结构是二叉树。有两种方案:自然排序,比较器排序。
2.24 MB
Binary file not shown.
Binary file not shown.
Lines changed: 6 additions & 0 deletions
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>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>day17_List_test</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>
Lines changed: 11 additions & 0 deletions
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.
873 Bytes
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cn.itcast_01;
2+
3+
import java.util.ArrayList;
4+
5+
/*
6+
* 需求:获取10个1-20之间的随机数,要求不能重复
7+
*
8+
* 分析:
9+
* A:定义一个集合,用于存储产生的随机数
10+
* B:定义一个统计变量,初始值是0
11+
* C:判断统计变量是否小于10
12+
* 是:
13+
* 产生一个随机,然后判断是在集合中
14+
* 是:不添加
15+
* 否:添加到集合
16+
* 否:结束
17+
* D:遍历集合
18+
*/
19+
public class RandomNumberDemo {
20+
public static void main(String[] args) {
21+
// 定义一个集合,用于存储产生的随机数
22+
ArrayList<Integer> array = new ArrayList<Integer>();
23+
24+
// 定义一个统计变量,初始值是0
25+
int count = 0;
26+
27+
// 判断统计遍历是否小于10
28+
while (count < 10) {
29+
// 产生一个随机,然后判断是在集合中
30+
int randomNumber = (int) (Math.random() * 20) + 1;
31+
32+
if (!array.contains(randomNumber)) {
33+
array.add(randomNumber);
34+
count++;
35+
}
36+
}
37+
38+
// 遍历集合
39+
for (Integer i : array) {
40+
System.out.println(i);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)