Created
September 8, 2017 15:43
-
-
Save khayyamsaleem/93dc6c717f641faf2a8881303203df09 to your computer and use it in GitHub Desktop.
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
package rc2; | |
/** | |
* | |
* @author khayyamsaleem A class that creates an interface for an array | |
* @param <E> | |
* generic parameter | |
*/ | |
public class MyArray<E> { | |
private E[] data; | |
private int length; | |
private int cur = 0; | |
private static class Node<E> { | |
E data; | |
int num; | |
public Node(E data, int num) { | |
this.data = data; | |
this.num = num; | |
} | |
public String toString() { | |
return "(" + data.toString() + ", " + num + ")"; | |
} | |
} | |
/** | |
* sets cur to first null spot in array | |
*/ | |
private void setCur() { | |
for (int i = 0; i < this.length; i++) { | |
if (this.data[i] == null) { | |
this.cur = i; | |
break; | |
} | |
} | |
} | |
/** | |
* Constructor for the MyArray class | |
* | |
* @param length | |
* the fixed size of the array | |
*/ | |
@SuppressWarnings("unchecked") | |
public MyArray(int length) { | |
this.length = length; | |
this.data = (E[]) new Object[length]; | |
} | |
/** | |
* a get method for our array | |
* | |
* @param index | |
* index of item to retrieve | |
* @return item at given index | |
*/ | |
public E get(int index) { | |
if (index < 0 || index >= this.length) { | |
throw new IndexOutOfBoundsException(); | |
} | |
return this.data[index]; | |
} | |
/** | |
* adds an element to our array | |
* | |
* @param elem | |
* element to add | |
*/ | |
public void add(E elem) { | |
if (this.cur >= this.length) { | |
throw new IndexOutOfBoundsException(); | |
} | |
this.data[cur] = elem; | |
this.setCur(); | |
} | |
/** | |
* overloads other add function, allows to add at index | |
* | |
* @param elem | |
* element to add | |
* @param index | |
* index at which to add | |
*/ | |
public void add(E elem, int index) { | |
if (index < 0 || index >= this.length) { | |
throw new IllegalArgumentException(); | |
} | |
if (this.cur == this.length) { | |
System.out.println("Array overflow!"); | |
throw new IllegalArgumentException(); | |
} | |
if (this.data[index] != null) { | |
for (int i = index + 1; i < this.length; i++) { | |
this.data[i] = this.data[i - 1]; | |
} | |
} | |
this.setCur(); | |
this.data[index] = elem; | |
} | |
/** | |
* removes item at given index | |
* | |
* @param index | |
* index at which to remove item | |
* @return item that was removed | |
*/ | |
public E remove(int index) { | |
if (index < 0 || index >= this.length) { | |
throw new IllegalArgumentException(); | |
} | |
E item = this.data[index]; | |
if (index == this.length - 1) { | |
this.data[index] = null; | |
return item; | |
} | |
for (int i = index; i < this.length - 1; i++) { | |
this.data[i] = this.data[i + 1]; | |
} | |
this.setCur(); | |
return item; | |
} | |
/** | |
* toString function for our array | |
* | |
* @return string representation of the array | |
*/ | |
public String toString() { | |
String out = ""; | |
out += "[ "; | |
for (int i = 0; i < this.length; i++) { | |
if (this.get(i) == null) { | |
out += "null "; | |
} else { | |
out += this.get(i) + " "; | |
} | |
} | |
out += "]"; | |
return out; | |
} | |
public static void main(String[] args) { | |
MyArray<String> s = new MyArray<String>(5); | |
s.add("Hello"); | |
s.add("Shalom"); | |
s.add("Hola"); | |
s.add("Bonjour"); | |
System.out.println(s); | |
s.remove(1); | |
System.out.println(s); | |
s.add("Salaam", 4); | |
System.out.println(s); | |
s.add("Namaste"); | |
System.out.println(s); | |
MyArray<Node<Boolean>> evens = new MyArray<Node<Boolean>>(10); | |
for (int i = 0; i < 10; i++) { | |
evens.add(new Node<Boolean>(i % 2 == 0, i)); | |
} | |
System.out.println(evens); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment