728x90 반응형 전체 글565 [Java] 17. 배열 Exam6 (숫자로 이루어진 삼각형 출력하기) package chap5; /* [결과] 46 47 37 48 38 29 49 39 30 22 50 40 31 23 16 51 41 32 24 17 11 52 42 33 25 18 12 7 53 43 34 26 19 13 8 4 54 44 35 27 20 14 9 5 2 55 45 36 28 21 15 10 6 3 1 1.가변 배열 선언 2.각행의 1차원 배열 객체 생성 3.배열의 내부에 숫자 채우기 4.결과 출력 숫자 i j ---------- 1 9 9 2 8 8 3 9 8 4 7 7 5 8 7 6 9 7 7 6 6 8 7 6 9 8 6 10 9 6 ... */ public class Exam6 { public static void main(String[] args) { int[][] arr = { .. 2022. 3. 16. [Java] 17. 배열 (가변배열) package chap5; /* * 가변배열 : 다차원배열에서만 가능함 */ public class ArrEx9 { public static void main(String[] args) { int arr[][] = new int[3][]; //가변배열 arr[0] = new int[] {10,20}; arr[1] = new int[] {30,40,50}; arr[2] = new int[] {60}; //화면에 내용 출력하기 for(int i = 0; i 정수값출력.. 2022. 3. 16. [Java] 17. 배열 Exam5 (배열의 행의합과 열의합을 출력하기) package chap5; //arr 배열의 행의합과 열의합을 출력하기 public class Exam5 { public static void main(String[] args) { int[][] arr = {{1},{10,20},{30,40,50},{60,70,80,90}}; int maxcol = 0; for(int i = 0; i < arr.length; i++) { if(maxcol < arr[i].length) maxcol = arr[i].length; } int[] cols = new int[maxcol]; for(int i = 0; i < arr.length; i++) { int sum = 0; for(int j = 0; j < arr[i].length; j++) { sum += arr[i].. 2022. 3. 16. [Java] 17. 배열 (2차원 배열 초기화) package chap5; //2차원 배열의 초기화 public class ArrEx8 { public static void main(String[] args) { int[][] arr = {{10,20},{30,40,1,2,3},{50,60}}; //가변배열 for(int i = 0; i < arr.length; i++ ) { for(int j = 0; j < arr[i].length; j++) { System.out.print("arr[" + i + "][" + j + "] = " + arr[i][j] + ", "); } System.out.println(); } } } 2022. 3. 16. 이전 1 ··· 114 115 116 117 118 119 120 ··· 142 다음 728x90 반응형