fork(4) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. int[] massivIntov = {1,2,3};
  10. int elementInt = 2;
  11. System.out.println(najtiElementVMassive(massivIntov,elementInt));
  12.  
  13. double[] massivDoublov = {1,2,3};
  14. double elementDouble = 2;
  15. System.out.println(najtiElementVMassive(massivDoublov,elementDouble));
  16.  
  17. String[] massivStringov = {"odin","dva","tri"};
  18. String elementString = "dva";
  19. System.out.println(najtiElementVMassive(massivStringov,elementString));
  20.  
  21. Integer[] massivIntegerov = {new Integer (1001),new Integer (1002),new Integer (1003)};
  22. Integer elementInteger = new Integer (1002);
  23. System.out.println(najtiElementVMassive(massivIntegerov,elementInteger));
  24.  
  25. // !!!
  26. int[][] massivMassivIntov = {{1,2,3},{2,3,4},{3,4,5}};
  27. int elementMassivIntov[] = {2,3,4};
  28. System.out.println(najtiElementVMassive(massivMassivIntov,elementMassivIntov));
  29.  
  30. String[][][] massivMassivMassivStringov = {{{"a"},{"b"}},{{"c","d"}},{{"e","f"}}};
  31. String[][] elementMassivMassivStringov = {{"c","d"}};
  32. System.out.println(najtiElementVMassive(massivMassivMassivStringov,elementMassivMassivStringov));
  33.  
  34. String[][] oshibochniiElementMassivMassivStringov = {{"c","e","z"}};
  35. System.out.println(najtiElementVMassive(massivMassivMassivStringov,oshibochniiElementMassivMassivStringov));
  36. }
  37.  
  38.  
  39.  
  40. // -1 esli NotFound, snachala massiv potom element
  41. static int najtiElementVMassive(Object massiv,Object element){
  42. if(massiv instanceof int[]) {
  43. for(int i=0; i<((int[])massiv).length; ++i)
  44. if(((int[])massiv)[i]==(int)element)
  45. return i;
  46. } else if(massiv instanceof byte[]) {
  47. for(int i=0; i<((byte[])massiv).length; ++i)
  48. if(((byte[])massiv)[i]==(byte)element)
  49. return i;
  50. } else if(massiv instanceof boolean[]) {
  51. for(int i=0; i<((boolean[])massiv).length; ++i)
  52. if(((boolean[])massiv)[i]==(boolean)element)
  53. return i;
  54. } else if(massiv instanceof char[]) {
  55. for(int i=0; i<((char[])massiv).length; ++i)
  56. if(((char[])massiv)[i]==(char)element)
  57. return i;
  58. } else if(massiv instanceof float[]) {
  59. for(int i=0; i<((float[])massiv).length; ++i)
  60. if(((float[])massiv)[i]==(float)element)
  61. return i;
  62. } else if(massiv instanceof double[]) {
  63. for(int i=0; i<((double[])massiv).length; ++i)
  64. if(((double[])massiv)[i]==(double)element)
  65. return i;
  66. } else if(massiv instanceof short[]) {
  67. for(int i=0; i<((short[])massiv).length; ++i)
  68. if(((short[])massiv)[i]==(short)element)
  69. return i;
  70. } else if(massiv instanceof long[]) {
  71. for(int i=0; i<((long[])massiv).length; ++i)
  72. if(((long[])massiv)[i]==(long)element)
  73. return i;
  74. } else {
  75. try {
  76. for(int i=0; i<((Object[])massiv).length; ++i)
  77. if(sravnitMassivi(((Object[])massiv)[i],element))
  78. return i;
  79. } catch (Exception e) {
  80. for(int i=0; i<((Object[])massiv).length; ++i)
  81. if((((Object[])massiv)[i]).equals(element))
  82. return i;
  83. }
  84. }
  85.  
  86. return -1;
  87. }
  88.  
  89.  
  90. static boolean sravnitMassivi(Object massiv1,Object massiv2) {
  91. try {
  92. if((((Object[])massiv1)).length!=(((Object[])massiv2)).length) return false;
  93. boolean ravni=true;
  94. for(int i=0; i<(((Object[])massiv1)).length; ++i)
  95. ravni=ravni&&sravnitMassivi((((Object[])massiv1))[i],(((Object[])massiv2))[i]);
  96. return ravni;
  97. } catch (Exception e) {
  98. if(massiv1 instanceof int[]) {
  99. return Arrays.equals((int[])massiv1,(int[])massiv2);
  100. } else if(massiv1 instanceof byte[]) {
  101. return Arrays.equals((byte[])massiv1,(byte[])massiv2);
  102. } else if(massiv1 instanceof boolean[]) {
  103. return Arrays.equals((boolean[])massiv1,(boolean[])massiv2);
  104. } else if(massiv1 instanceof char[]) {
  105. return Arrays.equals((char[])massiv1,(char[])massiv2);
  106. } else if(massiv1 instanceof float[]) {
  107. return Arrays.equals((float[])massiv1,(float[])massiv2);
  108. } else if(massiv1 instanceof double[]) {
  109. return Arrays.equals((double[])massiv1,(double[])massiv2);
  110. } else if(massiv1 instanceof short[]) {
  111. return Arrays.equals((short[])massiv1,(short[])massiv2);
  112. } else if(massiv1 instanceof long[]) {
  113. return Arrays.equals((long[])massiv1,(long[])massiv2);
  114. } else {
  115. return massiv1.equals(massiv2);
  116. }
  117. }
  118. }
  119. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
1
1
1
1
1
1
-1