Generic arrays Flashcards
MyArrayList start
public class MyArrayList<E> {
private int size; //number of elements in the list
private E[] data;</E>
public MyArrayList(){ data = (E[])new Object[100]; size = 0; }
MyArrayList add
public void add(int index, E e){
for(int i = size-1;i>=index;i–){
data[i+1] = data[i];
}
data[index] = e;
size++;
}
MyArrayList contain
public boolean Contains(E e){
boolean flag = true;
for(int i = 0;i<size;i++){
if(e.equals(data[i])){
flag = true;
}
else{
flag = false;
}
}
return flag;
}
MyArrayList remove
public E remove(int index){
E e = data[index];
for(int i = index;i<size-1;i++){
data[i] = data[i+1];
}
size–;
return e;
}
MArrayList toString
public String toString(){
String result = “[”;
for(int i = 0;i<size;i++){
result += data[i];
if(i<size-1){
result +=”, “;
}
else {
result += “]”;
}
}
return result;
}
}
MyArrayList sortList
public boolean sortList(){
E hold;
for(int i = 0;i<size-1;i++){
for(int j = 0;j<size-1;j++){
if(((Comparable)data[j]).compareTo(data[j+1])>0){
hold = data[j+1];
data[j+1] = data[j];
data[j] = hold;
}
}
}
return true;
}
MyArrayList clear
public void clear(){
size = 0;
}
MyArrayList filter
public void filter(E low, E high){
int j = 0;
E[] temp = (E[])new Object[MAX_ELEMENTS];
if(getSize()==0){ return; } if(((Comparable)low).compareTo(high)>0){ return; } for(int i = 0;i<size;i++){ if((((Comparable)data[i]).compareTo(low))>=0 && ((Comparable)data[i]).compareTo(high)<=0){ temp[j] = data[i]; j++; } } data = temp; size = j; }
MyArrayList merge
public MyArrayList<E> Merge(MyArrayList<E> param){
int i = 0;
int j = 0;
int k = 0;
MyArrayList<E> returnArray = new MyArrayList<E>();</E></E></E></E>
if(this.getSize() == 0){ return param; } if(param.getSize() == 0){ return this; } while(i<this.getSize() && j<param.getSize()){ if(((Comparable)data[i]).compareTo(param.data[j])<0){ returnArray.data[k] = this.data[i]; k++; i++; } else { returnArray.data[k] = param.data[j]; k++; j++; } } if(i<this.getSize()){ for(i = i;i<this.getSize();i++){ returnArray.data[k] = this.data[i]; k++; } } if(j<param.getSize()){ for(j = j;j<param.getSize();j++){ returnArray.data[k] = param.data[j]; k++; } } returnArray.size = k; return returnArray; }
big O definition
Definition of Big Oh
Consider a function f(n) that is non-negative for all inters n>=0. We say
that “f(n) is big oh g(n)” which we write f(n) = O(g(n)), if there exists an
integer n 0 and a constant c> 0 so that for all integers n>=n 0,
f(n) <= c g(n)
Reasons for generic arrays
Next level of programming - programs to be used by other programmers not
by the users of computers.
* These classes you are developing are aimed to be used by other Java
programmers to decrease their workload – re-usability and productivity.
Types of data
Most simple data structure => single variable – stores data according to its type.
More sophisticated => object – stores data according to the definition of the class.
More functional =>array – stores a list of data of a specific type or class.
Better data structure => generic array – then it is a list of objects from any class.
*
Actions on an array
What algorithms / actions can we perform on an array?
* Retrieve an element from the list.
* Insert a new element into the list.
* Delete an element from the list.
* Count elements in the list.
* Determine whether an element is in the list.
* Check whether the list is empty.
What must all classes have in order to compare values?
Implementation fo comparable.