๐งฉ ํ์ต ๋จ์ : ๋ฐฐ์ด(Array)
๐ ํ์ต ๋์ : <Java์ ์ ์ 3ํ>(๋จ๊ถ ์ฑ ์ )
โ ๋ฐฐ์ด ์์ ์์ฉ ํ์ต ์๋ฃ()
์์ฉA. char[] ๋ฐฐ์ด ํน์ง | ๋ฐฐ์ด์ ๊น์ ๋ณต์ฌ 4๊ฐ์ง ๋ฐฉ๋ฒ
์ถ๋ ฅ๋ฌธ์ System.out.println(iarr);๊ณผ ๊ฐ์ด ๋ฐฐ์ด์ ์ด๋ฆ์ ๊ทธ๋๋ก ์ฐ๋ฉด ๋ณดํต ์ฐธ์กฐํ๊ณ ์๋ ๋ฐฐ์ด์ ์ฃผ์๊ฐ 16์ง์๋ก ํํ๋ผ ๋์จ๋ค. ํ์ง๋ง ๋ฌธ์ํ char[] ๋ฐฐ์ด๋ง์ ๋ค๋ฅด๋ค. println ๋ฉ์๋๊ฐ char[] ๋ฐฐ์ด์ ๋ํด์๋ง ๊ตฌ๋ถ์ ์์ด ์์๋ฅผ ์ถ๋ ฅํ๋๋ก ๋ง๋ค์ด์ก๊ธฐ ๋๋ฌธ์ด๋ค. ๋จ, System.out.println("๋ฐฐ์ด num : " + num);์ฒ๋ผ ๋ฌธ์์ด์ ์์ด ์ถ๋ ฅํ ๊ฒฝ์ฐ char[] ๋ฐฐ์ด ์ญ์ ์ฃผ์๊ฐ์ ๋ด๋๋๋ค.
ABCD
0123456789
char[] abc = {'A', 'B', 'C', 'D'};
char[] num = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
System.out.println(abc);
System.out.println(num);
for๋ฌธ ์ด์ฉํ ๊น์ ๋ณต์ฌ
copyA length : 14
copyA : A B C D 0 1 2 3 4 5 6 7 8 9
/* A. for๋ฌธ */
System.out.println("====== A. for๋ฌธ ======");
char[] copyA = new char[abc.length + num.length];
/* A-1. abc ๋ณต์ฌ */
for(int i=0; i < abc.length; i++) {
copyA[i] = abc[i];
}
/* A-2. num ๋ณต์ฌ */
for(int i=0; i < num.length; i++) {
copyA[abc.length + i] = num[i];
}
/* A-3. ์ถ๋ ฅ */
System.out.println("copyA length : " + copyA.length);
System.out.print("copyA : ");
for(int i=0; i < copyA.length; i++) {
System.out.print(copyA[i] + " ");
}
System.out.println();
.clone(); ์ด์ฉํ ๊น์ ๋ณต์ฌ
๋๊ฐ์ ๋ฐ์ดํฐ, ๋๊ฐ์ ๊ธธ์ด๋ฐ์ ๋ง๋ค์ง ๋ชปํ๋ค!
๋ณต์ ์ copyB length : 14
๋ณต์ ํ copyB length : 4
copy B : A B C D
/* B. .clone() */
System.out.println("====== B. clone() ======");
char[] copyB = new char[abc.length + num.length];
System.out.println("๋ณต์ ์ copyB length : " + copyB.length);
copyB = abc.clone();
System.out.println("๋ณต์ ํ copyB length : " + copyB.length);
System.out.print("copy B : ");
for(int i=0; i < copyB.length; i++) {
System.out.print(copyB[i] + " ");
}
System.out.println();
System.arraycopy(); ์ด์ฉํ ๊น์ ๋ณต์ฌ
๋ค๋ฅธ ๋ณต์ฌ ๋ฐฉ๋ฒ๋ค์ ๋นํด ์๋์ ์ผ๋ก abc + num ๋ฐฐ์ด ํฉ์น๊ธฐ๊ฐ ์์ํ๋ค.
๋ํ, ์์ index๋ฅผ ์ง์ ํ ์ ์์ด ์ฒ์-์ค๊ฐ-๋ ์ด๋๋ ๋ด์ ์ ์๋ ๊ธธ์ด๋งํผ ์ฎ๊ฒจ๊ฐ ์ ์์๋ค.
์์ํ๊ฒ ๋ฐฐ์ด์ ๋ณต์ฌ๋ง์ ์ํด ๋ง๋ค์ด์ง ๋ฉ์๋๋ผ ๊ฐ์ฅ ํจ์จ์ ์ธ ๊น์ ๋ณต์ฌ ๋ฐฉ๋ฒ์ด๋ผ ํ ์ ์๋ค.
ABCD0123456789
ABCD456789
ABCD4ABC89
/* C. System.arraycopy() */
System.out.println("====== C. System.arraycopy() ======");
char[] copyC = new char[abc.length + num.length];
System.arraycopy(abc, 0, copyC, 0, abc.length);
System.arraycopy(num, 0, copyC, abc.length, num.length);
System.out.println(copyC);
System.arraycopy(abc, 0, num, 0, abc.length);
System.out.println(num);
System.arraycopy(abc, 0, num, 5, 3);
System.out.println(num);
Arrays.copyOf(); ์ด์ฉํ ๊น์ ๋ณต์ฌ
๊ฐ๊ฒฐํ ๊ตฌ์ฑ์ ๊ฐ์ท๋ค. ๋ณต์ฌ ๋ฐฐ์ด์ index 0๋ถํฐ ์์ํ ์ ์๋ค. ๊ธธ์ด๋ ์ง์ ์ง์ ์ด ๊ฐ๋ฅํ๋ฐ ์๋ ์์ ์์๋ charํ ๊ธฐ๋ณธ๊ฐ์ธ ๊ณต๋ฐฑ๋ฌธ์๊ฐ ๋ค๋ก ์ธ ์๋ฆฌ ๋ ์๋ค.
copy D : A B C D
/* D. Arrays.copyOf() */
System.out.println("====== D. Arrays.copyOf() ======");
char[] copyD = Arrays.copyOf(abc, 7);
System.out.print("copy D : ");
for(int i=0; i < copyD.length; i++) {
System.out.print(copyD[i] + " ");
}
System.out.println();
์์ฉB. ์ดํฉ-ํ๊ท -์ต๋๊ฐ-์ต์๊ฐ ๊ตฌํ๊ธฐ
ํ๊ท ์ ๊ฒฝ์ฐ ์ค์ํ ํ์ ์ ์ ํํ๊ณ , printf ๋ฉ์๋ ํตํด ์์์ ์๋ฆฌ ๋ ๋ฒ์งธ๊น์ง ์ถ๋ ฅํ๋ค: "ํ๊ท : %.2f%n"
์ดํฉ : 530
ํ๊ท : 75.71
์ต๋๊ฐ : 100
์ต์๊ฐ : 40
package com.reminder.exercises;
public class Ex13_196 {
public static void main(String[] args) {
int[] score = {70, 80, 90, 100, 100, 50, 40};
/* ์ดํฉ, ํ๊ท */
int sum=0;
float average=0.0f;
for(int i=0; i < score.length; i++) {
sum += score[i];
}
average = (float)sum / score.length;
System.out.printf("์ดํฉ : %d%n", sum);
System.out.printf("ํ๊ท : %.2f%n", average);
/* ์ต๋๊ฐ, ์ต์๊ฐ */
int max=score[0];
int min=score[0];
for(int i=0; i < score.length; i++) {
if(max < score[i]) {
max = score[i];
}
if(min > score[i]) {
min = score[i];
}
}
System.out.println("์ต๋๊ฐ : " + max);
System.out.println("์ต์๊ฐ : " + min);
}
}
๋ฒํธ ๊ตญ์ด ์์ด ์ํ ์ด์ ํ๊ท
=================================
1 100 100 100 300 100.0
2 20 20 20 60 20.0
3 30 30 30 90 30.0
4 40 40 40 120 40.0
5 50 50 50 150 50.0
=================================
์ด์ : 240 240 240
package com.reminder.exercises;
public class Ex16_218 {
public static void main(String[] args) {
int[][] score = {{100, 100, 100}, {20, 20, 20}, {30, 30, 30}, {40, 40, 40}, {50, 50, 50}};
int kor=0, eng=0, math=0;
System.out.println("๋ฒํธ ๊ตญ์ด ์์ด ์ํ ์ด์ ํ๊ท ");
System.out.println("=================================");
for(int i=0; i < score.length; i++) {
kor += score[i][0];
eng += score[i][1];
math += score[i][2];
System.out.printf("%3d", i+1);
int sum=0;
float avg=0.0f;
for(int j=0; j < score[i].length; j++) {
sum += score[i][j];
System.out.printf("%5d", score[i][j]);
}
avg = (float)sum / score[i].length;
System.out.printf("%5d %5.1f%n", sum, avg);
}
System.out.println("=================================");
System.out.printf("์ด์ : %3d %4d %4d%n", kor, eng, math);
}
}
์์ฉC. ์ ํ(๋ก๋ ๋ฒํธ ์ถ๋ ฅํ๊ธฐ)
๋ฐฐ์ด index๋ฅผ ํ์ฉํด ์ด๊ธฐํํ๊ธฐ ๋๋ฌธ์ ball[random] ๊ฐ์ ๊ฐ์ง๊ณ ์ฌ์ฏ ์๋ฆฌ ์ถ์ถํ๋ฉด ์์นซ ์ค๋ณต๋ ๋ฒํธ๊ฐ ๋์ฌ ์ ์์์ ์ฃผ์ํ์. ์ฌ์ฉ๋ ์ธ ๊ฐ์ง for๋ฌธ์ ๊ฐ๊ฐ ball[i] ์ด๊ธฐํ-์์์๋ถํฐ ๋น์ฒจ ๋ฒํธ 6๊ฐ ์ ๋ ฅ-์ฌ์ฏ ์๋ฆฌ ์ถ๋ ฅ์ ๋งํ๋ค.
ball[1]=14
ball[2]=3
ball[3]=13
ball[4]=28
ball[5]=36
ball[6]=9
package com.reminder.exercises;
public class Ex14_199 {
public static void main(String[] args) {
int[] ball = new int[45];
for(int i=0; i < ball.length; i++) {
ball[i] = i + 1;
}
int tmp=0;
for(int i=0; i < 6; i++) {
int random = (int)(Math.random() * 45) + 1;
tmp = ball[i];
ball[i] = ball[random];
ball[random]=tmp;
}
for(int i=0; i < 6; i++) {
System.out.printf("ball[" + (i+1) + "]=" + ball[i] + "%n");
}
}
}
์์ฉD. ๋น๋์ ๊ตฌํ๊ธฐ
iarr[i]์ ์์์ ๊ฐ์ด ์ ์ฅ๋ ์ํ์ด๊ธฐ์ count[iarr[i]]++; ์ฒ๋ผ ์์ฑํ๋ฉด ์ฒซ ๋ฒ์งธ์ ๊ฒฝ์ฐ count[5]++;๋ก ํด์๋๋ค. ์ด๋ ์ฆ count ๋ฐฐ์ด์ [5] ์์๋ฅผ 1 ์ฆ๊ฐ์ํจ๋ค๋ ์๋ฏธ์ด๋ค.
5618068222
0์ ๊ฐ์ : 1
1์ ๊ฐ์ : 1
2์ ๊ฐ์ : 3
3์ ๊ฐ์ : 0
4์ ๊ฐ์ : 0
5์ ๊ฐ์ : 1
6์ ๊ฐ์ : 2
7์ ๊ฐ์ : 0
8์ ๊ฐ์ : 2
9์ ๊ฐ์ : 0
package com.reminder.exercises;
public class Ex15_204 {
public static void main(String[] args) {
int[] iarr = new int[10];
int[] count = new int[10];
/* ๋์ ์ ์ฅ */
for(int i=0; i < iarr.length; i++) {
iarr[i] = (int)(Math.random() * 10);
System.out.print(iarr[i]);
}
System.out.println();
/* ๋น๋์ ํ๋จ */
for(int i=0; i < iarr.length; i++) {
count[iarr[i]]++;
}
/* ์ถ๋ ฅ */
for(int i=0; i < iarr.length; i++) {
System.out.println(i +"์ ๊ฐ์ : " + count[i]);
}
}
}
์์ฉE. ๋น๊ณ ๊ฒ์
1~25์ ์ซ์๋ฅผ ์ ๋ ฅํ์ธ์. (์ข ๋ฃ 0) > 2
12 19 10 13 0
9 16 22 20 0
8 23 17 15 11
21 25 0 3 6
0 24 1 5 18
1~25์ ์ซ์๋ฅผ ์ ๋ ฅํ์ธ์. (์ข ๋ฃ 0) > 0
๊ฒ์์ ์ข ๋ฃํฉ๋๋ค.
package com.reminder.exercises;
import java.util.Scanner;
public class Ex17_223 {
public static void main(String[] args) {
final int SIZE = 5;
int x=0, y=0, num=0;
int[][] bingo = new int[SIZE][SIZE];
Scanner scanner = new Scanner(System.in);
for(int i=0; i < SIZE; i++) {
for(int j=0; j < SIZE; j++) {
bingo[i][j] = i * SIZE + j + 1;
}
}
for(int i=0; i < SIZE; i++) {
for(int j=0; j < SIZE; j++) {
x = (int)(Math.random() * SIZE);
y = (int)(Math.random() * SIZE);
int tmp = bingo[i][j];
bingo[i][j] = bingo[x][y];
bingo[x][y] = tmp;
}
}
do {
for(int i=0; i < SIZE; i++) {
for(int j=0; j < SIZE; j++) {
System.out.printf("%2d ", bingo[i][j]);
}
System.out.println();
}
System.out.println();
System.out.printf("1~%d์ ์ซ์๋ฅผ ์
๋ ฅํ์ธ์. (์ข
๋ฃ 0) > ", SIZE*SIZE);
String tmp = scanner.nextLine();
num = Integer.parseInt(tmp);
if(num == 0) {
System.out.println("๊ฒ์์ ์ข
๋ฃํฉ๋๋ค.");
break;
}
over:
for(int i=0; i < SIZE; i++) {
for(int j=0; j < SIZE; j++) {
if(bingo[i][j] == num) {
bingo[i][j] = 0;
break over;
}
}
}
} while(num != 0);
}
}
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] 6-4. ํด๋์ค๋ณ์, ์ธ์คํด์ค๋ณ์, ์ง์ญ๋ณ์, ์ด๊ธฐํ ์์ (0) | 2022.01.03 |
---|---|
[JAVA/์์ ๊ณผ์ practice] ๋ฐฐ์ด | ๋ค์ฐจ์ ๋ฐฐ์ด Lv. 3~4 (0) | 2022.01.02 |
[์๋ฐ์ ์ ์] Ch 5. ๋ฐฐ์ด ์ฐ์ต๋ฌธ์ ํ์ด (0) | 2022.01.01 |
[JAVA/์์ Quiz] ํด๋์ค์ ๊ฐ์ฒด | ์ค๋ฒ๋ก๋ฉ | DTO (0) | 2021.12.31 |
[JAVA/์์ ๊ณผ์ practice] ํด๋์ค์ ๊ฐ์ฒด Lv. 3~4 (0) | 2021.12.30 |