/** * Removes the element at the specified position in this Bag. * does this by overwriting it was last element then removing * last element * * @param index the index of element to be removed * @return element that was removed from the Bag */publicObjectremove(intindex) {Objecto = data[index]; // make copy of element to remove so it can be returneddata[index] = data[--size]; // overwrite item to remove with last elementdata[size] = null; // null last element, so gc can do its workreturno; }

/** * Removes the first occurrence of the specified element from this Bag, * if it is present. If the Bag does not contain the element, it is * unchanged. does this by overwriting it was last element then removing * last element * * @param o element to be removed from this list, if present * @return <tt>true</tt> if this list contained the specified element */publicbooleanremove(Objecto) {for (inti = 0; i < size; i++) {if (o == data[i]) {data[i] = data[--size]; // overwrite item to remove with last elementdata[size] = null; // null last element, so gc can do its workreturntrue; } }

returnfalse; }

/** * Removes from this Bag all of its elements that are contained in the * specified Bag. * * @param bag Bag containing elements to be removed from this Bag * @return {@code true} if this Bag changed as a result of the call */publicbooleanremoveAll(Bagbag) {booleanmodified = false;

for (inti = 0; i < bag.size(); i++) {Objecto1 = bag.get(i);

for (intj = 0; j < size; j++) {Objecto2 = data[j];

if (o1 == o2) {remove(j);j--;modified = true;break; } } }

returnmodified; }

/** * Returns the element at the specified position in Bag. * * @param index index of the element to return * @return the element at the specified position in bag */publicObjectget(intindex) {returndata[index]; }

/** * Returns the number of elements in this bag. * * @return the number of elements in this bag */publicintsize() {returnsize; }

/** * Adds the specified element to the end of this bag. * if needed also increases the capacity of the bag. * * @param o element to be added to this list */publicvoidadd(Objecto) { // if size greater than data capacity increase capacityif(size == data.length) {grow(); }

/** * Removes all of the elements from this bag. The bag will * be empty after this call returns. */publicvoidclear() {// null all elements so gc can clean upfor (inti = 0; i < size; i++) {data[i] = null; }

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org