ArrayIndexOutOfBoundsException

morgens6

Neues Mitglied
Hallo,

wollte insertionSort üben, indem ich einen 1d. Array in der Methode sortiere und in einem 2d. Array die Zwischenschritte bis zum Ergebnis speichere. Leider klappt's bei der Ausgabe nicht ganz.

Java:
    public static void main(String[] args) {
        int t[] = {4, 1, 8, 9, 6, 5};
        int sortSteps[][] = insertionSort(t);
        for(int i = 0; i  < sortSteps.length; i++) {
            for(int j = 0; i < sortSteps.length; j++) {
                System.out.println(sortSteps[i][j]);
            }
        }
    }
  
    public static int [][] insertionSort(int [] a){
      
        if(a == null) return null;
        if(a.length == 0) return new int [0][0];
        int result [][] = new int [a.length] [a.length];
        result [0] = a.clone();
      
        for(int i = 1; i < a.length; i++) {
            int index = i;
            int value = a[i];
            while(index > 0 && a[index-1] > a[index]) {
                a[index] = a[index-1];
                a[index-1] = value;
                index--;
            }
            result[i] = a.clone();
        }
        return result;
    }
}

Ausgabe:
4
1
8
9Exception in thread "main"
6
5
java.lang.ArrayIndexOutOfBoundsException: 6 at test.test.main(test.java:10)

Ich verstehe die Fehlermeldung, nur sehe ich den Fehler leider nicht. Ich hoffe mir kann jemand auf die Sprünge helfen.

Grüße
 

morgens6

Neues Mitglied
Gut, habe jetzt den dummen Fehler selber entdeckt. Falls dieser Thread jemals irgendwen interessieren sollte:
In der 2. for-Schleife der Main-Methode habe ich ausversehen "i < sortSteps.length" statt "j < sortSteps.length" getippt.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
F TableModelListener: java.lang.ArrayIndexOutOfBoundsException: 132 Java Basics - Anfänger-Themen 3
Kumora ArrayIndexOutOfBoundsException bei einem Sortierverfahren Java Basics - Anfänger-Themen 2
F Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11 at main.main(main.java:11) Java Basics - Anfänger-Themen 2
B ArrayIndexOutOfBoundsException, ich finde den Fehler nicht? Java Basics - Anfänger-Themen 10
J Problem mit "ArrayIndexOutOfBoundsException" Java Basics - Anfänger-Themen 11
JaVaN0oB java.lang.ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 18
R Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 5
F ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 2
I Compiler-Fehler Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 Java Basics - Anfänger-Themen 3
C Erste Schritte ArrayIndexOutOfBoundsException beim Speichern im Array Java Basics - Anfänger-Themen 2
Hanschyo ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 5
I java.lang.ArrayIndexOutOfBoundsException at lösung.main Java Basics - Anfänger-Themen 3
R Compiler-Fehler ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 7
R java.lang.ArrayIndexOutOfBoundsException: 0 Rechner Error Java Basics - Anfänger-Themen 4
C Compiler-Fehler Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 Java Basics - Anfänger-Themen 3
L ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 10
J Zwei Arrays zippen wirft eine ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 11
V java.lang.ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 2
L Fehler: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 4
L Compiler-Fehler Problem beim Programmieren eines Kalenders (java.lang.ArrayIndexOutOfBoundsException) Java Basics - Anfänger-Themen 2
V ArrayIndexOutofBoundsException:0 Java Basics - Anfänger-Themen 0
R ArrayIndexOutofBoundsException: 10 Java Basics - Anfänger-Themen 5
P ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 2
T ArrayIndexOutOfBoundsException -> Fehler in for-Schleife? Java Basics - Anfänger-Themen 6
S ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 5
T ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 4
B java.lang.ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 4
S noch ein ArrayIndexOutOfBoundsException Fehler Java Basics - Anfänger-Themen 2
R Compiler-Fehler java.lang.ArrayIndexOutOfBoundsException, warum? Java Basics - Anfänger-Themen 6
O ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 18
J "ArrayIndexOutOfBoundsException", finde den Fehler schlicht nicht Java Basics - Anfänger-Themen 6
A ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 20
T ArrayIndexOutOfBoundsException - Problem mit Array-Größe Java Basics - Anfänger-Themen 4
C ArrayIndexOutOfBoundsException bei meinem ersten objektiorientierten Programm Java Basics - Anfänger-Themen 4
A parseDouble - ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 3
S ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 5
A ArrayIndexOutOfBoundsException - woher kommt er?! Java Basics - Anfänger-Themen 4
J AWT-EventQueue: ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 2
S ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 6
S ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 5
K ArrayIndexOutOfBoundsException: 0 Java Basics - Anfänger-Themen 4
Luk10 ArrayIndexOutOfBoundsException .... Java Basics - Anfänger-Themen 2
X Probleme mit ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 7
B Fehlermeldung: "ArrayIndexOutOfBoundsException"??? Java Basics - Anfänger-Themen 3
H java.lang.ArrayIndexOutOfBoundsException: 0 >= 0 Java Basics - Anfänger-Themen 5
N ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 7
U ArrayIndexOutOfBoundsException - dringend Hilfe gesucht Java Basics - Anfänger-Themen 8
S java.lang.ArrayIndexOutOfBoundsException: 5 nur wieso? Java Basics - Anfänger-Themen 2
T ArrayIndexOutOfBoundsException ? Matrizenrechnung. Java Basics - Anfänger-Themen 3
B ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 5
T ArrayIndexOutOfBoundsException finde den Auslöser nicht Java Basics - Anfänger-Themen 5
G ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 11
V java.lang.ArrayIndexOutOfBoundsException: Java Basics - Anfänger-Themen 3
E 2 Prob.:"ArrayIndexOutOfBoundsException" & do- Java Basics - Anfänger-Themen 2
Z Warum habe ich eine ArrayIndexOutOfBoundsException? Java Basics - Anfänger-Themen 2
N ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 2
S ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 3
N ArrayIndexOutOfBoundsException...... Java Basics - Anfänger-Themen 8

Ähnliche Java Themen

Neue Themen


Oben