Skip to content

Commit 36c0c37

Browse files
committed
Java第十六天
1 parent 4d1587e commit 36c0c37

File tree

84 files changed

+1478
-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.

84 files changed

+1478
-0
lines changed
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>day16_ArrayList_Vector_LinkedList</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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cn.itcast_01;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
6+
/*
7+
* List:
8+
* ArrayList:
9+
* 底层数据结构是数组,查询快,增删慢
10+
* 线程不安全,效率高
11+
* Vector:
12+
* 底层数据结构是数组,查询快,增删慢
13+
* 线程安全,效率低
14+
* LinkedList:
15+
* 底层数据结构是链表,查询慢,增删快
16+
* 线程不安全,效率高
17+
*
18+
* 面试题:ArrayList,Vector和LinkedList的各自特点?
19+
*
20+
* 思考题:ArrayList,Vector和LinkedList我们到底使用谁呢?
21+
* 看情况
22+
*
23+
* 要安全吗?
24+
* 要:Vector(这个现在也不常用,在Collections里面有新的方式)
25+
* 不要:ArrayList和LinkedList
26+
* 查询多:ArrayList
27+
* 增删多:LinkedList
28+
*
29+
* 不知道用哪个,就用ArrayList。
30+
*
31+
* 需求:用ArrayList存储字符串并遍历
32+
*/
33+
public class ArrayListDemo {
34+
public static void main(String[] args) {
35+
ArrayList array = new ArrayList();
36+
37+
array.add("hello");
38+
array.add("world");
39+
array.add("java");
40+
41+
// 迭代器
42+
Iterator it = array.iterator();
43+
while (it.hasNext()) {
44+
String s = (String) it.next();
45+
System.out.println(s);
46+
}
47+
System.out.println("--------------");
48+
49+
// 普通for
50+
for (int x = 0; x < array.size(); x++) {
51+
String s = (String) array.get(x);
52+
System.out.println(s);
53+
}
54+
55+
}
56+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cn.itcast_01;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
6+
/*
7+
* 需求:存储三个学生对象,并遍历。用两种方式遍历
8+
*
9+
* 练习:
10+
* Vector和LinkedList。
11+
* 存储字符串并遍历
12+
* 存储自定义对象并遍历
13+
*/
14+
public class ArrayListTest {
15+
public static void main(String[] args) {
16+
// 创建集合对象
17+
ArrayList array = new ArrayList();
18+
19+
// 创建元素对象
20+
Student s1 = new Student("朱元璋", 50);
21+
22+
Student s2 = new Student();
23+
s2.setName("李世民");
24+
s2.setAge(60);
25+
26+
Student s3 = new Student("秦始皇", 40);
27+
28+
// 把元素添加到集合
29+
array.add(s1);
30+
array.add(s2);
31+
array.add(s3);
32+
33+
// 遍历集合
34+
Iterator it = array.iterator();
35+
while (it.hasNext()) {
36+
Student s = (Student) it.next();
37+
System.out.println(s.getName() + "---" + s.getAge());
38+
}
39+
System.out.println("-----------------");
40+
41+
for (int x = 0; x < array.size(); x++) {
42+
Student s = (Student) array.get(x);
43+
System.out.println(s.getName() + "---" + s.getAge());
44+
}
45+
}
46+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.itcast_01;
2+
3+
public class Student {
4+
private String name;
5+
private int age;
6+
7+
public Student() {
8+
super();
9+
}
10+
11+
public Student(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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cn.itcast_02;
2+
3+
import java.util.Enumeration;
4+
import java.util.Vector;
5+
6+
/*
7+
* 特有功能:
8+
* A:添加元素
9+
* public void addElement(Object obj) -- add(Object obj)
10+
* B:获取元素
11+
* public Object elementAt(int index) -- get(int index)
12+
* public Enumeration elements() -- Iterator iterator()
13+
* hasMoreElements() hasNext()
14+
* nextElement() next()
15+
*
16+
* JDK版本升级:
17+
* A:安全
18+
* B:效率
19+
* C:简化书写
20+
*/
21+
public class VectorDemo {
22+
public static void main(String[] args) {
23+
// 创建集合对象
24+
// Collection c = new Vector();
25+
// List list = new Vector();
26+
27+
Vector v = new Vector();
28+
29+
// public void addElement(Object obj)
30+
v.addElement("hello");
31+
v.addElement("world");
32+
v.addElement("java");
33+
34+
// public Object elementAt(int index)
35+
// System.out.println(v.elementAt(0));
36+
// System.out.println(v.elementAt(1));
37+
// System.out.println(v.elementAt(2));
38+
// 普通for
39+
// for (int x = 0; x < v.size(); x++) {
40+
// String s = (String) v.elementAt(x);
41+
// System.out.println(s);
42+
// }
43+
44+
// 类似迭代器的方式
45+
Enumeration en = v.elements();
46+
while (en.hasMoreElements()) {
47+
String s = (String) en.nextElement();
48+
System.out.println(s);
49+
}
50+
51+
System.out.println("v:" + v);
52+
}
53+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package cn.itcast_03;
2+
3+
import java.util.LinkedList;
4+
5+
/*
6+
* LinkedList的特有功能:
7+
* A:添加功能
8+
* void addFirst()
9+
* void addLast()
10+
* B:移除功能
11+
* Object removeFirst()
12+
* Object removeLast()
13+
* C:获取功能
14+
* Object getFirst()
15+
* Object getLast()
16+
*/
17+
public class LinkedListDemo {
18+
public static void main(String[] args) {
19+
// 创建集合对象
20+
LinkedList link = new LinkedList();
21+
22+
// 添加元素
23+
link.add("hello");
24+
link.add("world");
25+
link.add("java");
26+
27+
// void addFirst()
28+
// void addLast()
29+
// link.addFirst("android");
30+
// link.addLast("javaee");
31+
32+
// Object removeFirst()
33+
// Object removeLast()
34+
// System.out.println("removeFirst:" + link.removeFirst());
35+
// System.out.println("removeLast:" + link.removeLast());
36+
37+
// Object getFirst()
38+
// Object getLast()
39+
System.out.println("getFirst:" + link.getFirst());
40+
System.out.println("getLast:" + link.getLast());
41+
42+
System.out.println("link:" + link);
43+
}
44+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cn.itcast_04;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
6+
/*
7+
* 去除集合中字符串的重复值(字符串的内容相同)
8+
* 举例:
9+
* hello,world,java,android,world,java,javaee,java,java,java,android
10+
* 结果:
11+
* hello,world,java,android,javaee
12+
*
13+
* 创建新集合的方式:
14+
* A:定义集合,存储带重复的元素
15+
* B:创建新集合
16+
* C:遍历旧集合,获取到旧集合中的每一个元素
17+
* D:那旧集合的每一个元素到新集合中去找,看有没有
18+
* 有:就不添加(不搭理它)
19+
* 木有:就添加
20+
* E:遍历新集合
21+
*/
22+
public class ArrayListTest {
23+
public static void main(String[] args) {
24+
// 定义集合,存储带重复的元素
25+
ArrayList array = new ArrayList();
26+
array.add("hello");
27+
array.add("world");
28+
array.add("java");
29+
array.add("android");
30+
array.add("world");
31+
array.add("java");
32+
array.add("javaee");
33+
array.add("java");
34+
array.add("java");
35+
array.add("java");
36+
array.add("android");
37+
38+
// 创建新集合
39+
ArrayList newArrayList = new ArrayList();
40+
41+
// 遍历旧集合,获取到旧集合中的每一个元素
42+
for (int x = 0; x < array.size(); x++) {
43+
String s = (String) array.get(x);
44+
// 那旧集合的每一个元素到新集合中去找,看有没有
45+
if (!newArrayList.contains(s)) {
46+
// 木有:就添加
47+
newArrayList.add(s);
48+
}
49+
}
50+
51+
// 遍历新集合
52+
Iterator it = newArrayList.iterator();
53+
while (it.hasNext()) {
54+
String s = (String) it.next();
55+
System.out.println(s);
56+
}
57+
}
58+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cn.itcast_04;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
6+
/*
7+
* 去除集合中字符串的重复值(字符串的内容相同)
8+
* 举例:
9+
* hello,world,java,android,world,java,javaee,java,java,java,android
10+
* 结果:
11+
* hello,world,java,android,javaee
12+
*
13+
* 和数组排序的选择排序思想一样。
14+
*/
15+
public class ArrayListTest2 {
16+
public static void main(String[] args) {
17+
// 定义集合,存储带重复的元素
18+
ArrayList array = new ArrayList();
19+
array.add("hello");
20+
array.add("world");
21+
array.add("java");
22+
array.add("android");
23+
array.add("world");
24+
array.add("java");
25+
array.add("javaee");
26+
array.add("java");
27+
array.add("java");
28+
array.add("java");
29+
array.add("android");
30+
31+
for (int x = 0; x < array.size() - 1; x++) {
32+
for (int y = x + 1; y < array.size(); y++) {
33+
if (array.get(y).equals(array.get(x))) {
34+
array.remove(y);
35+
y--; // 把元素删除后,还要和当前位置的比较一次
36+
}
37+
}
38+
}
39+
40+
// 遍历集合
41+
Iterator it = array.iterator();
42+
while (it.hasNext()) {
43+
String s = (String) it.next();
44+
System.out.println(s);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)