๋ฐฐ์ด(array)์ด๋?
ํฌ๊ธฐ์ ์ฑ๊ฒฉ์ด ๊ฐ์ ์ผ๋ จ์ ์์๋ค์ด ๋ชจ์ฌ ๊ณต๋์ ์งํฉ ๊ตฌ์กฐ๋ฅผ ๊ฐ๋ ์๋ฃ์ ์งํฉ์ฒด
๋์ง์ ์ธ ์ฑ๊ฒฉ์ ๊ฐ์ง ์๋ฃ๋ค์ ๊ฐ๋ณ์ ์ผ๋ก ์๋ฃํ์ ๋ถ์ฌํ์ง ์๊ณ
ํ๋์ ์ด๋ฆ์ ๊ฐ์ง๊ณ ์ฒ๋ฆฌํ ๋ชฉ์ ์ผ๋ก ์ฌ์ฉ
๋์ผํ ํ์
์ ์ฌ๋ฌ ๋ณ์๋ค์ ํ๋์ ๋ฌถ์์ผ๋ก ๋ค๋ฃฌ๋ค
๊ฐ ์์๋ ๋ฉ๋ชจ๋ฆฌ์์ ์ฐ์์ ์ผ๋ก ์ ์ฅ
๋ฐฐ์ด์ ์ด๋ฆ (์ฐธ์กฐํ ๋ณ์, ๋ ํผ๋ฐ์ค ๋ณ์)์ ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์
์ ์ฅ๋ ๊ฐ์ ์ฐธ์กฐํ๊ธฐ ์ํ ์ฐธ์กฐ ๊ฐ(ํฌ์ธํฐ)์ ๊ฐ๋๋ค
์ฒ์ ์ค์ ํ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝ ํ ์ ์๋ค.
๋ฐฐ์ด ์ ์ธ
์๋ฃํ(ํ์ ) ๋๋ ๋ฐฐ์ด๋ช (๋ณ์) ๋ค์ ๋๊ดํธ ([])๋ฅผ ๋ถ์ฌ ๋ฐฐ์ด ์ ์ธ
๋ฉ๋ชจ๋ฆฌ ํ ๋น
๋ฐฐ์ด์ ์ ์ธ ํ ์ฐ์์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ์์ ๊ฐ์ ์ ์ฅํ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ new ์ฐ์ฐ์๋ฅผ ์ด์ฉํ์ฌ ํ ๋น
ํ ๋น ์ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ intํ์ ์ฌ์ฉํ๋ค.
new ์ฐ์ฐ์๋ฅผ ์ด์ฉํ์ฌ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋น ํ ๊ฒฝ์ฐ ํ์ ์ด ์ซ์ ํ์ธ ๊ฒฝ์ฐ๋ 0,
๊ฐ์ฒด๋ null๋ก ์ด๊ธฐํ๊ฐ ์๋์ผ๋ก ์ด๋ฃจ์ด์ง๋ค.
length ํ๋
๋ฐฐ์ด์ ํฌ๊ธฐ๋ length ๋ผ๋ ํ๋๋ฅผ ์ด์ฉํ์ฌ ํ์ธ
Ex01. ๋ฐฐ์ด ๊ฐ ์ถ๋ ฅํ๊ธฐ
package ch05.unit01;
public class Ex01_Array {
public static void main(String[] args) {
// ๋ฐฐ์ด ์ ์ธ ๋ฐ ๊ฐ ํ ๋น
int []a; // ๋ฐฐ์ด ์ ์ธ
a = new int[5]; // ๋ฉ๋ชจ๋ฆฌํ ๋น(5๊ฐ์ ์ ์๋ฅผ ์ ์ฅํ ์ ์๋ ์ ์ํ ๋ฐฐ์ด)
// int []a = new int[5]; // ๋ฐฐ์ด ์ ์ธ๊ณผ ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ํ๋ฒ์
// System.out.println( a[0] ); // 0
// ๋ฐฐ์ด์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น๊ณผ ๋์์ ์ ์ํ ๋ฐฐ์ด์ 0์ผ๋ก ์ด๊ธฐํ
// ๋ฐฐ์ด์ ๊ฐ ์์์ ๊ฐ ํ ๋น
a[0] = 1; a[1] = 3; a[2] = 5; a[3] = 7; a[4] = 9;
// a[5] = 11; // ๋ฐํ์ ์ค๋ฅ. ArrayIndexOutOfBoundsException
// ์ฌ์ฉ ๊ฐ๋ฅํ ์ฒจ์๋ 0~๋ฐฐ์ด๊ธธ์ด-1
// length ํ๋ : ๋ฐฐ์ด์ ์์์ ๊ฐ์ ๋ฐํ
int s = 0;
for(int i=0; i<a.length; i++) {
s += a[i];
}
System.out.println("๊ฒฐ๊ณผ : " + s);
}
}
ํฅ์๋ for๋ฌธ
Ex02. ๋ฐฐ์ด
package ch04.unit06;
public class Ex02_Array {
public static void main(String[] args) {
int[] a = new int [5];
a[0] = 90; a[1] = 80; a[3]=85; a[4]=90;
int tot,ave;
tot=0;
for(int n:a) { //for(์๋ฃํ ๋ณ์ : ์ปฌ๋ ์
ํ์
๋ณ์)
tot +=n;
}
ave = tot / a.length;
System.out.println("์ด์ : "+tot);
System.out.println("ํ๊ท : "+ ave);
}
}
Ex03.๋ฐฐ์ด ์ถ๋ ฅ
package ch04.unit06;
public class Ex03 {
public static void main(String[] args) {
// ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝ ํ ์ ์๋ค.
// ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ๋ฉด ์ด์ ๋ฐฐ์ด์ ์์น๋ฅผ ์ฐธ์กฐํ ์ ์์ด
// ์ด์ ๋ฐฐ์ด์ ๊ฐ๋น์ง์ปฌ๋ ํฐ์ ๋์์ด ๋๋ฉฐ ์๋ก์ด ๋ฐฐ์ด์ด ๋ง๋ค์ด ์ง๋ค
int [] a = new int [3];
a[0] = 10; a[1]= 20; a[2]=30;
System.out.println("๋ฐฐ์ด์ ์ ์ธํ๊ณ ๋ฉ๋ชจ๋ฆฌ ํ ๋น๊ณผ ๊ฐ ํ ๋นํ ...");
for(int n:a) {
System.out.println(n+" ");
}
System.out.println("\n๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ ๊ฒฝ์ฐ...");
a = new int[5]; // ๋ค์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋น ๋ฐ์ผ๋ฉด, ์ด์ ๋ฐฐ์ด์ garbage collector ๋์
for(int n:a) {
System.out.println(n+" "); // 0 0 0 0 0
}
}
}
Ex04. ๋ฐฐ์ด ํ ๋น
package ch04.unit06;
public class Ex04 {
public static void main(String[] args) {
// ๋ฐฐ์ด์ ์ฐธ์กฐํ์ด๋ค
// ์ผ๋ฐ ๋ณ์
int x = 10;
int y = x;
y+=20;
System.out.println(x+","+y); // 10,30
// ๋ฐฐ์ด
int []a = new int[3];
a[0] = 10; a[1] = 20; a[2] = 30;
System.out.println("์ด๊ธฐ a ๋ฐฐ์ด...");
for(int n:a) {
System.out.println(n+" ");
}
System.out.println();
int []b = a; // ํ๋์ ๋ฐฐ์ด ๊ณต์
b[1] = 200;
System.out.println("a ๋ฐฐ์ด...");
for(int n : a) {
System.out.println(n+" ");
}
System.out.println();
System.out.println("b ๋ฐฐ์ด...");
for(int n : b) {
System.out.println(n+" ");
}
System.out.println();
}
}
Ex05. ์ด๋ฆ, ์ ์๋ฅผ ๋ฐ์์ ํธ์ฐจ,์ดํฉ,ํ๊ท ๊ตฌํ๊ธฐ
package ch04.unit06;
import java.util.Scanner;
public class Ex05 {
public static void main(String[] args) {
// 5๋ช
์ ์ด๋ฆ๊ณผ ์ ์๋ฅผ ์
๋ ฅ ๋ฐ์ ํธ์ฐจ (์-ํ๊ท )์ ๊ณ์ฐํ์ฌ ์ด๋ฆ, ์ ์, ํธ์ฐจ๋ฅผ ์ถ๋ ฅํ๊ณ
// ๋ง์ง๋ง์ ์ ์ฒด ์ด์ , ํ๊ท ์ถ๋ ฅ
Scanner sc = new Scanner(System.in);
String []name = new String[5];
int []score = new int[5];
int tot = 0;
double ave, s;
for(int i=0; i<name.length; i++) {
System.out.println((i+1)+" ๋ฒ์งธ ์ด๋ฆ? ");
name[i] = sc.next();
System.out.print(" ์ ์ ?");
score[i] = sc.nextInt();
tot += score[i];
}
ave = (double)tot / score.length;
System.out.println("์ด๋ฆ\t์ ์\tํธ์ฐจ");
System.out.println("------------------");
for(int i=0; i<name.length; i++) {
s = score[i] -ave;
System.out.println(name[i]+"\t"+score[i]+"\t"+s);
}
System.out.println("------------------");
System.out.println("์ดํฉ : "+ tot);
System.out.println("------------------");
System.out.println("ํ๊ท : "+ ave);
sc.close();
}
}
Ex06. 1~ 100์ฌ์ด ์๋ก ๋ค๋ฅธ ๋์ 10๊ฐ๋ฅผ ์ถ๋ ฅํ๊ธฐ
package ch04.unit06;
public class Ex06 {
public static void main(String[] args) {
// 1~100์ฌ์ด์ ์์ค ์๋ก ๋ค๋ฅธ ๋์ 10๊ฐ๋ฅผ ์ถ๋ ฅํ๊ธฐ
/*
int n ;
for(int i=1; i<=10; i++) {
n = (int)(Math.random()*100)+1;
System.out.print(n+" ");
}
System.out.println();
*/
int[]n = new int [10];
for(int i = 0; i<n.length; i++) {
n[i] = (int)(Math.random()*100)+1;
// ๋์ผํ ๋์๊ฐ ๋ฐ์ํ๋ฉด
for(int j=0; j<i; j++) {
if(n[i]==n[j]) {
i--;
break;
}
}
}
for(int a:n) {
System.out.println(a+ " ");
}
System.out.println();
}
}
๋ฐฐ์ด์ ์ด๊ธฐํ
์ ์ ๋์์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น๊ณผ ์ด๊ธฐํ ๊ฐ๋ฅ
int[]arr = new int[]{10,20,30,40,50}
or
int[]arr;
arr=new int []{,10,20,30,40,50};
Ex07. ๋ฐฐ์ด ์ด๊ธฐํ
package ch04.unit06;
public class Ex07 {
public static void main(String[] args) {
// ๋ฐฐ์ด ์ด๊ธฐํ
/*
- ๋ฐฐ์ด ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํ
: ์ด๊ธฐํ ํ ๋ ์ด๊ธฐ๊ฐ์ ๊ฐฏ์๊ฐ ๋ฐฐ์ด์ ํฌ๊ธฐ
: ์ด๊ธฐํ ํ ๋ new int[]๋ฅผ new int[5]์ฒ๋ผ ์์์ ํฌ๊ธฐ ์ง์ ๋ถ๊ฐ
*/
int[] a= new int[] {2,4,6,8,10};
// int [] a = {2,4,6,8,10};
// ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํ ํ ๊ฒฝ์ฐ์๋ new int[] ์๋ต ๊ฐ๋ฅ
/*
int [] x ;
x = {1,3,5,7,9};; // ์ปดํ์ผ ์ค๋ฅ
x = new int[] {,1,3,5,7,9};
*/
System.out.println("๋ฐฐ์ด ์์์ ๊ฐ์ : "+ a.length);
int tot = 0;
for(int n : a) {
tot += n;
}
System.out.println("๊ฒฐ๊ณผ:"+tot);
}
}
Ex08. ์งํ ๊ฐฏ์ ๊ตฌํ๊ธฐ
package ch04.unit06;
public class Ex08 {
public static void main(String[] args) {
// 4320์์ ์ง๋ถํ๊ธฐ ์ํด ํ์ํ 1000, 500,100,50,10์
// ์งํ ๋๋ ๋์ ์ ๊ฐ์
int[]unit = new int[] {1000,500,100,50,10};
int money = 4320;
System.out.println("๊ธ์ก : "+money);
System.out.println("ํํ\t๊ฐ์");
for(int n : unit) {
System.out.println(n+"์\t"+(money/n));
money %=n;
}
}
}
Quiz01. ๋ฐฐ์ด๋ก ๋ ๊ตฌํ๊ธฐ
- ์ ๋ ฅํ ๋ ๋๊ฐ 1900๋ณด๋ค ์ ์ผ๋ฉด ๋ค์ ์ ๋ ฅ
๋ | ์ฅ | ์ | ๋ฒ | ํ ๋ผ | ์ฉ | ๋ฑ | ๋ง | ์ | ์์ญ์ด | ๋ญ | ๊ฐ | ๋ผ์ง |
์ฐ๋ | 2020 | 2021 | 2022 | 2023 | 2024 | 2025 | 2026 | 2027 | 2028 | 2029 | 2030 | 2031 |
package ch04.unit06;
import java.util.Scanner;
public class Quiz01 {
public static void main(String[] args) {
// ๋ ๊ตฌํ๊ธฐ
Scanner sc = new Scanner(System .in);
String[]t = new String[] {"์์ญ์ด","๋ญ","๊ฐ","๋ผ์ง","์ฅ","์","๋ฒ","ํ ๋ผ","์ฉ","๋ฑ","์"};
int y ;
do {
System.out.print("๋
๋ ?");
y = sc.nextInt();
}while(y<1900);
System.out.printf("%d๋
๋์ ํด๋ %s๋ ์ ํด์
๋๋ค.\n",y,t[y%12]);
sc.close();
}
}
Quiz02. 5๊ฐ์ ์ ์(0~10)๋ฅผ ์ ๋ ฅ ๋ฐ์ ์ต๋์ ์์ ์ต์์ ์๋ฅผ ๋บ ํฉ์ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ
- ์ ๋ ฅํ ์ ์๊ฐ 0~10 ์ฌ์ด์ ์ ์๊ฐ ์๋ ๊ฒฝ์ฐ ๋ค์ ์ ๋ ฅ
- ์ ๋ ฅ ๋ฐ์ ์ ์๋ ๋ฐฐ์ด์ ์ ์ฅ
- ์ทจ๋์ ์ = ์ ์ฒด์ ์์ํฉ - ์ต๋์ ์ - ์ต์์ ์
package ch05.unit01;
import java.util.Scanner;
public class Quiz02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int []num = new int[5];
for(int i=0; i<num.length; i++) {
do {
System.out.println((i+1)+"๋ฒ์งธ ์ ์ : ");
num[i] = sc.nextInt();
} while(num[i] < 0 || num[i] > 10);
}
// ์ต๋๊ฐ, ์ต์๊ฐ ๊ตฌํ๊ธฐ
int max, min;
/*
max = 0; min = 10; // ๊ฐ์ฅ ์์๊ฐ๊ณผ ํฐ๊ฐ์ ์ด๊ธฐ์น๋ก
for(int i=0; i<num.length; i++) {
if(max < num[i]) {
max = num[i];
}
if(min > num[i]) {
min = num[i];
}
}
*/
max = min = num[0]; // ์ฒ์๊ฐ์ ์ด๊ธฐ์น
for(int i=1; i<num.length; i++) {
if(max < num[i]) {
max = num[i];
} else if(min > num[i]) {
min = num[i];
}
}
// ์
๋ ฅ ๋ฐ์ ์ ์ฒด ์ ์ ๋ฆฌ์คํธ ์ถ๋ ฅ
int tot =0;
System.out.print("์ ์ฒด ์ ์ ๋ฆฌ์คํธ : ");
for(int n : num) {
tot += n;
System.out.print(n + " ");
}
System.out.println();
// ์ทจ๋์ ์ ์ถ๋ ฅ
System.out.println("์ทจ๋ ์ ์ : " + (tot-max-min));
sc.close();
}
}
Quiz04. ๋ฌ๋ ฅ ์ถ์ถํ๊ธฐ
package ch05.unit01;
import java.util.Scanner;
public class Quiz04 {
public static void main(String[] args) {
/*
- ๋
๋๊ฐ 4์๋ฐฐ์์ด๊ณ 100์ ๋ฐฐ์๊ฐ ์๋๊ฑฐ๋ 400์ ๋ฐฐ์์ด๋ฉด
์ค๋
์ผ๋ก 2์์ด 29์ผ์ด๊ณ ๊ทธ๋ ์ง ์์ผ๋ฉด 2์์ 28์ผ
- 1๋
1์ 1์ผ : ์์์ผ
*/
Scanner sc = new Scanner(System.in);
int [] days = new int[] {31,28,31,30,31,30,31,31,30,31,30,31};
int y, m, tot, w;
do {
System.out.print("๋
๋ ? ");
y = sc.nextInt();
} while(y < 1900);
do {
System.out.print("์ ? ");
m = sc.nextInt();
} while(m < 1 || m > 12);
// y๋
๋์ 2์์ ๋ง์ง๋ง ๋ ์ง
days[1] = y%4==0&&y%100!=0 || y%400==0 ? 29 : 28;
// 2024๋
3์
// 1.1.1์ผ ~ 2024๋
๋ 3์ 1์ผ๊น์ง ์ ์ฒด ์ผ์
// ์ ์ฒด์ผ์ = (y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400;
// ์ ์ฒด์ผ์ += 1์ + 2์(m-1) + 1
tot = (y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400;
for(int i=0; i<m-1; i++) {
tot += days[i];
}
tot++;
w = tot%7; // 1์ผ์ ์์ผ(0:์ผ,.. ,6:ํ )
System.out.printf("\n\t%d๋
%d์\n", y, m);
System.out.println(" ์ผ ์ ํ ์ ๋ชฉ ๊ธ ํ ");
System.out.println("------------------------------");
// 1์ผ ์ ๊ณต๋ฐฑ์ฒ๋ฆฌ
for(int i=0; i<w; i++) {
System.out.print(" ");
}
for(int i=1; i<=days[m-1]; i++) {
System.out.printf("%4d", i);
if(++w % 7 == 0) {
System.out.println();
}
}
if(w%7 != 0) {
System.out.println();
}
System.out.println("------------------------------");
sc.close();
}
}
'๐ป > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] 7์ผ์ฐจ : ๋ฐฐ์ด์ ๋ฐฐ์ด (0) | 2024.02.22 |
---|---|
[Java] 6์ผ์ฐจ : ์ ๋ ฌ (0) | 2024.02.21 |
[Java] 6์ผ์ฐจ : return ๋ฌธ (0) | 2024.02.21 |
[Java] 5์ผ์ฐจ : break๋ฌธ, continue๋ฌธ (0) | 2024.02.20 |
[Java] 5์ผ์ฐจ : for ๋ฌธ (1) | 2024.02.20 |