Created
September 8, 2017 13:27
-
-
Save khayyamsaleem/c4089edcfde67216d7bef7d9d8748b8f 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
public class myArrTemplate<E>{ | |
//inner class that lets you zip a list into pairs | |
public static 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; | |
} | |
} | |
//empty constructor | |
public myArrTemplate(){ | |
} | |
//returns element at given index | |
public E get(E[] data, int index){ | |
//TODO: implement get | |
//make sure to check for bad input | |
} | |
//removes from array at index | |
@SuppressWarnings("unchecked") | |
public E[] take(E[] data, int index){ | |
E[] newData = (E[])new Object[data.length-1]; | |
//TODO: implement remove at index, make sure to check for bad input | |
} | |
//inserts element at given index in array | |
@SuppressWarnings("unchecked") | |
public E[] insert(E[] data, int index, E item){ | |
E[] newData = (E[])new Object[data.length+1]; | |
//TODO: implement insert, make sure to check for bad input | |
} | |
//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]; | |
//TODO: implement zip, make sure to check if both are the same length!! | |
} | |
//creates representation of array | |
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){ | |
myArrTemplate a = new myArrTemplate(); | |
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