Skip to content

Instantly share code, notes, and snippets.

@khayyamsaleem
Created September 8, 2017 15:40
Show Gist options
  • Save khayyamsaleem/3799638052da5d9f46b634d9d04799d5 to your computer and use it in GitHub Desktop.
Save khayyamsaleem/3799638052da5d9f46b634d9d04799d5 to your computer and use it in GitHub Desktop.
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 + ")";
}
}
/**
* 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;
for(int i = 0; i < this.length; i++) {
if (this.data[i] == null) {
this.cur = i;
break;
}
}
}
/**
* 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];
}
}
for(int i = 0; i < this.length; i++) {
if (this.data[i] == null) {
this.cur = i;
break;
}
}
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];
}
for(int i = 0; i < this.length; i++) {
if (this.data[i] == null) {
this.cur = i;
break;
}
}
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