[Java] 5. Matrix


2중for문, 2중while문, 2차원배열


  • Category : Java
  • Tag :


Matrix

For문(기초)

public static void matrix() {
	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < 5; j++) {
			System.out.print("["+i+","+j+"]");
		}
		System.out.println();
	}
}// The end of method




While문(기초)

public static void matrix() {
	int i = 0, j = 0;
	while (i < 5) {
		while (j < 5) {
			System.out.print("["+i+","+j+"]");
			++j;
		}
		++i;
		j = 0;
		System.out.println();
	}
}// The end of method




배열 [for]

public static void matrix() {
	String[][] starList = new String[5][5];
	for (int i = 0; i < starList.length; i++) {
		for (int j = 0; j < starList[i].length; j++) {
			starList[i][j] = "["+i+","+j+"]";
		}
	}// The end of For
	
	for (int i = 0; i < starList.length; i++) {
		for (int j = 0; j < starList[i].length; j++) {
			System.out.print(starList[i][j]);
		}
		System.out.println();
	}// The end of For
}




배열 [while]

public static void matrix() {
	String[][] starList = new String[5][5];
	for (int i = 0; i < starList.length; i++) {
		for (int j = 0; j < starList[i].length; j++) {
			starList[i][j] = "["+i+","+j+"]";
		}
	}// The end of For
	
	int i = 0;
	int j = 0;
	
	while (i < starList.length) {
		while (j < starList[i].length) {
			System.out.print(starList[i][j]);
			j++;
		}
		j = 0;
		i++;
		System.out.println();
	}// The end of while
}// The end of method





Share this post