Hallo ich wollte für die UNI heute ein Programm schreiben, dass ein 2dimensionales Array mit unterschiedlicher länge der Zeilen und fixer Zeilenanzahl erstellt und dieses dann befüllt. Mein Problem ist, dass ich bei meinen Versuchen entweder ein leeres Array produziere oder eine Fehlermeldung
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at test.main(test.java:17)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at test.main(test.java:17)
Code:
public class test {
public static int[][] array;
public static void main(String[] args) {
int rows = 5;//fixe Zeilenanzahl
array = new int[rows][];
for(int i=0;i<5;i++){ //zum Erstellen des Arrays mit unterschiedlicher Länge
int randomInt=(int)(Math.random()*10+1);//Zahl zwischen 1 und 10
array= new int [i][randomInt];
}
for(int i=0; i<5;i++){
for(int j=0; j<array[i].length;j++){
int randomInt=(int)(Math.random()*20+1);//Zahl zwischen 1 und 20
array[i][j]=randomInt;
}
}
for (int i = 0; i < 5; i++) {//ausgabe des Arrays
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}