2017-03-07 3 views
1

Es gibt zwei int-Arrays mit derselben Größe. Ich möchte die gemeinsamen Elemente finden und ausdrucken.Fehler "ArrayIndexOutOfBounds" beim Versuch, die gemeinsamen Elemente von 2 angegebenen Arrays zu finden

public class Main { 

    public static void main(String[] args) { 

    int[] arrayOne = new int[] {1, 4, 5, 324, 67, 0, -2}; 
    int[] arrayTwo = new int[] {1, 45, 23, -2, 543, 3, 0}; 

    for (int i = 0; i < arrayOne.length; i++) { 
     for (int j = i; i < arrayTwo.length; j++) { 

     if (arrayOne[i] == arrayTwo[j]) { 
      System.out.println(arrayOne[i]); 
     } 
     } 
    } 
    } 
} 

so dass der Code should print 1 , -2and 0 in diesem Fall. Stattdessen erhalte ich eine Ausnahme und das Programm druckt 1.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 bei Main.main (Main.java:14) bei sun.reflect.NativeMethodAccessorImpl.invoke0 (native Methode) bei sun.reflect. NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) bei sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) bei java.lang.reflect.Method.invoke (Method.java:498) bei com.intellij .rt.execution.application.AppMain.main (AppMain.java:147) 1

Prozess beenden ed mit Exit-Code 1

+4

Sie einen Typ hier bekam - 'for (int j = i; i Eran

+0

Ohhh. Aber diesmal druckt es nicht -2. –

+1

Set initial 'int j = 0' – vikingmaster

Antwort

4

Es

for (int j = 0; j < arrayTwo.length; j++) 

Statt

for (int j = i; i < arrayTwo.length; j++) 

Bessere Wege-Datenstruktur zu verwenden sein sollten - Set

Set<Integer> s1 = new HashSet<>(Arrays.asList(array1)); 
Set<Integer> s2= new HashSet<>(Arrays.asList(array2)); 

s1.retainAll(s2); 

System.out.println(s1); 
+1

habe ich den Tippfehler korrigiert. Es druckt jedoch nur 1 und 0. Es wird nicht gedruckt -2 –

+0

@ Egek92 behoben. Als der Wikingermeister auf einen kleinen Fehler hinwies ... willst du, dass ich zeige, wie man hier festgelegte Datenstrukturen benutzt? – minigeek

0

diese Schleife nachprüfen: for (int j = i; i < arrayTwo.length; j++)

1

Sie haben falsch zweite Schleife

public static void main(String[] args) { 
    int[] arrayOne = new int[] {1, 4, 5, 324, 67, 0, -2}; 
    int[] arrayTwo = new int[] {1, 45, 23, -2, 543, 3, 0}; 

    for (int i = 0; i < arrayOne.length; i++) { 
     for (int j = 0; j < arrayTwo.length; j++) { 

     if (arrayOne[i] == arrayTwo[j]) { 
      System.out.println(arrayOne[i]); 
     } 
     } 
    } 

} 
0

public class Tests {

public static void main(String[] args) 
{ 

    int[] arrayOne = new int[] 
    { 1, 4, 5, 324, 67, 0, -2 }; 
    int[] arrayTwo = new int[] 
    { 1, 45, 23, -2, 543, 3, 0 }; 

    for (int i = 0; i < arrayOne.length; i++) 
    { 
     for (int j = 0; j < arrayTwo.length; j++) 
     { 

      if (arrayOne[i] == arrayTwo[j]) 
      { 
       System.out.println(arrayOne[i]); 
      } 
     } 
    } 
} 

}

4

Alternative Lösung, die Sie könnten versuchen wollen:

Set<Integer> set1 = new HashSet<>(Arrays.asList(array1)); 
Set<Integer> set2 = new HashSet<>(Arrays.asList(array2)); 
set1.retainAll(set2); 

// Prints result using enhanced for loop 
for (int num : set1) { 
    System.out.println(num); 
} 

// Prints result using forEach 
set1.forEach(System.out::println); 
1

Das Problem ist diese zweite Schleife sho Uld Iterate nach dem zweiten Array und nicht den Index der ersten Schleife. Und Sie sollten/foreach für eine bessere Leistung verwenden.

int[] arrayOne = new int[] {1, 4, 5, 324, 67, 0, -2}; 
     int[] arrayTwo = new int[] {1, 45, 23, -2, 543, 3, 0}; 

     for (int anArrayOne : arrayOne) { 
      for (int anArrayTwo : arrayTwo) { 
       if (anArrayOne == anArrayTwo) { 
        System.out.println(anArrayOne); 
       } 
      } 
     } 
1

Statt ab i th index Start von 0th index

und prüfen, ob j in Bedingung nicht i.

wie dies

for (int j = 0; j < arrayTwo.length; j++) 
2

Ich werde Sie auch eine Lösung, die nicht die besten sein könnte zu verwenden, aber Java8 mit Bächen, die Lösung zu machen nur ein „fancy“ one-liner.

Arrays.stream(arrayOne) //Stream out of array 
      .forEach(e -> Arrays.stream(arrayTwo) //iterate through elements 
        .filter(ele -> ele == e) //get only elements from array 2 that are equal to the one we are at in array 1 
        .findFirst() //try to find first element of all that we got from array 2 after filtering 
        .ifPresent(System.out::println)); //if that findFirst() found an element (means that array 2 did contain the int), print it out 
1

kam ich mit dieser Lösung bis basierend auf Oleksandr Antwort:

int[] arrayOne = new int[] {1, 4, 5, 324, 67, 0, -2}; 
int[] arrayTwo = new int[] {1, 45, 23, -2, 543, 3, 0}; 

HashSet<int[]> set1 = new HashSet<>(Arrays.asList(arrayOne)); 
HashSet<int[]> set2 = new HashSet<>(Arrays.asList(arrayTwo)); 
set1.retainAll(set2); 

System.out.println(Arrays.toString(set2.toArray())) 
Verwandte Themen