- 001
- 002
- 003
- 004
- 005
- 006
- 007
- 008
- 009
- 010
- 011
- 012
- 013
- 014
- 015
- 016
- 017
- 018
- 019
- 020
- 021
- 022
- 023
- 024
- 025
- 026
- 027
- 028
- 029
- 030
- 031
- 032
- 033
- 034
- 035
- 036
- 037
- 038
- 039
- 040
- 041
- 042
- 043
- 044
- 045
- 046
- 047
- 048
- 049
- 050
- 051
- 052
- 053
- 054
- 055
- 056
- 057
- 058
- 059
- 060
- 061
- 062
- 063
- 064
- 065
- 066
- 067
- 068
- 069
- 070
- 071
- 072
- 073
- 074
- 075
- 076
- 077
- 078
- 079
- 080
- 081
- 082
- 083
- 084
- 085
- 086
- 087
- 088
- 089
- 090
- 091
- 092
- 093
- 094
- 095
- 096
- 097
- 098
- 099
- 100
public static void main (String[] args) throws java.lang.Exception
{
int[] massivIntov = {1,2,3};
int elementInt = 2;
System.out.println(najtiElementVMassive(massivIntov,elementInt));
double[] massivDoublov = {1,2,3};
double elementDouble = 2;
System.out.println(najtiElementVMassive(massivDoublov,elementDouble));
String[] massivStringov = {"odin","dva","tri"};
String elementString = "dva";
System.out.println(najtiElementVMassive(massivStringov,elementString));
Integer[] massivIntegerov = {new Integer (1001),new Integer (1002),new Integer (1003)};
Integer elementInteger = new Integer (1002);
System.out.println(najtiElementVMassive(massivIntegerov,elementInteger));
// !!!
int[][] massivMassivIntov = {{1,2,3},{2,3,4},{3,4,5}};
int elementMassivIntov[] = {2,3,4};
System.out.println(najtiElementVMassive(massivMassivIntov,elementMassivIntov));
String[][][] massivMassivMassivStringov = {{{"a"},{"b"}},{{"c","d"}},{{"e","f"}}};
String[][] elementMassivMassivStringov = {{"c","d"}};
System.out.println(najtiElementVMassive(massivMassivMassivStringov,elementMassivMassivStringov));
String[][] oshibochniiElementMassivMassivStringov = {{"c","e","z"}};
System.out.println(najtiElementVMassive(massivMassivMassivStringov,oshibochniiElementMassivMassivStringov));
}
// -1 esli NotFound, snachala massiv potom element
static int najtiElementVMassive(Object massiv,Object element){
if(massiv instanceof int[]) {
for(int i=0; i<((int[])massiv).length; ++i)
if(((int[])massiv)[i]==(int)element)
return i;
} else if(massiv instanceof byte[]) {
for(int i=0; i<((byte[])massiv).length; ++i)
if(((byte[])massiv)[i]==(byte)element)
return i;
} else if(massiv instanceof boolean[]) {
for(int i=0; i<((boolean[])massiv).length; ++i)
if(((boolean[])massiv)[i]==(boolean)element)
return i;
} else if(massiv instanceof char[]) {
for(int i=0; i<((char[])massiv).length; ++i)
if(((char[])massiv)[i]==(char)element)
return i;
} else if(massiv instanceof float[]) {
for(int i=0; i<((float[])massiv).length; ++i)
if(((float[])massiv)[i]==(float)element)
return i;
} else if(massiv instanceof double[]) {
for(int i=0; i<((double[])massiv).length; ++i)
if(((double[])massiv)[i]==(double)element)
return i;
} else if(massiv instanceof short[]) {
for(int i=0; i<((short[])massiv).length; ++i)
if(((short[])massiv)[i]==(short)element)
return i;
} else if(massiv instanceof long[]) {
for(int i=0; i<((long[])massiv).length; ++i)
if(((long[])massiv)[i]==(long)element)
return i;
} else {
try {
for(int i=0; i<((Object[])massiv).length; ++i)
if(sravnitMassivi(((Object[])massiv)[i],element))
return i;
} catch (Exception e) {
for(int i=0; i<((Object[])massiv).length; ++i)
if((((Object[])massiv)[i]).equals(element))
return i;
}
}
return -1;
}
static boolean sravnitMassivi(Object massiv1,Object massiv2) {
try {
if((((Object[])massiv1)).length!=(((Object[])massiv1)).length) return false;
boolean ravni=true;
for(int i=0; i<(((Object[])massiv1)).length; ++i)
ravni=ravni&&sravnitMassivi((((Object[])massiv1))[i],(((Object[])massiv2))[i]);
return ravni;
} catch (Exception e) {
if(massiv1 instanceof int[]) {
return Arrays.equals((int[])massiv1,(int[])massiv2);
} else if(massiv1 instanceof byte[]) {
return Arrays.equals((byte[])massiv1,(byte[])massiv2);
} else if(massiv1 instanceof boolean[]) {
return Arrays.equals((boolean[])massiv1,(boolean[])massiv2);
} else if(massiv1 instanceof char[]) {
return Arrays.equals((char[])massiv1,(char[])massiv2);
} else if(massiv1 instanceof float[]) {