Advertisement
gon2

Untitled

Feb 12th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. /*
  2.  Takmörkuð eftirlíking af java.util.ArrayList klasanum fyrir Tölvunarfræði 2
  3.  */
  4.  
  5. import edu.princeton.cs.algs4.StdOut;
  6. import java.util.Iterator;
  7. import java.util.NoSuchElementException;
  8.  
  9. public class ArrayList<Item> implements Iterable<Item> {
  10.     private Item[] storage; // Fjölnota geymslusvæði
  11.     private int length; // Fjöldi staka í listanum
  12.  
  13.     public ArrayList() {
  14.         //Smiður, býr til tóman lista með upphafsrýmd 2
  15.         this.storage = (Item[]) new Object[2];
  16.         this.length = 0;
  17.     }
  18.  
  19.     public int size() {
  20.         // Skilar fjölda staka í listanum
  21.         return this.length;
  22.     }
  23.  
  24.     public int capacity() {
  25.         // Skilar rýmd listans, fjölda staka sem hægt er að bæta við án þess að breyta geymslusvæðinu
  26.         return this.storage.length;
  27.     }
  28.  
  29.     public void clear() {
  30.         // Eyðir öllum stökum úr listanum og upphafsstillir rýmd hans
  31.         this.storage = (Item[]) new Object[2];
  32.         this.length = 0;
  33.     }
  34.  
  35.     private void resize(int newCapacity) {
  36.         // Eykur rýmd listans upp í newCapacity, ekki er gert ráð fyrir minnkun á rýmd
  37.         assert newCapacity >= length;
  38.  
  39.         Item[] temp = (Item[]) new Object[newCapacity];
  40.         for (int i = 0; i < this.length; i++) {
  41.             temp[i] = this.storage[i];
  42.         }
  43.         this.storage = temp;
  44.     }
  45.  
  46.     public Item get(int index) {
  47.         // Sækir stak númer index í listanum
  48.         if (index < 0 || this.size() <= index) {
  49.             throw new IndexOutOfBoundsException("Þessi vísir er utan geymslunnar");
  50.         }
  51.         return this.storage[index];
  52.     }
  53.  
  54.     public Item set(int index, Item item) {
  55.         // Yfirskrifar stak númer index með item
  56.         Item previousItem = this.storage[index];
  57.         this.storage[index] = item;
  58.         return previousItem;
  59.     }
  60.  
  61.     public void add(Item item) {
  62.         // Bætir item aftast í listann, eykur rýmd ef á þarf að halda
  63.         if (this.length == this.storage.length) {
  64.             this.resize(2 * this.storage.length);
  65.         }
  66.         this.storage[this.length++] = item;
  67.     }
  68.  
  69.     public void add(int index, Item item) {
  70.         // Bætir item í listann í staðsetningu index,
  71.         if (index < 0 || this.size() < index) {
  72.             throw new IndexOutOfBoundsException("Þessi vísir er utan geymslunnar");
  73.         } else if (index == this.length) {
  74.             this.add(item);
  75.         } else {
  76.             Item oldItem = this.set(index, item);
  77.             this.add(index + 1, oldItem);
  78.         }
  79.     }
  80.  
  81.  
  82.     public Iterator<Item> iterator()  {
  83.         return new ListIterator<Item>(storage);
  84.     }
  85.  
  86.     private class ListIterator<Item> implements Iterator<Item> {
  87.         private Item[] current;
  88.         private int i = 0;
  89.  
  90.         // smiðurinn tekur inn fylkið sem við ætlum að ítra:
  91.         public ListIterator(Item[] first) {
  92.             current = first;
  93.         }
  94.  
  95.         public Item next() {
  96.             if (!hasNext()) throw new NoSuchElementException();
  97.             Item item = current[i];
  98.             i++;
  99.             return item;
  100.         }
  101.  
  102.         // next fallið skoðar hvort það séu fleiri stök í fylkinu:
  103.         public boolean hasNext() {
  104.           return current[i] != null;
  105.         }
  106.  
  107.         // remove er optional í interface-inu:
  108.         public void remove() {
  109.           throw new UnsupportedOperationException();
  110.         }
  111.     }
  112.  
  113.     public static void main(String[] args) {
  114.         ArrayList<Character> myList = new ArrayList<>();
  115.         myList.add(0, 'A'); // Tómur listi búinn til
  116.         myList.add(1, 'N');
  117.         myList.add(1, 'A');
  118.         myList.add(3, 'I'); // Bætt aftast í lista
  119.         myList.add(0, 'B'); // Bætt framan á lista
  120.         myList.add(2, 'N');
  121.  
  122.         StdOut.print("[ ");
  123.  
  124.         // Afkommentið þetta fyrir sýnidæmið
  125.  
  126.         for (Character c : myList) {
  127.             StdOut.print(c + " ");
  128.         }
  129.  
  130.         StdOut.println("]");
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement