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