forked from DuGuQiuBai/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9408e48
commit 4d1587e
Showing
57 changed files
with
1,141 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>day15_Collection</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
11 changes: 11 additions & 0 deletions
11
day15/code/day15_Collection/.settings/org.eclipse.jdt.core.prefs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.7 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
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 added
BIN
+1016 Bytes
day15/code/day15_Collection/bin/cn/itcast_02/CollectionDemo3.class
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
public interface Iterator { | ||
boolean hasNext(); | ||
Object next(); | ||
} | ||
|
||
public interface Iterable { | ||
Iterator iterator(); | ||
} | ||
|
||
public interface Collection extends Iterable { | ||
|
||
} | ||
|
||
public interface List extends Collection { | ||
|
||
} | ||
|
||
public class ArrayList implements List { | ||
public Iterator iterator() { | ||
return new Itr(); | ||
} | ||
|
||
private class Itr implements Iterator { | ||
public boolean hasNext() {...} | ||
|
||
public Object next(){...} | ||
} | ||
} | ||
|
||
Collection c = new ArrayList(); | ||
|
||
Iterator it = c.iterator(); //new Itr() | ||
|
||
while(it.hasNext()) { | ||
it.next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cn.itcast_01; | ||
|
||
public class Student { | ||
private String name; | ||
private int age; | ||
|
||
public Student() { | ||
super(); | ||
} | ||
|
||
public Student(String name, int age) { | ||
super(); | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Student [name=" + name + ", age=" + age + "]"; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
day15/code/day15_Collection/src/cn/itcast_01/StudentDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cn.itcast_01; | ||
|
||
/* | ||
* 需求:请定义一个数组,存储三个学生对象,然后遍历该数组,得到每一个学生对象的成员变量值。 | ||
* | ||
* Student[] 对象数组。 | ||
*/ | ||
public class StudentDemo { | ||
public static void main(String[] args) { | ||
// 创建对象数组 | ||
Student[] students = new Student[3]; | ||
|
||
// 默认值是null | ||
for (int x = 0; x < students.length; x++) { | ||
System.out.println(students[x]); | ||
} | ||
|
||
// 创建学生对象元素 | ||
Student s1 = new Student("林青霞", 28); | ||
Student s2 = new Student("风清扬", 31); | ||
Student s3 = new Student("林志玲", 18); | ||
|
||
// 把元素放到数组中 | ||
students[0] = s1; | ||
students[1] = s2; | ||
students[2] = s3; | ||
|
||
// 赋值后的学生对象 | ||
for (int x = 0; x < students.length; x++) { | ||
// System.out.println(students[x]); | ||
Student s = students[x]; | ||
System.out.println(s.getName() + "---" + s.getAge()); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
day15/code/day15_Collection/src/cn/itcast_02/CollectionDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package cn.itcast_02; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
/* | ||
* 为什么出现集合类? | ||
* 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,用前面所学知识,我们只能采用对象数组实现。 | ||
* 但是,对象数组的长度又是固定的,适应不了变化的需求,所以,java就提供了集合类供我们使用。 | ||
* | ||
* 集合类的特点: | ||
* A:长度可变 | ||
* B:只能存储对象 | ||
* C:可以存储不同类型的对象 | ||
* | ||
* 面试题:数组和集合的区别? | ||
* A:集合长度可变;集合只能存储引用数据类型;集合可以存储不同数据类型的元素 | ||
* B:数组长度固定;数组可以存储引用数据类型,也可以存储基本数据类型;数组存储的是同一种数据类型的元素 | ||
* | ||
* 由于我们的功能需求不太一样,所以,java就提供了多种集合类供我们使用。这些集合类的本质区别:其实是底层的数据结构不同。 | ||
* 数据结构:数据的存储方式。 | ||
* | ||
* Collection的功能: | ||
* A:添加功能 | ||
* boolean add(Object obj):往集合中添加一个元素 | ||
* boolean addAll(Collection c):往集合中添加多个元素。 | ||
* B:获取功能 | ||
* Iterator iterator():迭代器 | ||
* int size():集合的元素个数,长度 | ||
* C:删除功能 | ||
* void clear():清空集合的所有元素 | ||
* boolean remove(Object obj):从集合中移除一个元素 | ||
* boolean removeAll(Collection c):从集合中移除多个元素 | ||
* D:判断功能 | ||
* boolean contains(Object obj):判断集合中是否包含指定的元素 | ||
* boolean containsAll(Collection c):判断集合中是否包含指定的多个元素 | ||
* boolean isEmpty():判断集合是否为空 | ||
* E:交集元素 | ||
* boolean retainAll(Collection c) | ||
* F:转数组 | ||
* Object[] toArray():把集合转成对象数组 | ||
*/ | ||
public class CollectionDemo { | ||
public static void main(String[] args) { | ||
// 创建集合对象 | ||
Collection c = new ArrayList(); | ||
|
||
// boolean add(Object obj):往集合中添加一个元素 | ||
// System.out.println("add:" + c.add("hello")); | ||
// System.out.println("add:" + c.add("world")); | ||
c.add("hello"); | ||
c.add("world"); | ||
c.add("java"); | ||
|
||
// void clear():清空集合的所有元素 | ||
// c.clear(); | ||
|
||
// boolean remove(Object obj):从集合中移除一个元素 | ||
// System.out.println("remove:" + c.remove("world")); | ||
// System.out.println("remove:" + c.remove("javaee")); | ||
|
||
// boolean contains(Object obj):判断集合中是否包含指定的元素 | ||
// System.out.println("contains:" + c.contains("hello")); | ||
// System.out.println("contains:" + c.contains("javaee")); | ||
|
||
// boolean isEmpty():判断集合是否为空 | ||
// System.out.println("isEmpty:" + c.isEmpty()); | ||
|
||
// int size():集合的元素个数,长度 | ||
System.out.println("size:" + c.size()); | ||
|
||
System.out.println("c:" + c); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
day15/code/day15_Collection/src/cn/itcast_02/CollectionDemo2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cn.itcast_02; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
/* | ||
* boolean addAll(Collection c) | ||
* boolean removeAll(Collection c):删除一个,就是删除,返回true | ||
* boolean containsAll(Collection c):全部包含,才是包含。 | ||
* boolean retainAll(Collection c) | ||
*/ | ||
public class CollectionDemo2 { | ||
public static void main(String[] args) { | ||
// 创建集合 | ||
Collection c1 = new ArrayList(); | ||
c1.add("abc1"); | ||
c1.add("abc2"); | ||
c1.add("abc3"); | ||
c1.add("abc4"); | ||
|
||
// 创建集合 | ||
Collection c2 = new ArrayList(); | ||
// c2.add("abc1"); | ||
// c2.add("abc2"); | ||
// c2.add("abc3"); | ||
// c2.add("abc4"); | ||
c2.add("abc5"); | ||
c2.add("abc6"); | ||
c2.add("abc7"); | ||
|
||
// boolean addAll(Collection c) | ||
// System.out.println("addAll:" + c1.addAll(c2)); | ||
|
||
// boolean removeAll(Collection c) | ||
// System.out.println("removeAll:" + c1.removeAll(c2)); | ||
|
||
// boolean containsAll(Collection c) | ||
// System.out.println("containsAll:" + c1.containsAll(c2)); | ||
|
||
// boolean retainAll(Collection c) | ||
System.out.println("retainAll:" + c1.retainAll(c2)); | ||
/* | ||
* 如果我有两个集合A,B。用A对B做交集 交集元素存储在A集合,B集合不发生改变。 返回值表达是A集合是否发生过改变。 | ||
*/ | ||
|
||
System.out.println("c1:" + c1); | ||
System.out.println("c2:" + c2); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
day15/code/day15_Collection/src/cn/itcast_02/CollectionDemo3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cn.itcast_02; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
/* | ||
* Object[] toArray():把集合转成对象数组 | ||
* | ||
* 练习:用集合存储三个学生对象,并遍历。 | ||
*/ | ||
public class CollectionDemo3 { | ||
public static void main(String[] args) { | ||
// 创建集合对象 | ||
Collection c = new ArrayList(); | ||
|
||
// 往集合中添加元素 | ||
// String s = "hello"; | ||
c.add("hello"); | ||
c.add("world"); | ||
c.add("java"); | ||
|
||
// Object[] toArray():把集合转成对象数组 | ||
Object[] objs = c.toArray(); | ||
// 遍历数组 | ||
for (int x = 0; x < objs.length; x++) { | ||
// System.out.println(objs[x]); | ||
String s = (String) objs[x]; | ||
System.out.println(s); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
day15/code/day15_Collection/src/cn/itcast_03/CollectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cn.itcast_03; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
/* | ||
* Object[] toArray():把集合转成对象数组 | ||
* | ||
* 练习:用集合存储三个学生对象,并遍历。 | ||
* | ||
* 总结:集合的使用步骤 | ||
* A:创建集合对象 | ||
* B:创建元素对象 | ||
* C:把元素添加到集合 | ||
* D:遍历集合 | ||
*/ | ||
public class CollectionTest { | ||
public static void main(String[] args) { | ||
// 创建集合对象 | ||
Collection c = new ArrayList(); | ||
|
||
// 创建元素对象 | ||
Student s1 = new Student("林黛玉", 18); | ||
Student s2 = new Student("秦可卿", 20); | ||
Student s3 = new Student("王熙凤", 22); | ||
|
||
// 把学生对象添加到集合 | ||
c.add(s1); | ||
c.add(s2); | ||
c.add(s3); | ||
// c.add(new Student("贾宝玉", 20)); | ||
|
||
// 遍历集合 | ||
Object[] objs = c.toArray(); | ||
for (int x = 0; x < objs.length; x++) { | ||
Student s = (Student) objs[x]; | ||
System.out.println(s.getName() + "---" + s.getAge()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cn.itcast_03; | ||
|
||
public class Student { | ||
private String name; | ||
private int age; | ||
|
||
public Student() { | ||
super(); | ||
} | ||
|
||
public Student(String name, int age) { | ||
super(); | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
} |
Oops, something went wrong.