|
| 1 | +package Bags; |
| 2 | + |
| 3 | +import java.util.Iterator; |
| 4 | +import java.util.NoSuchElementException; |
| 5 | + |
| 6 | +/** |
| 7 | + * Collection which does not allow removing elements (only collect and iterate) |
| 8 | + * |
| 9 | + * @param <Element> - the generic type of an element in this bag |
| 10 | + */ |
| 11 | +public class Bag<Element> implements Iterable<Element> { |
| 12 | + |
| 13 | + private Node<Element> firstElement; // first element of the bag |
| 14 | + private int size; // size of bag |
| 15 | + |
| 16 | + private static class Node<Element> { |
| 17 | + private Element content; |
| 18 | + private Node<Element> nextElement; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Create an empty bag |
| 23 | + */ |
| 24 | + public Bag() { |
| 25 | + firstElement = null; |
| 26 | + size = 0; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @return true if this bag is empty, false otherwise |
| 31 | + */ |
| 32 | + public boolean isEmpty() { |
| 33 | + return firstElement == null; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @return the number of elements |
| 38 | + */ |
| 39 | + public int size() { |
| 40 | + return size; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param element - the element to add |
| 45 | + */ |
| 46 | + public void add(Element element) { |
| 47 | + Node<Element> oldfirst = firstElement; |
| 48 | + firstElement = new Node<>(); |
| 49 | + firstElement.content = element; |
| 50 | + firstElement.nextElement = oldfirst; |
| 51 | + size++; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Checks if the bag contains a specific element |
| 56 | + * |
| 57 | + * @param element which you want to look for |
| 58 | + * @return true if bag contains element, otherwise false |
| 59 | + */ |
| 60 | + public boolean contains(Element element) { |
| 61 | + Iterator<Element> iterator = this.iterator(); |
| 62 | + while(iterator.hasNext()) { |
| 63 | + if (iterator.next().equals(element)) { |
| 64 | + return true; |
| 65 | + } |
| 66 | + } |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return an iterator that iterates over the elements in this bag in arbitrary order |
| 72 | + */ |
| 73 | + public Iterator<Element> iterator() { |
| 74 | + return new ListIterator<>(firstElement); |
| 75 | + } |
| 76 | + |
| 77 | + @SuppressWarnings("hiding") |
| 78 | + private class ListIterator<Element> implements Iterator<Element> { |
| 79 | + private Node<Element> currentElement; |
| 80 | + |
| 81 | + public ListIterator(Node<Element> firstElement) { |
| 82 | + currentElement = firstElement; |
| 83 | + } |
| 84 | + |
| 85 | + public boolean hasNext() { |
| 86 | + return currentElement != null; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * remove is not allowed in a bag |
| 91 | + */ |
| 92 | + @Override |
| 93 | + public void remove() { |
| 94 | + throw new UnsupportedOperationException(); |
| 95 | + } |
| 96 | + |
| 97 | + public Element next() { |
| 98 | + if (!hasNext()) |
| 99 | + throw new NoSuchElementException(); |
| 100 | + Element element = currentElement.content; |
| 101 | + currentElement = currentElement.nextElement; |
| 102 | + return element; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * main-method for testing |
| 108 | + */ |
| 109 | + public static void main(String[] args) { |
| 110 | + Bag<String> bag = new Bag<>(); |
| 111 | + |
| 112 | + bag.add("1"); |
| 113 | + bag.add("1"); |
| 114 | + bag.add("2"); |
| 115 | + |
| 116 | + System.out.println("size of bag = " + bag.size()); |
| 117 | + for (String s : bag) { |
| 118 | + System.out.println(s); |
| 119 | + } |
| 120 | + |
| 121 | + System.out.println(bag.contains(null)); |
| 122 | + System.out.println(bag.contains("1")); |
| 123 | + System.out.println(bag.contains("3")); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments