1. First define the prototype of an array, this class has a data base of properties and methods to achieve the basic array class, we can create an array, but its elements can not operate, then you realize the operation of the array elements method
public class the Array <E> { Private E [] Data; Private int size; // constructor, the capacity of the array capacity passed configured the Array public the Array ( int capacity) { Data = (E []) new new Object [capacity]; size = 0 ; } // no constructor parameter, default = 10 array capacity capacity public the array () { the this (10 ); } // get the array capacity public int getCapacity () { return data.length; } / /Get the number of elements in the array public int getSize () { return size; } // Returns the array is empty public Boolean isEmpty () { return size == 0 ; } }
2. A method to achieve an add element is added, and the need to provide a location index element to be inserted is inserted, when the element is inserted in the index at the time index comprising index back of an element needs to move backward
// inserted position index index of a new element E public void the Add ( int index, E E) { // when the array is full, an exception is thrown IF (size == data.length) the throw new new an IllegalArgumentException ( "Failed to add, array full "! ); // when the index parameter is invalid, throws an exception iF (index <0 || index> size) the throw new new IllegalArgumentException (" failed to add, index parameter illegal "! ); // the index and index elements move back behind, then the index is inserted in the target element E, the final size increment. for ( int I = size -. 1; I> = index; I - ) Data [I+ 1] = data[i]; data[index] = e; size ++; }
By multiplexing method we can achieve this add to the array elements and inserted into the head insertion end of the array elements method
// Add a new element in the head array public void the addFirst ( E E) { the Add ( 0 , E); } // add a new element in the end of the array public void addLast ( E E) { the Add (size, E); }
3. Obtain elements and element update
// Get the element at index index public E GET ( int index) { IF (index <0 || index> = size) the throw new new an IllegalArgumentException ( "acquisition has failed, index illegal." ); Return Data [index]; } // modify position index index elements E public void sET ( int index, E E) { IF (index <0 || index> = size) the throw new new an IllegalArgumentException ( "setup fails, index illegal." ); Data [ index] = E; }
4. Find the elements and remove elements
// Find the index array element e is located, if the element e is not present, it returns -1 public int Find ( E e) { // loop through the array, the presence of the index i is returned e for ( int i = 0; i < size; I ++ ) { IF (Data [I] .equals (E) ) return I; } return -1 ; }
Instead delete elements and adding elements, add elements behind the index moved back, and delete elements of the index back to move forward
// remove elements from the array index position, the return element to remove public E Remove ( int index) { IF (index <0 || index> = size) the throw new new an IllegalArgumentException ( "delete fails, index illegal." ); // behind a forward elements to the index, and returns the deleted element E RET = Data [index]; for ( int I = index +. 1; I <size; I ++ ) Data [I -. 1] = Data [I]; size - ;
Data [size] = null; return RET; }
By multiplexing method remove method, we can achieve delete array element head and tail elements
// removes the first element from the array elements returned remove public E removeFirst () { return Remove (0 ); } // remove the last element of the array, return the removed elements public E removeLast () { return Remove (size -. 1 ); }
Sometimes we do not remove elements with the index, but to delete based on the value of the element itself, we can achieve this by re-operating with the find method
// removed from the array element E public void removeElement ( E E) { int index = Find (E); IF (index = -1! ) Remove (index); }
The scaling capacity of the array
When the array is full you can not add an element clearly unreasonable, so we need to implement an array of scalable capacity, which is dynamic arrays. First, let's realize resize method, note that this approach is private, after all, can not let users freely resize.
// the space capacity of the array size becomes newCapacity Private void a resize ( int newCapacity) { // create a data array, based on the passed parameter setting array size, and last elements of the original copy of the data array, the last data point to the new array. E [] newData = (E []) new new Object [newCapacity]; for ( int I = 0; I <size; I ++ ) newData [I] = Data [I]; Data = newData; }
We can modify the add and remove methods after method to achieve good resize method
add method, the array is full not throw, but expansion
IF (size == data.length) // expansion to 2 times the size of the original array resize (2 * data.length);
remove method, the array is too small, volume reduction.
// the number of elements equal to a quarter of the length of the array, the array length shrunk to one-half the original IF (data.length size == /. 4 && data.length / 2! = 0 ) a resize (Data. length / 2);