Advertisement
Guest User

What the fuck, over

a guest
Apr 26th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. public class Prog7b {
  2.  
  3. // @SuppressWarnings({ "unchecked", "rawtypes" })
  4. public static <E> void main(String[] args) {
  5.  
  6. for(int i=0;i<args.length;i++)System.out.print(args[i]);
  7. System.out.println();
  8.  
  9. int arraySize = Integer.parseInt(args[1]);
  10. E[] array = (E[])new Object[arraySize];
  11. E key = null;
  12.  
  13.  
  14. if (args[0].matches("I|i")) {
  15. for (int i = 2; i < args.length-1; i++) {
  16. array[i-2]=(E)new Integer(args[i]);
  17. System.out.println(array[i-2]);
  18. }
  19. key = (E) new Integer(args[args.length-1]);
  20. System.out.println("Key is: " + key);
  21.  
  22. }
  23. // else if (args[0].matches("S|s")) {
  24. // for (int i = 2; i < args.length-1; i++) {
  25. // array.add((E) new String(args[i]));
  26. // }
  27. // key = (E) new String(args[args.length-1]);
  28. // }
  29. // else {
  30. // for (int i = 2; i < args.length-1; i++) {
  31. // array.add((E) new Double(args[i]));
  32. // }
  33. // key = (E) new Double(args[args.length-1]);
  34. // System.out.println(key);
  35. // }
  36.  
  37.  
  38. if(linearSearch(array, key)<0){
  39. System.out.println("Didnt find it");
  40. }else System.out.println("Found it: "+(linearSearch(array, key)));
  41.  
  42.  
  43. }
  44.  
  45. public static <E> int linearSearch(E[]array,E key) {
  46. int index=-1;
  47. for(int i=0;i<array.length;i++) {
  48. System.out.println("arrayvalue is: "+array[i]+ " With class type:" + array[i].getClass() + " key is: " +key+" with class type: "+key.getClass());
  49. if(array[i]==key){
  50. index = (int) array[i];
  51. }
  52. } return index;
  53. }
  54. }
  55.  
  56.  
  57. Output:
  58. I412344
  59. 1
  60. 2
  61. 3
  62. 4
  63. Key is: 4
  64. arrayvalue is: 1 With class type:class java.lang.Integer key is: 4 with class type: class java.lang.Integer
  65. arrayvalue is: 2 With class type:class java.lang.Integer key is: 4 with class type: class java.lang.Integer
  66. arrayvalue is: 3 With class type:class java.lang.Integer key is: 4 with class type: class java.lang.Integer
  67. arrayvalue is: 4 With class type:class java.lang.Integer key is: 4 with class type: class java.lang.Integer
  68. Didnt find it
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement