Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Prog7b {
- // @SuppressWarnings({ "unchecked", "rawtypes" })
- public static <E> void main(String[] args) {
- for(int i=0;i<args.length;i++)System.out.print(args[i]);
- System.out.println();
- int arraySize = Integer.parseInt(args[1]);
- E[] array = (E[])new Object[arraySize];
- E key = null;
- if (args[0].matches("I|i")) {
- for (int i = 2; i < args.length-1; i++) {
- array[i-2]=(E)new Integer(args[i]);
- System.out.println(array[i-2]);
- }
- key = (E) new Integer(args[args.length-1]);
- System.out.println("Key is: " + key);
- }
- // else if (args[0].matches("S|s")) {
- // for (int i = 2; i < args.length-1; i++) {
- // array.add((E) new String(args[i]));
- // }
- // key = (E) new String(args[args.length-1]);
- // }
- // else {
- // for (int i = 2; i < args.length-1; i++) {
- // array.add((E) new Double(args[i]));
- // }
- // key = (E) new Double(args[args.length-1]);
- // System.out.println(key);
- // }
- if(linearSearch(array, key)<0){
- System.out.println("Didnt find it");
- }else System.out.println("Found it: "+(linearSearch(array, key)));
- }
- public static <E> int linearSearch(E[]array,E key) {
- int index=-1;
- for(int i=0;i<array.length;i++) {
- System.out.println("arrayvalue is: "+array[i]+ " With class type:" + array[i].getClass() + " key is: " +key+" with class type: "+key.getClass());
- if(array[i]==key){
- index = (int) array[i];
- }
- } return index;
- }
- }
- Output:
- I412344
- 1
- 2
- 3
- 4
- Key is: 4
- arrayvalue is: 1 With class type:class java.lang.Integer key is: 4 with class type: class java.lang.Integer
- arrayvalue is: 2 With class type:class java.lang.Integer key is: 4 with class type: class java.lang.Integer
- arrayvalue is: 3 With class type:class java.lang.Integer key is: 4 with class type: class java.lang.Integer
- arrayvalue is: 4 With class type:class java.lang.Integer key is: 4 with class type: class java.lang.Integer
- Didnt find it
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement