Created
September 8, 2017 13:25
-
-
Save khayyamsaleem/cd3ddc409c470e56057be89a136f44fa 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
import java.lang.reflect.Array; | |
public class myArr<E> { | |
//inner class that lets you zip a list into pairs | |
public class Pair<F, S> { | |
private F first; | |
private S second; | |
public Pair(F first, S second){ | |
this.first = first; | |
this.second = second; | |
} | |
public F getF(){ | |
return this.first; | |
} | |
public S getS(){ | |
return this.second; | |
} | |
public String toString(){ | |
String out = ""; | |
out += "(" + this.getF() + ", " + this.getS() + ")"; | |
return out; | |
} | |
} | |
public myArr(){} | |
//returns element at given index | |
public E get(E[] data, int index){ | |
return data[index]; | |
} | |
//removes from array at index | |
@SuppressWarnings("unchecked") | |
public E[] take(E[] data, int index){ | |
E[] newData = (E[])new Object[data.length - 1]; | |
//might need a check for if index is greater than data.length | |
for(int i=0; i<index; i++){ | |
newData[i] = data[i]; | |
} | |
for(int i=index; i<data.length-1; i++){ | |
newData[i] = data[i+1]; | |
} | |
return newData; | |
} | |
//inserts element at given index in array | |
@SuppressWarnings("unchecked") | |
public E[] insert(E[] data, int index, E item){ | |
//check if index in range | |
E[] newData = (E[])new Object[data.length+1]; | |
for(int i = 0; i < index; i++){ | |
newData[i] = data[i]; | |
} | |
newData[index] = item; | |
for(int i = index + 1; i < newData.length; i++){ | |
newData[i] = data[i-1]; | |
} | |
return newData; | |
} | |
//zips up two lists by creating pairs | |
// be sure to check that they're the same length! | |
@SuppressWarnings({"unchecked", "rawtypes"}) | |
public Pair<E, E>[] zip(E[] first, E[] second){ | |
Pair<E, E>[] zipped = new Pair[first.length]; | |
for(int i = 0; i < first.length; i++){ | |
zipped[i] = new Pair(first[i], second[i]); | |
} | |
return zipped; | |
} | |
public String toString(E[] data){ | |
String out = ""; | |
out += "["; | |
for(int i = 0; i < data.length - 1; i++){ | |
out += data[i].toString() + ", "; | |
} | |
out += data[data.length-1].toString() + "]"; | |
return out; | |
} | |
@SuppressWarnings({"unchecked", "rawtypes"}) | |
public static void main(String[] args){ | |
myArr a = new myArr(); | |
Object[] A = new Integer[] {1, 2, 3, 4, 5}; | |
Object[] B = new Integer[] {1, 4, 9, 16, 25}; | |
System.out.println("A: " + a.toString(A)); | |
System.out.println("B: " + a.toString(B)); | |
System.out.println("Item at 2nd index of B: " + a.get(B, 2)); | |
System.out.println("Item at 2nd index of A: " + a.get(A, 2)); | |
System.out.println("Zipped lists: "); | |
System.out.println(a.toString(a.zip(A, B))); | |
System.out.println("Removing item at 3rd index of B and A: "); | |
A = a.take(A, 3); | |
B = a.take(B, 3); | |
System.out.println("B: " + a.toString(B)); | |
System.out.println("A: " + a.toString(A)); | |
System.out.println("Inserting items at end of A and B: "); | |
A = a.insert(A, A.length, 6); | |
B = a.insert(B, B.length, 36); | |
System.out.println("B: " + a.toString(B)); | |
System.out.println("A: " + a.toString(A)); | |
System.out.println("Zipping again: "); | |
System.out.println(a.toString(a.zip(A, B))); | |
System.out.println("\n-----------------------\n"); | |
System.out.println("With strings: "); | |
Object[] C = new String[] {"Apple", "Broccoli", "Potato"}; | |
Object[] D = new String[] {"Fruit", "Vegetable", "Root"}; | |
System.out.println("C: " + a.toString(C)); | |
System.out.println("D: " + a.toString(D)); | |
System.out.println("Removing item at 1st index of C and D: "); | |
C = a.take(C, 1); | |
D = a.take(D, 1); | |
System.out.println("C: " + a.toString(C)); | |
System.out.println("D: " + a.toString(D)); | |
System.out.println("Inserting item at 1st index of C and D: "); | |
C = a.insert(C, 1, "Hummus"); | |
D = a.insert(D, 1, "Dip"); | |
System.out.println("C: " + a.toString(C)); | |
System.out.println("D: " + a.toString(D)); | |
System.out.println("Zipped lists: "); | |
System.out.println(a.toString(a.zip(C, D))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment