This abomination of code creates arrays based off input from the user. Sample input would be something like "S 4 aba bab baa aab" where:
args[0]
= array type (Int
,Double
,String
)args[1]
= length of arrayargs[2...]
= contents of the array
The creation of the arrays in my code is horrible, but I am not quite sure how else to do it while incorporating generics. The point of the code is to be able to pass any of the created arrays to the methods at the bottom, hence generics.
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
public class Prog7a<E> {
public static void main(String[] args) throws FileNotFoundException {
checkUsage(args);
//This would be nice, but I get "Cannot make static reference to non-static E"
// ArrayList<E> array = new ArrayList<>();
//
// if (args[0].matches("I|i"))
// for (int i = 2; i < args.length; i++)
// array.add((E) new Integer(args[i]));
//
// else if (args[0].matches("S|s"))
// for (int i = 2; i < args.length; i++)
// array.add((E) new String(args[i]));
// else
// for (int i = 2; i < args.length; i++)
// array.add((E) new Double(args[i]));
//
//
// System.out.println("Original: " + array);
// System.out.println(" Unique: " + removeDuplicates(array));
// shuffle(array);
// System.out.println("Shuffled: " + array);
// System.out.println(" Maximum: " + max(array));
My question is here: I have the loop to fill in the array and call to the methods in each conditional, which makes me think I could rewrite this in such a way that it is not so repetitive.
if (args[0].matches("I|i")) {
ArrayList<Integer> iL = new ArrayList<Integer>();
for (int i = 2; i < args.length; i++) {
iL.add(Integer.parseInt(args[i]));
}
System.out.println("Original: " + iL);
System.out.println(" Unique: " + removeDuplicates(iL));
shuffle(iL);
System.out.println("Shuffled: " + iL);
System.out.println(" Maximum: " + max(iL));
} else if (args[0].matches("S|s")) {
ArrayList<String> sL = new ArrayList<String>();
for (int i = 2; i < args.length; i++) {
sL.add(args[i]);
}
System.out.println("Original: " + sL);
System.out.println(" Unique: " + removeDuplicates(sL));
shuffle(sL);
System.out.println("Shuffled: " + sL);
System.out.println(" Maximum: " + max(sL));
} else {
ArrayList<Double> dL = new ArrayList<Double>();
for (int i = 2; i < args.length; i++) {
dL.add(Double.parseDouble(args[i]));
}
System.out.println("Original: " + dL);
System.out.println(" Unique: " + removeDuplicates(dL));
shuffle(dL);
System.out.println("Shuffled: " + dL);
System.out.println(" Maximum: " + max(dL));
}
}
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
new HashSet<E>(list);
list = new ArrayList<E>(new HashSet<E>(list));
return list;
}
public static <E> void shuffle(ArrayList<E> list) {
Collections.shuffle(list);
}
public static <E extends Comparable<E>> E max(ArrayList<E> list) {
return Collections.max(list);
}
public static void checkUsage(String[] args)
{
if (args.length < 2)
{
System.out.println("Please supply more than 2 arguments");
System.exit(1);
}
if (!(args[0]=="s") && (args[0]=="S") &&
(args[0]=="d") && (args[0]=="D") &&
(args[0]=="i") && (args[0]=="I")){
System.out.println("Incorrect format of command.");
System.exit(1);
}
}
}