Skip to content

Commit

Permalink
初步完成第四章lecture
Browse files Browse the repository at this point in the history
  • Loading branch information
lzz19980125 committed Dec 17, 2021
1 parent 46c8c49 commit 3dc1aa1
Show file tree
Hide file tree
Showing 17 changed files with 582 additions and 8 deletions.
7 changes: 7 additions & 0 deletions lecture/src/Hight_order_fun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @author Li Zezhong
* @create 2021-12-16 19:07
*/
public interface Hight_order_fun {
public int apply(int x);
}
7 changes: 7 additions & 0 deletions lecture/src/Ourcomparable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @author Li Zezhong
* @create 2021-12-17 14:28
*/
public interface Ourcomparable<Item> {
public int compareTo(Item o);
}
1 change: 1 addition & 0 deletions lecture/src/lecture2_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @author Li Zezhong
* @create 2021-12-03 20:28
*/

public class lecture2_1 {
public static void main(String[] args) {
//正向构建List
Expand Down
15 changes: 13 additions & 2 deletions lecture/src/lecture4_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public static void main(String[] args) {
n.addLast("c");
n.rotateRight();

// VengefulSLList <String> n = new VengefulSLList<>(null);
System.out.println(do_twice(new Tenx(),2));
VengefulSLList <String> f = (VengefulSLList <String>) new SList<String>(null);
// n.addLast("a");
// n.addLast("d");
// n.addLast("b");
Expand All @@ -20,6 +21,9 @@ public static void main(String[] args) {
// n.removeLast();
// n.print_deletedItems();
}
public static int do_twice(Hight_order_fun f, int x){
return f.apply(f.apply(x));
}
}

class RotatingSLList<Item> extends SList<Item>{
Expand Down Expand Up @@ -49,5 +53,12 @@ public Item removeLast(){
public void print_deletedItems(){
deletedItems.positive_print();
}

}

class Tenx implements Hight_order_fun{

@Override
public int apply(int x) {
return 10*x;
}
}
66 changes: 66 additions & 0 deletions lecture/src/lecture4_3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
* @author Li Zezhong
* @create 2021-12-16 16:30
*/

public class lecture4_3 {
public static void main(String[] args){
Dog[] dogs = new Dog[]{new Dog("Elyse", 3), new Dog("Sture", 9), new Dog("Benjamin", 15)};
max(dogs).bark();
Comparator<Dog> nc = Dog.getNameComparator();
System.out.println(nc.compare(dogs[0],dogs[2]));
}

public static Dog max(Dog[] items){
int maxDex = 0;
for (int i = 0; i < items.length; i += 1) {
int cmp = items[i].compareTo(items[maxDex]);
if (cmp > 0) {
maxDex = i;
}
}
return items[maxDex];
}

public static void getWords(String inputFileName){
List<String> lst = new ArrayList<String>();
In in = new In();

}
}

class Dog implements Comparable<Dog>{
private String name;
private int size;

public Dog(String n, int s) {
name = n;
size = s;
}

public void bark() {
System.out.println(name + " says: bark");
}

@Override
public int compareTo(Dog o) {
return this.size - o.size;
}

private static class NameComparator implements Comparator<Dog> {

@Override
public int compare(Dog o1, Dog o2) {
return o1.name.compareTo(o2.name);
}
}

public static Comparator<Dog> getNameComparator() {
return new NameComparator();
}
}

Loading

0 comments on commit 3dc1aa1

Please sign in to comment.