java:inner_classes
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java:inner_classes [2019/04/02 20:20] – gthanos | java:inner_classes [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Μη στατικές εμφωλευμένες κλάσεις ====== | ====== Μη στατικές εμφωλευμένες κλάσεις ====== | ||
| - | Οι μη στατικές εμφωλευμένες κλάσεις ή εσωτερικές κλάσεις (inner classes) αποτελούν την γενικότερη περίπτωση εμφώλευσης μίας κλάσης μέσα σε μία άλλη κλάση. | + | Οι μη στατικές εμφωλευμένες κλάσεις ή εσωτερικές κλάσεις (inner classes) αποτελούν την γενικότερη περίπτωση εμφώλευσης μίας κλάσης μέσα σε μία άλλη κλάση. |
| - | Η λίστα θα χρειαστεί να έχει | + | Η λίστα θα χρειαστεί να έχει |
| - την κλάση που περιγράφει τον κόμβο της λίστας και | - την κλάση που περιγράφει τον κόμβο της λίστας και | ||
| - μία κλάση τύπου // | - μία κλάση τύπου // | ||
| - | Οι δύο παραπάνω κλάσεις εξυπηρετεί να δηλωθούν ως εσωτερικές κλάσεις αποκρύπτοντας την | + | Οι δύο παραπάνω κλάσεις εξυπηρετεί να δηλωθούν ως εσωτερικές κλάσεις αποκρύπτοντας την |
| + | |||
| + | Δείτε τον παρακάτω κώδικα που την υλοποιεί. | ||
| <code java LinkedList.java> | <code java LinkedList.java> | ||
| + | /* A linked list with sentinels at head and tail. | ||
| + | */ | ||
| public class LinkedList< | public class LinkedList< | ||
| private Node< | private Node< | ||
| + | int size; | ||
| | | ||
| public LinkedList() { | public LinkedList() { | ||
| Line 15: | Line 20: | ||
| tail = new Node<> | tail = new Node<> | ||
| head.setNext(tail); | head.setNext(tail); | ||
| + | size = 0; | ||
| } | } | ||
| | | ||
| Line 27: | Line 33: | ||
| } | } | ||
| | | ||
| - | public Node< | + | public Node< |
| public void setNext(Node< | public void setNext(Node< | ||
| public Node< | public Node< | ||
| Line 37: | Line 43: | ||
| private class Iterator< | private class Iterator< | ||
| Node< | Node< | ||
| + | | ||
| public Iterator(Node< | public Iterator(Node< | ||
| curr = c; | curr = c; | ||
| Line 52: | Line 59: | ||
| curr.getPrev().setNext(curr.getNext()); | curr.getPrev().setNext(curr.getNext()); | ||
| curr.getNext().setPrev(curr.getPrev()); | curr.getNext().setPrev(curr.getPrev()); | ||
| - | curr = null; | + | curr = curr.getPrev(); |
| } | } | ||
| } | } | ||
| Line 61: | Line 68: | ||
| | | ||
| // append in the end | // append in the end | ||
| - | public | + | public |
| Node< | Node< | ||
| tail.getPrev().setNext(plus); | tail.getPrev().setNext(plus); | ||
| tail.setPrev(plus); | tail.setPrev(plus); | ||
| + | size++; | ||
| + | return true; | ||
| + | } | ||
| + | | ||
| + | public boolean add(int index, E elem) { | ||
| + | if(index> | ||
| + | return false; | ||
| + | Node< | ||
| + | for(int i=0; curr != tail && i<index; i++) { | ||
| + | curr = curr.next; | ||
| + | } | ||
| + | Node< | ||
| + | curr.prev.next = plus; | ||
| + | curr.prev = plus; | ||
| + | size++; | ||
| + | return true; | ||
| } | } | ||
| | | ||
| Line 87: | Line 110: | ||
| } | } | ||
| return -1; | return -1; | ||
| + | } | ||
| + | | ||
| + | int size() { | ||
| + | return size; | ||
| } | } | ||
| } | } | ||
| Line 97: | Line 124: | ||
| public class LinkedListUsage { | public class LinkedListUsage { | ||
| - | public static final int SIZE = 20; | + | public static final int SIZE = 16; |
| + | public static final int RANGE = 8 * SIZE; | ||
| + | |||
| + | static void print(LinkedList< | ||
| + | Iterator< | ||
| + | |||
| + | // Print list | ||
| + | while(it.hasNext()) { | ||
| + | System.out.print(it.next()+" | ||
| + | } | ||
| + | System.out.println(); | ||
| + | } | ||
| | | ||
| public static void main(String[] args) { | public static void main(String[] args) { | ||
| Line 103: | Line 141: | ||
| LinkedList< | LinkedList< | ||
| for(int i=0; i<SIZE; i++) { | for(int i=0; i<SIZE; i++) { | ||
| - | | + | |
| + | int pos = rand.nextInt(list.size()+1); | ||
| + | System.out.format(" | ||
| + | list.add(pos, | ||
| + | print(list); | ||
| } | } | ||
| + | print(list); | ||
| + | | ||
| + | // Remove all elements less than RANGE/2 | ||
| + | System.out.println(" | ||
| Iterator< | Iterator< | ||
| while(it.hasNext()) { | while(it.hasNext()) { | ||
| - | System.out.print(it.next()+" | + | |
| + | //System.out.println(" | ||
| + | if(value < RANGE/2) { | ||
| + | // | ||
| + | it.remove(); | ||
| + | } | ||
| } | } | ||
| - | | + | |
| + | | ||
| } | } | ||
| } | } | ||
| + | |||
| </ | </ | ||
java/inner_classes.1554236457.txt.gz · Last modified: 2019/04/02 19:20 (external edit)
