Generic arrays Flashcards

1
Q

MyArrayList start

A

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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

MyArrayList add

A

public void add(int index, E e){
for(int i = size-1;i>=index;i–){
data[i+1] = data[i];
}
data[index] = e;
size++;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

MyArrayList contain

A

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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

MyArrayList remove

A

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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

MArrayList toString

A

public String toString(){
String result = “[”;
for(int i = 0;i<size;i++){
result += data[i];
if(i<size-1){
result +=”, “;
}
else {
result += “]”;
}
}
return result;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

MyArrayList sortList

A

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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

MyArrayList clear

A

public void clear(){
size = 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

MyArrayList filter

A

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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

MyArrayList merge

A

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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

big O definition

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Reasons for generic arrays

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Types of data

A

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.
*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Actions on an array

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What must all classes have in order to compare values?

A

Implementation fo comparable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly