Skip to content

Commit 8a64719

Browse files
author
yangqc
committed
调整编码
1 parent 8885859 commit 8a64719

20 files changed

Lines changed: 532 additions & 475 deletions
Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,74 @@
11
package thinkinjava.test14;
22

33
public class Person {
4-
public final String first;
5-
public final String last;
6-
public final String address;
7-
public Person(String first,String last,String address){
8-
this.first=first;
9-
this.last=last;
10-
this.address=address;
11-
}
12-
public String toString(){
13-
return "Person: "+first+" "+last+" "+address;
14-
}
15-
public static class NullPerson extends Person implements Null{
16-
private NullPerson(){
17-
super("None","None","None");
18-
}
19-
public String toString(){
20-
return "NullPerson";
21-
}
22-
}
23-
public static final Person NULL=new NullPerson();
24-
public static void main(String[] args) {
25-
Person person1=new Person("a","b","c");
26-
}
4+
public final String first;
5+
public final String last;
6+
public final String address;
7+
8+
public Person(String first, String last, String address) {
9+
this.first = first;
10+
this.last = last;
11+
this.address = address;
12+
}
13+
14+
public String toString() {
15+
return "Person: " + first + " " + last + " " + address;
16+
}
17+
18+
public static class NullPerson extends Person implements Null {
19+
private NullPerson() {
20+
super("None", "None", "None");
21+
}
22+
23+
public String toString() {
24+
return "NullPerson";
25+
}
26+
}
27+
28+
public static final Person NULL = new NullPerson();
29+
30+
public static void main(String[] args) {
31+
Person person1 = new Person("a", "b", "c");
32+
}
2733
}
28-
class Position{
29-
private String title;
30-
private Person person;
31-
public Position(String jobTitle,Person employee){
32-
title=jobTitle;
33-
person=employee;
34-
if(person==null){
35-
person=Person.NULL;
36-
}
37-
}
38-
public Position(String jobTitle){
39-
title=jobTitle;
40-
person=Person.NULL;
41-
}
42-
public String getTitle(){
43-
return title;
44-
}
45-
public void setTitle(String newTitle){
46-
title=newTitle;
47-
}
48-
public Person getPerson(){
49-
return person;
50-
}
51-
public void setPerson(Person newPerson){
52-
person=newPerson;
53-
if(person==null){
54-
person=Person.NULL;
55-
}
56-
}
57-
public String toString(){
58-
return "Position: "+title+" "+person;
59-
}
34+
35+
class Position {
36+
private String title;
37+
private Person person;
38+
39+
public Position(String jobTitle, Person employee) {
40+
title = jobTitle;
41+
person = employee;
42+
if (person == null) {
43+
person = Person.NULL;
44+
}
45+
}
46+
47+
public Position(String jobTitle) {
48+
title = jobTitle;
49+
person = Person.NULL;
50+
}
51+
52+
public String getTitle() {
53+
return title;
54+
}
55+
56+
public void setTitle(String newTitle) {
57+
title = newTitle;
58+
}
59+
60+
public Person getPerson() {
61+
return person;
62+
}
63+
64+
public void setPerson(Person newPerson) {
65+
person = newPerson;
66+
if (person == null) {
67+
person = Person.NULL;
68+
}
69+
}
70+
71+
public String toString() {
72+
return "Position: " + title + " " + person;
73+
}
6074
}

src/main/java/thinkinjava/test14/Staff.java

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,41 @@
22

33
import java.util.ArrayList;
44

5-
public class Staff extends ArrayList<Position>{
6-
public void add(String title,Person person){
7-
add(new Position(title,person));
8-
}
9-
public void add(String... titles){
10-
for(String title:titles){
11-
add(new Position(title));
12-
}
13-
}
14-
public Staff(String... titles){
15-
add(titles);
16-
}
17-
public boolean positionAvailable(String title){
18-
for(Position position:this){
19-
if(position.getTitle().equals(title) && position.getPerson()==Person.NULL){
20-
return true;
21-
}
22-
}
23-
return false;
24-
}
25-
public void fillPosition(String title,Person hire){
26-
for(Position position:this){
27-
if(position.getTitle().equals(title) && position.getPerson()==Person.NULL){
28-
position.setPerson(hire);
29-
return;
30-
}
31-
}
32-
throw new RuntimeException("Position "+title+" not available");
33-
}
34-
public static void main(String[] args) {
35-
Staff staff=new Staff();
36-
}
5+
public class Staff extends ArrayList<Position> {
6+
public void add(String title, Person person) {
7+
add(new Position(title, person));
8+
}
9+
10+
public void add(String... titles) {
11+
for (String title : titles) {
12+
add(new Position(title));
13+
}
14+
}
15+
16+
public Staff(String... titles) {
17+
add(titles);
18+
}
19+
20+
public boolean positionAvailable(String title) {
21+
for (Position position : this) {
22+
if (position.getTitle().equals(title) && position.getPerson() == Person.NULL) {
23+
return true;
24+
}
25+
}
26+
return false;
27+
}
28+
29+
public void fillPosition(String title, Person hire) {
30+
for (Position position : this) {
31+
if (position.getTitle().equals(title) && position.getPerson() == Person.NULL) {
32+
position.setPerson(hire);
33+
return;
34+
}
35+
}
36+
throw new RuntimeException("Position " + title + " not available");
37+
}
38+
39+
public static void main(String[] args) {
40+
Staff staff = new Staff();
41+
}
3742
}

src/main/java/thinkinjava/test16/ArrayOfGenerics.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@
33
import java.util.ArrayList;
44
import java.util.Arrays;
55
import java.util.List;
6-
/*
6+
7+
/**
78
* 泛型数组,一般不用,最好还是用泛型容器吧
9+
* java的数组并没有泛型
810
* 2015.9.1
911
*/
1012
public class ArrayOfGenerics {
11-
@SuppressWarnings("unchecked")
12-
public static void main(String[] args) {
13-
List<String>[] ls;
14-
List[] la=new List[10];
15-
ls=la;
16-
ls[0]=new ArrayList<String>();
17-
Object[] objects=ls;
18-
objects[1]=new ArrayList<Integer>();
19-
List<BerylliumSphere>[] spheres=new List[10];
20-
for(int i=0;i<spheres.length;i++){
21-
spheres[i]=new ArrayList<BerylliumSphere>();
22-
}
23-
System.out.println(Arrays.toString(spheres));
24-
}
13+
14+
@SuppressWarnings("unchecked")
15+
public static void main(String[] args) {
16+
List<String>[] ls;
17+
List[] la = new List[10];
18+
ls = la;
19+
ls[0] = new ArrayList<>();
20+
Object[] objects = ls;
21+
objects[1] = new ArrayList<Integer>();
22+
List<BerylliumSphere>[] spheres = new List[10];
23+
for (int i = 0; i < spheres.length; i++) {
24+
spheres[i] = new ArrayList<>();
25+
}
26+
System.out.println(Arrays.toString(spheres));
27+
}
2528
}

src/main/java/thinkinjava/test16/ArrayOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package thinkinjava.test16;
22

33
import java.util.Arrays;
4-
/*
4+
/**
55
* 几种创建数组的对象的方式,记住,a[] v=new a[]{}这种方式表示创建了一个长度为0的数组对象
66
* 1.对象数组中存储的是对象的引用
77
* 2.基本类型数组中存放的是基本类型的数值
@@ -25,7 +25,7 @@ public static void main(String[] args) {
2525

2626
a = new BerylliumSphere[] { new BerylliumSphere(),
2727
new BerylliumSphere() };
28-
28+
2929
// for(int i=0;i<100;i++){
3030
// e[i]=new BerylliumSphere();
3131
// }

src/main/java/thinkinjava/test16/ContainerComparison.java

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,40 @@
33
import java.util.ArrayList;
44
import java.util.Arrays;
55
import java.util.List;
6-
/*
6+
7+
/**
78
* 数组和List存储和使用的对象的区别
89
* 2015.8.31
910
*/
10-
class BerylliumSphere{
11-
private static long counter;
12-
private final long id=counter++;
13-
public String toString(){
14-
return "Shere "+id;
15-
}
11+
class BerylliumSphere {
12+
private static long counter;
13+
private final long id = counter++;
14+
15+
public String toString() {
16+
return "Shere " + id;
17+
}
1618
}
19+
1720
public class ContainerComparison {
18-
public static void main(String[] args) {
19-
BerylliumSphere[] spheres=new BerylliumSphere[10];
20-
for(int i=0;i<5;i++){
21-
spheres[i]=new BerylliumSphere();
22-
}
23-
System.out.println(Arrays.toString(spheres));
24-
System.out.println(spheres[4]);
25-
List<BerylliumSphere> sphereList=new ArrayList<BerylliumSphere>();
26-
for(int i=0;i<5;i++){
27-
sphereList.add(new BerylliumSphere());
28-
}
29-
System.out.println(sphereList);
30-
System.out.println(sphereList.get(4));
31-
int [] integers={0,1,2,3,4,5};
32-
System.out.println(Arrays.toString(integers));
33-
System.out.println(integers[4]);
34-
List<Integer> intList=new ArrayList<Integer>(Arrays.asList(0,1,2,3,4,5));
35-
intList.add(97);
36-
System.out.println(intList);
37-
System.out.println(intList.get(0));
38-
}
21+
public static void main(String[] args) {
22+
BerylliumSphere[] spheres = new BerylliumSphere[10];
23+
for (int i = 0; i < 5; i++) {
24+
spheres[i] = new BerylliumSphere();
25+
}
26+
System.out.println(Arrays.toString(spheres));
27+
System.out.println(spheres[4]);
28+
List<BerylliumSphere> sphereList = new ArrayList<BerylliumSphere>();
29+
for (int i = 0; i < 5; i++) {
30+
sphereList.add(new BerylliumSphere());
31+
}
32+
System.out.println(sphereList);
33+
System.out.println(sphereList.get(4));
34+
int[] integers = {0, 1, 2, 3, 4, 5};
35+
System.out.println(Arrays.toString(integers));
36+
System.out.println(integers[4]);
37+
List<Integer> intList = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5));
38+
intList.add(97);
39+
System.out.println(intList);
40+
System.out.println(intList.get(0));
41+
}
3942
}

src/main/java/thinkinjava/test16/IceCream.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,30 @@
44
import java.util.Random;
55

66
public class IceCream {
7-
private static Random rand=new Random(47);
8-
static final String[] FLAVORS={"njknjk","2cas","3casca","cscs","c2scs","dqwdwq"};
9-
public static String[] flavorSet(int n){
10-
if(n>FLAVORS.length){
11-
throw new IllegalArgumentException("Set too big");
12-
}
13-
String[] results=new String[n];
14-
boolean[] picked=new boolean[FLAVORS.length];
15-
for(int i=0;i<n;i++){
16-
int t;
17-
do{
18-
t=rand.nextInt(FLAVORS.length);
19-
}
20-
while(picked[t]);//ɸѡ²»Öظ´µÄÔªËØ
21-
results[i]=FLAVORS[t];
22-
picked[t]=true;
23-
}
24-
return results;
25-
}
26-
public static void main(String[] args) {
27-
for(int i=0;i<7;i++){
28-
System.out.println(Arrays.toString(flavorSet(6)));
29-
}
30-
}
7+
private static Random rand = new Random(47);
8+
static final String[] FLAVORS = {"njknjk", "2cas", "3casca", "cscs", "c2scs", "dqwdwq"};
9+
10+
public static String[] flavorSet(int n) {
11+
if (n > FLAVORS.length) {
12+
throw new IllegalArgumentException("Set too big");
13+
}
14+
String[] results = new String[n];
15+
boolean[] picked = new boolean[FLAVORS.length];
16+
for (int i = 0; i < n; i++) {
17+
int t;
18+
do {
19+
t = rand.nextInt(FLAVORS.length);
20+
}
21+
while (picked[t]);//ɸѡ²»Öظ´µÄÔªËØ
22+
results[i] = FLAVORS[t];
23+
picked[t] = true;
24+
}
25+
return results;
26+
}
27+
28+
public static void main(String[] args) {
29+
for (int i = 0; i < 7; i++) {
30+
System.out.println(Arrays.toString(flavorSet(6)));
31+
}
32+
}
3133
}

0 commit comments

Comments
 (0)