Created
October 19, 2012 16:33
-
-
Save zwilias/3919207 to your computer and use it in GitHub Desktop.
Vector class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Assignments { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// create the vector | |
Vector v = new Vector(200); | |
// add 1 to 100 to the vector | |
for(int i=0; i<100; i++) { | |
v.addLast(i+1); | |
} | |
// get the size of the vector | |
System.out.println("Size = " + v.size()); | |
// check for 6 and 101 | |
System.out.println("Contains 6 = " + v.contains(6)); | |
System.out.println("Contains 101 = " + v.contains(101)); | |
// get the first element | |
System.out.println("First element = " + v.getFirst()); | |
// get the last element | |
System.out.println("Last element = " + v.getLast()); | |
// print all contents of vector | |
for(int i=0; i<v.size(); i++) { | |
System.out.print(v.get(i) + " "); | |
} | |
System.out.println(""); | |
// add an element to the beginning of the vector | |
v.addFirst(666); | |
// print all contents of vector | |
for(int i=0; i<v.size(); i++) { | |
System.out.print(v.get(i) + " "); | |
} | |
System.out.println(""); | |
// remove the first element | |
v.removeFirst(); | |
// print all contents of vector | |
for(int i=0; i<v.size(); i++) { | |
System.out.print(v.get(i) + " "); | |
} | |
System.out.println(""); | |
// remove the last element | |
v.removeLast(); | |
// print all contents of vector | |
for(int i=0; i<v.size(); i++) { | |
System.out.print(v.get(i) + " "); | |
} | |
System.out.println(""); | |
// reverse the vector | |
v.reverse(); | |
// print all contents of vector | |
for(int i=0; i<v.size(); i++) { | |
System.out.print(v.get(i) + " "); | |
} | |
System.out.println(""); | |
// duplicate & double | |
v.duplicate(); | |
// print all contents of v | |
for(int i=0; i<v.size(); i++) { | |
System.out.print(v.get(i) + " "); | |
} | |
System.out.println(""); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Vector { | |
protected Object data[]; | |
protected int count; | |
// create a vector with a fixed capacity | |
public Vector(int capacity) { | |
data = new Object[capacity]; | |
count = 0; | |
} | |
public Vector duplicate() { | |
Vector result = new Vector(count*2); | |
for (int i = 0; i < count; i++) { | |
result.addLast(data[i]); | |
result.addLast(data[i]); | |
} | |
return result; | |
} | |
// read the data from an element | |
public Object get(int index) { | |
return data[index]; | |
} | |
// get the first element of the vector | |
public Object getFirst() { | |
return data[0]; | |
} | |
// get the last element of the vector | |
public Object getLast() { | |
return data[count-1]; | |
} | |
// add an element to the beginning of the vector | |
public void addFirst(Object item) { | |
for(int i=count; i>0; i--) { | |
data[i] = data[i-1]; | |
} | |
data[0] = item; | |
count++; | |
} | |
// add an element to the end of the vector | |
public void addLast(Object o) { | |
data[count] = o; | |
count++; | |
} | |
// overwrite an element | |
public void set(int index, Object obj) { | |
data[index] = obj; | |
} | |
// return size of vector | |
public int size() { | |
return count; | |
} | |
// check if vector is empty | |
public boolean isEmpty() { | |
return size() == 0; | |
} | |
// check if vector contains element | |
public boolean contains(Object obj) { | |
for(int i=0; i<count; i++) { | |
if (data[i].equals(obj)) return true; | |
} | |
return false; | |
} | |
// quickly search for a value within the vector | |
public boolean binarySearch(Object key) { | |
int start = 0; | |
int end = count - 1; | |
while(start <= end) { | |
int middle = (start + end + 1) / 2; | |
if(key < data[middle]) end = middle - 1; | |
else if(key > data[middle]) start = middle + 1; | |
else return true; | |
} | |
return false; | |
} | |
// remove the first element | |
public void removeFirst() { | |
data[0] = null; | |
for(int i=0; i<count; i++) { | |
data[i] = data[i+1]; | |
} | |
count--; | |
} | |
// remove the last element | |
public void removeLast() { | |
data[count-1] = null; | |
count--; | |
} | |
// reverse the vector, using data[count] as swap element | |
public void reverse() { | |
for(int i=0; i<(count/2); i++) { | |
data[count] = data[i]; | |
data[i] = data[count-i-1]; | |
data[count-i-1] = data[count]; | |
} | |
data[count] = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment