Created
April 7, 2014 00:28
-
-
Save Karasiq/10013067 to your computer and use it in GitHub Desktop.
Поиск элемента в массиве
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
class Ideone | |
{ | |
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[]) { | |
return Arrays.equals((float[])massiv1,(float[])massiv2); | |
} else if(massiv1 instanceof double[]) { | |
return Arrays.equals((double[])massiv1,(double[])massiv2); | |
} else if(massiv1 instanceof short[]) { | |
return Arrays.equals((short[])massiv1,(short[])massiv2); | |
} else if(massiv1 instanceof long[]) { | |
return Arrays.equals((long[])massiv1,(long[])massiv2); | |
} else { | |
return massiv1.equals(massiv2); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment