2-Dimensionales Array mit RandomNumbers fuellen

Status
Nicht offen für weitere Antworten.

nowaii

Neues Mitglied
Hallo 🙂
Ich habe ein kleines Problem, wenn ich ein 2-dimensionales Array mit Zufallszahlenfuellen will, bekomme ich nicht die erwuenschte ausgabe 🙁 ausgabe ist nur ein 1-dimensioanles array ....

hier mein code ...

Code:
	import java.util.*;


		import java.util.*;

		public class arrayTest {
		
			public static void main(String[] args)
			{
				Random randNumGenerator = new Random();

				int array [] [] = new int[10][10];
				for (int i=0; i<array.length; i++)
				{
					array[i][i] = (randNumGenerator.nextInt(100));
					System.out.println(array[i][i]);
				}
			}
		}

hoffentlich kann mir jemand helfen 🙂
thx
nowaii
 
Hallo nowaii
Für ein zweidimensionales Array benötigst du auch zwei Schleifen (geht auch mit
einer aber so ist es vielleicht ein bisschen besser verständlich).
Dabei ist die äußere Schleife für die Zeilen und die innere für die Spalten.
Java:
import java.util.*;
import java.util.*;

public class arrayTest {

	public static void main(String[] args) {
		
		Random randNumGenerator = new Random();

		int array [] [] = new int[10][10];
		for (int i=0; i<array.length; i++) {
			
			for(int j=0;j<array[i].length;j++) {
				
				array[i][j] = (randNumGenerator.nextInt(100));
				System.out.print(array[i][j]+" ");
			}
			System.out.println();
		}
	}
}
 
hi
thx fuer die antwort - waere ja irgendwie logisch, sonst zahelt ja nur ein teil des arrays hoch 🙂
thx ! 🙂
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben