Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Prog7b {
- // @SuppressWarnings({ "unchecked", "rawtypes" })
- public static void main(String[] args) {
- if (args[0].matches("I|i")) {
- Integer[] array = new Integer[Integer.parseInt(args[1])];
- for (int i = 2; i < args.length - 1; i++) {
- array[i - 2] = new Integer(args[i]);
- }
- Integer key = new Integer(args[args.length - 1]);
- if (linearSearch(array, key) < 0) {
- System.out.println("Didnt find it");
- } else
- System.out.println("Found it at index: " + (linearSearch(array, key) - 1));
- System.out.println("The max of the array is: " + max(array));
- print(array);
- } else if (args[0].matches("S|s")) {
- String[] array = new String[Integer.parseInt(args[1])];
- for (int i = 2; i < args.length - 1; i++) {
- array[i - 2] = new String(args[i]);
- }
- String key = new String(args[args.length - 1]);
- if (linearSearch(array, key) < 0) {
- System.out.println("Didnt find it");
- } else
- System.out.println("Found it at index: " + (linearSearch(array, key) - 1));
- System.out.println("The max of the array is: " + max(array));
- print(array);
- } else {
- Double[] array = new Double[Integer.parseInt(args[1])];
- for (int i = 2; i < args.length - 1; i++) {
- array[i - 2] = new Double(args[i]);
- }
- Double key = new Double(args[args.length - 1]);
- if (linearSearch(array, key) < 0) {
- System.out.println("Didnt find it");
- } else
- System.out.println("Found it at index: " + (linearSearch(array, key) - 1));
- System.out.println("The max of the array is: " + max(array));
- print(array);
- }
- }
- public static <E extends Comparable<E>> int linearSearch(E[] array, E key) {
- int index = 0;
- for (int i = 0; i < array.length; i++) {
- index++;
- if (array[i].equals(key)) {
- return index;
- }
- }
- return -1;
- }
- public static <E extends Comparable<E>> E max(E[] list) {
- E max = list[0];
- for (int i = 1; i < list.length; i++) {
- if (max.compareTo(list[i]) < 0) {
- max = list[i];
- }
- }
- return max;
- }
- private static <E> void print(E[] list) {
- System.out.print("[");
- for (int i = 0; i < list.length - 1; i++)
- System.out.print(list[i] + ", ");
- System.out.print(list[list.length - 1] + "]\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement