-
Notifications
You must be signed in to change notification settings - Fork 23
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
Mario Benov
committed
Nov 28, 2023
1 parent
82af3f7
commit c815881
Showing
8 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
materials/2023-2024/11b/2023-11-27-generics/generics/.idea/.gitignore
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
materials/2023-2024/11b/2023-11-27-generics/generics/.idea/encodings.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
materials/2023-2024/11b/2023-11-27-generics/generics/.idea/misc.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
materials/2023-2024/11b/2023-11-27-generics/generics/.idea/modules.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
materials/2023-2024/11b/2023-11-27-generics/generics/.idea/vcs.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
materials/2023-2024/11b/2023-11-27-generics/generics/generics.iml
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 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
31 changes: 31 additions & 0 deletions
31
materials/2023-2024/11b/2023-11-27-generics/generics/src/Main.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 @@ | ||
import java.util.Comparator; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
List<Integer> list = new LinkedList<>(); | ||
// list.add(Integer.getInteger("123")); | ||
list.add(123); | ||
// list.add(true); | ||
|
||
MyList<Integer> list2 = new MyList<>(); | ||
// list2.add(123); | ||
|
||
MyList<Integer> list3 = MyList.<Integer>fromArray( | ||
new Integer[]{1, 2, 3} | ||
); | ||
|
||
list3.<Object>foo(); | ||
|
||
list2.sort((o1, o2) -> o1.hashCode() - o2.hashCode()); | ||
list2.sort((o1, o2) -> o1 - o2); | ||
|
||
list3.sort(Comparator.comparingInt(Object::hashCode)); | ||
list3.sort(Comparator.comparingInt(o -> o)); | ||
|
||
list3.sort(); | ||
|
||
MyList<Main> list4 = new MyList<Main>(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
materials/2023-2024/11b/2023-11-27-generics/generics/src/MyList.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,54 @@ | ||
import java.io.Serializable; | ||
import java.util.Comparator; | ||
|
||
public class MyList<E extends Comparable & Serializable> { | ||
private class Node { | ||
E value; | ||
Node next = null; | ||
|
||
public Node(E value) { | ||
this.value = value; | ||
} | ||
}; | ||
|
||
private Node head = null; | ||
|
||
public void add(E e) { | ||
if(head == null) { | ||
head = new Node(e); | ||
return; | ||
} | ||
|
||
// ... | ||
} | ||
|
||
public static <K extends Comparable & Serializable> MyList<K> fromArray(K[] arr) { | ||
MyList<K> l = new MyList<>(); | ||
// ... | ||
// l.add(1); | ||
l.add(arr[0]); | ||
|
||
return l; | ||
} | ||
|
||
public <O> void foo() {} | ||
|
||
public void sort(Comparator<E> comp) { | ||
E o1 = head.value; | ||
E o2 = head.next.value; | ||
// for( ... ) | ||
int result = comp.compare(o1, o2); | ||
// if(result < 0) | ||
// swap(); | ||
} | ||
|
||
public void sort() { | ||
E o1 = head.value; | ||
E o2 = head.next.value; | ||
// for( ... ) | ||
// int result = ((Comparable)o1).compareTo(o2); | ||
int result = o1.compareTo(o2); | ||
// if(result < 0) | ||
// swap(); | ||
} | ||
} |