Binary World

JAVA CLASS : 배열 본문

개발자의 길/JAVA

JAVA CLASS : 배열

모쿠 2016. 12. 29. 11:31

<Java Practice>


*ArrayMain02(배열의 선언, 초기화, 출력)


public class ArrayMain02 {


public static void main(String[] args) {

System.out.println("배열의 선언, 초기화, 출력");

// int 10개를 저장할 수 있는 배열 선언

int[] array = new int[10];

// 배열의 길이를 출력

System.out.println(array.length);

System.out.println("===============");

// 배열의 인덱스 : 0 ~ (length - 1)

// for문을 사용한 배열의 초기화

for (int i = 0; i < array.length; i++) {

array[i] = (i + 1) * 10;

}

// for문을 사용한 배열 내용 출력

for (int i = 0; i < array.length; i++) {

System.out.println(array[i]);

}

// 배열의 모든 원소들의 합을 저장할 변수

int sum = 0; // sum = array[0] + array[1] + ..

for (int i = 0; i < array.length; i++){

sum += array[i];

}

System.out.println("sum = " + sum);

} // end main()


} // end class 


*ArrayMain03(배열을 통한 점수 출력)


 import java.util.Scanner;


public class ArrayMain03 {


public static void main(String[] args) {

// int 3개를 저장 할 수 있는 scores 배열을 선언

// for문을 사용하고, 입력을 받아서 점수를 배열에 저장

// 각 점수를 출력

// 배열에 있는 모든 점수들의 합계를 출력

// 점수의 평균을 계산해서 출력

// 점수의 최대값 출력

// 점수의 최소값 출력

int[] scores = new int[3];

// 배열값 초기화

for(int i = 0; i < scores.length; i++){

scores[i] = 0;

}

Scanner sc = new Scanner(System.in);

// 각 점수 입력

System.out.println("국어 점수 입력:");

scores[0] = sc.nextInt();

System.out.println("영어 점수 입력:");

scores[1] = sc.nextInt();

System.out.println("수학 점수 입력:");

scores[2] = sc.nextInt();

// 각 점수를 출력

for(int i=0; i < scores.length; i++){

if(i == 0)

{

System.out.print("국어 점수 : ");

}

else if (i == 1)

{

System.out.print("영어 점수 : ");

}

else

{

System.out.print("수학 점수 : ");

}

System.out.println(scores[i]);

}

int sum = 0;

for(int i=0; i < scores.length; i++){

sum += scores[i];

}

System.out.println("점수의 합계 :" + sum);

double avr = sum / 3.0;

System.out.println("점수의 평균 :" + avr);

//점수의 최대 최소 출력

int max = scores[0]; //배열의 0번째 원소를 최대값이라고 가정

for(int i=1; i < scores.length; i++){

if(max <= scores[i]){

max = scores[i];

}

}

int min = scores[0]; //배열의 0번째 원소를 최소값이라고 가정

for(int i=1; i < scores.length; i++){

if (min >= scores[i]){

min = scores[i];

}

}

System.out.println("최대값 : " + max);

System.out.println("최소값 : " + min);

sc.close();

} // end main()


} // end class


*ArrayMain09(이차 배열)


public class ArrayMain09 {


public static void main(String[] args) {

// 2차원 배열: 1차원 배열을 원소로 갖는 배열

int[] korean = { 10, 20, 30 }; // 1차원 배열

int[] english = { 40, 50, 60 };


int[][] scores = { { 10, 20, 30 }, { 40, 50, 60 } }; // 2X3인 2차원 배열

System.out.println(scores[1][1]);

System.out.println(scores[0][2]);

System.out.println(scores.length);

System.out.println(scores[0].length);


for (int x = 0; x < scores.length; x++) {

for (int y = 0; y < scores[x].length; y++) {

System.out.print(scores[x][y] + " ");

} // end for(y)

System.out.println();

} // end for (x)


System.out.println("=================");

int[][] array = { { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8, 9 } };


for (int i = 0; i < array.length; i++) {

for (int j = 0; j < array[i].length; j++) {

System.out.print(array[i][j] + " ");

} // end for(i)

System.out.println();

} // end for (j)

System.out.println("=================");

String[] names = {"홍길동" , "허균"};

System.out.println(names);

String[][] names2 = {{"홍길동"}, {"허균"}};

System.out.println(names2);

} // end main


} // end class 



'개발자의 길 > JAVA' 카테고리의 다른 글

JAVA CLASS : Method  (0) 2016.12.29
JAVA CLASS : 연습(반복문)  (0) 2016.12.29
JAVA CLASS : 반복문(for, while, loop)  (0) 2016.12.29
JAVA CLASS : 조건문(if)  (0) 2016.12.29
JAVA CLASS : 연산자  (0) 2016.12.29
Comments