Created
December 11, 2016 22:10
-
-
Save FLamparski/a2bc6161e08fc4101a3bc687ff15dc14 to your computer and use it in GitHub Desktop.
I may have just written an abomination
This file contains 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.util.ArrayList; | |
import java.util.Collection; | |
import java.util.HashSet; | |
import java.util.Scanner; | |
public class SetOfLists { | |
private static | |
<COLL_INNER extends Collection<Integer>, COLL_OUTER extends Collection<COLL_INNER>> | |
COLL_OUTER collectionOfCollections(Scanner in, Class<COLL_OUTER> OuterCollection, Class<COLL_INNER> InnerCollection) | |
throws InstantiationException, IllegalAccessException { | |
System.out.println("Enter one sequence of ints per line to add to the list (space separated; enter to finish line; empty to finish input)"); | |
COLL_OUTER sequences = OuterCollection.newInstance(); | |
String input; | |
while (!(input = in.nextLine().trim()).isEmpty()) { | |
String[] inputs = input.split("\\s+"); | |
COLL_INNER curSequence = InnerCollection.newInstance(); | |
for (String numstr : inputs) { | |
int num = Integer.parseInt(numstr); | |
curSequence.add(num); | |
} | |
sequences.add(curSequence); | |
} | |
return sequences; | |
} | |
public static void main(String[] args) throws InstantiationException, IllegalAccessException { | |
Scanner in = new Scanner(System.in); | |
System.out.print("[S]etOfLists or [L]istOfSets? "); | |
String decision = in.nextLine(); | |
Collection<Collection<Integer>> outer; | |
switch (decision.toCharArray()[0]) { | |
case 'S': | |
outer = collectionOfCollections(in, HashSet.class, ArrayList.class); | |
break; | |
case 'L': | |
outer = collectionOfCollections(in, ArrayList.class, HashSet.class); | |
break; | |
default: | |
System.out.println("Input not recognised."); | |
System.exit(1); | |
return; | |
} | |
for (Collection inner : outer) { | |
System.out.println(inner.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment