The second chapter uses the array to implement the bag 1. Implementing an ADT bag with a fixed size array a core set of methods
The core approach: the implementation of the intent of the class is extremely important, and allows reasonable testing, also become the core group.
- constructor function
- Public boolean Add (T anentity)
- Public t[] ToArray ()
- public boolean isfull ()
Implementation of the core approach
data field : Before you define any methods, consider the data fields of the class.
private final T[] bag;//袋子用来容纳对象的数组private static final int DEFAULT_CAPACITY=25;//默认数组大小private int numberOfEntries;//跟踪袋子中物品当前物品的数量
Adding data fields to UML
First change to:-bag:t []
constructor Function :
public ArrayBag() { this(DEFAULT_CAPACITY); } public ArrayBag(int capacity) { numberOfEntires = 0; T[] tempBag = (T[]) new Object[capacity]; bag = tempBag; }
Method add
public boolean add(T newEntity) { boolean result = true; if (isFull()) { result = false; } else { bag[numberOfEntires] = newEntity; numberOfEntires++; } return result; }
Method Isfull
public boolean isFull() { return numberOfEntires == bag.length; }
Method ToArray
Method ToArray cannot return the bag itself, returning the bag itself, giving the customer direct access to private data. The class should not return a reference to a private array data field.
public T[] toArray() { T[] result = (T[]) new Object[numberOfEntires]; for (int index = 0; index < numberOfEntires; index++) { result[index] = bag[index]; } return result;}
More ways to implement
(1) Methods IsEmpty and Getcurrentsize
public boolean isEmpty() { return numberOfEntires==0; } public Integer getCurrentSize() { return numberOfEntires; }
(2) method getfrequencyof Public Integer getfrequencyof (T anentity) {int counter = 0, for (int i = 0; i < numberofentires; i++) {if (bag[i ].equals (anentity)) {counter++;}} return counter; }
(2) method contains
public boolean contains(T anEntity) { boolean flag = false; for (int i = 0; i < numberOfEntires; i++) { if (bag[i].equals(anEntity)) { flag = true; break; } } return flag;}
Ways to delete items
(1) Method clear
public void clear() { // 调用remove方法,不断删除,知道为空 while (!isEmpty()) { remove(); }}
(2) Deletion of an item not specifically specified
First, ensure that the bag is not empty before deletion, and secondly, it is simpler to remove from the back; Step: Access the item so that it returns the array element that sets the item is null; The Numberofentires value is reduced by one.
public T remove() { T result = null; if (numberOfEntires > 0) { result = bag[numberOfEntires - 1]; bag[numberOfEntires - 1] = null; numberOfEntires--; } return result;}
(3) Delete a given item
When the item appears multiple times, only the first occurrence is removed. Just bag[inex]=null; however, this creates a notch in the array where the bag content is no longer located above the contiguous array position, and can be solved by moving the back elements forward one by another, but time consuming. We don't have to maintain the position of the items in the bag in the array, so we can use the last item in the array to replace the item that will be removed, and then delete the last item in the array.
public boolean remove(T anEntity) { boolean flag = false;// 未删除 for (int i = 0; i < numberOfEntires; i++) { if (anEntity.equals(bag[i])) { bag[i] = bag[numberOfEntires - 1]; bag[numberOfEntires - 1] = null; numberOfEntires--; flag = true; break; } } return flag; }
(4) Avoid duplication of work
Private method, for two remove method calls
private T remove(int givenIndex) { T result = null; if (!isEmpty() && givenIndex >= 0) { result = bag[givenIndex]; numberOfEntires--; bag[givenIndex] = bag[numberOfEntires]; bag[numberOfEntires] = null; } return result;}
Private method, Getindexof
private int getIndexOf(T anEntity) { int where = -1; boolean found = false; for (int i = 0; !found && i < numberOfEntires; i++) { if (anEntity.equals(bag[i])) { found = true; where = i; } } return where;}
Remove after modification
public T remove() { T result = remove(numberOfEntires - 1); return result;}
Remove after modification (T anentity)
public boolean remove(T anEntity) { int index = getIndexOf(anEntity); T result = remove(index); return anEntity.equals(result);}
2. Implementing an ADT bag with a variable-sized array
An array of fixed sizes implements the ADT bag, which limits the size of the bag. When the array is full, Isfull returns the True,add method returns false. Some applications can use such a limited number of bags or collections, while others, the size of the collection need to grow indefinitely.
Detailed procedures
Suppose you have an array of myarray references, first define a Oldarray,oldarray reference myarray, create a new array larger than myarray, let myarray reference this new array, and then copy the Oldarray content to MyArray, Then Oldarray=null.
Code can be used myarray=arrays.copyof (myarray,2*myarray.length)
A new realization of the bag
Ensure that the array is not full when Add.
The advantages and disadvantages of the array implementation bag
Advantages:
- The process of adding an item to the bag is fast, and deleting and adding items at the end of the array is very simple and fast because we know the index at the end of the array.
- Deleting non-specific items is also quick.
- Delete the characteristic items in the middle of the array, avoid creating a gap in the array, and take the strategy of replacing the deleted item with the last item in the array.
- Allow direct access to elements, as long as you know the index.
- Fixed-size arrays limit bag capacity.
- Variable-size arrays, which need to copy items into a new array, copy all the references to the array items, do not occupy too much space, and do not spend much time on the move.
Disadvantages:
- Deleting a feature item takes time to locate the item
- Increasing the size of the array takes time to copy its items (references).
The second chapter uses the array to implement the bag