์๋ฐ์ ๋ฐฐ์ด(Array)
- ๋ฐฐ์ด ์์ฑ
์๋ฐ์์ ๋ฐฐ์ด ์์ฑ์ ์๋์ ๊ฐ์ด ํ๋ค.
์๋์ ๋ฐฐ์ด ์์์์ 'student'๋ ๋ฐฐ์ด๋ณ์์ ํด๋นํ๋ค.
int[] =student; // ๋ฐฐ์ด ๋ณ์ ์ ์ธ
students= new int[5] //๋ฐฐ์ด ์์ฑ
๋ ์๋์ ๊ฐ์ด ๋ฐฐ์ด์ ๋ํ๋ผ ์ ์๋๋ฐ, ์์ ๋์ผํ ๋ฐฐ์ด์ด๋ค.
new int[5]๋ก ๋ฐฐ์ด์ ์์ฑํ๋ฉด ๋ฐฐ์ด์ ํฌ๊ธฐ๋งํผ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ๋ณดํ๋ค.
int[] student = new int[5];
๋ฐฐ์ด ์์ ๊ฐ์ ํ๋์ฝ๋ฉํ์ฌ ์ฌ์ฉํ ๋์๋ ์๋์ ๊ฐ์ด ์ฌ์ฉํ๋ค.
int[] student = {90,30,50,60,20};
- ๋ฐฐ์ด๊ฐ ์ฝ๊ธฐ
๋ฐฐ์ด์ ํน์ ์ธ๋ฑ์ค์ ํด๋นํ๋ ๊ฐ์ ์ฝ๊ณ ์ถ๋ค๋ฉด ์๋์ฒ๋ผ ๋ํ๋ผ ์ ์๋ค.
System.out.println(student[3]);
๊ธฐ๋ณธํ(Primitive Type) vs. ์ฐธ์กฐํ(Reference Type)
์๋ฐ์์ '๋ณ์๋ฐ์ดํฐ ํ์ '์ ๊ธฐ๋ณธํ๊ณผ ์ฐธ์กฐํ์ผ๋ก ๋ถ๋ฅํ๋ค.
- ๊ธฐ๋ณธํ
: ๋ณ์์ ์ฌ์ฉํ ๊ฐ์ ์ง์ ๋ฃ์ ์ ์๋ ๋ฐ์ดํฐํ์
: ๊ธฐ๋ณธํ์ ์ ์ธ๊ณผ ๋์์ ํฌ๊ธฐ๊ฐ ์ ํด์ง
ex)
int, long, double, boolean
- ์ฐธ์กฐํ
: ๋ฐฐ์ด ๋ณ์์ ๊ฐ์ด ๋ฉ๋ชจ๋ฆฌ์ ์ฐธ์กฐ ์ฃผ์๋ฅผ ์ ์ฅํ๋ ๋ฐ์ดํฐํ์ .
: ํฌ๊ธฐ๋ฅผ ๋์ ์ผ๋ก ํ ๋นํ ์ ์์ , ์ฌ์ฉ์์ ๋ ฅ์ ๋ฐ๋ผ size๋ณ์์ ๊ฐ์ด ๋ณํ๊ณ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ ๋ฌ๋ผ์ง.
์ด๋ฅผ '๋์ ๋ฉ๋ชจ๋ฆฌํ ๋น'์ด๋ผ๊ณ ํจ.
ex)
int size=10000;
new int[size];
//๊ธฐ๋ณธํ
int i; //4byte
long l; //8byte
double d; //8byte
//์ฐธ์กฐํ
int size=10000;
new int[size];
: ์ฐธ์กฐํ์ ๋ ๋ณต์กํ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ๋ง๋ค ์ ์์ผ๋ฉฐ, ๊ธฐ๋ณธํ์ ๋ ๋ฐ๋ฅด๊ณ ํจ์จ์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฒ๋ฆฌํ๋ค๋ ํน์ง์ ๊ฐ์ง๋ค.
*๋ฆฌํฉํ ๋ง : ์ฝ๋๋ฅผ ์ดํดํ๊ธฐ ์ฌ์ด ์ฝ๋๋ก ์ฝ๋์ ์ค๊ณ๋ฅผ ๊ฐ์ ํ๋ ๊ฒ, ๊ธฐ๋ฅ์ ๋์ผํ๋ค.
๋ฐฐ์ด์ ์ด๊ธฐํ
๋ฐฐ์ด์ {}์ ์ฌ์ฉํด์ ์์ฑ๊ณผ ๋์์ ์ด๊ธฐํํ ์ ์๋ค.
int[] student;
student = new int[]{90,30,40,20,40}; //๋ฐฐ์ด ์์ฑ๊ณผ ์ด๊ธฐํ
์์ ์ด๊ธฐํํ ๋ฐฐ์ด์ ์๋์ ๊ฐ์ด๋ ๋ํ๋ผ ์ ์๋ค.
int[] student={90,30,40,20,40};
๋ฐ๋ฉด ์๋์ ์ฝ๋๋ ์๋ชป๋ ๋ฐฐ์ด ์์ฑ ์ฝ๋๋ค.
//์ค๋ฅ!!!
int[] student;
student={90,30,40,20,40};
2์ฐจ์ ๋ฐฐ์ด
- 2์ฐจ์ ๋ฐฐ์ด ์์ฑ๊ณผ ์ด๊ธฐํ
int [][] arr= new int[2][3]; //2x3 ๋ฐฐ์ด
2์ฐจ์ ๋ฐฐ์ด์ ์์ฑ์ ์์ ๊ฐ๋ค.
์์ฑ๊ณผ ์ด๊ธฐํ๋ฅผ ํจ๊ปํ๋ ๊ฒฝ์ฐ์๋ ์๋์ ๊ฐ์ด ๋ํ๋ธ๋ค.
int[][] arr = {
{1, 2, 3},
{4, 5, 6}
};
- ๋ฐฐ์ด์ ๊ธธ์ด
package array;
public class Array4_blog
{
public static void main(String[] args)
{
int[][]arr={
{1,2,3},
{4,5,6}
};
System.out.println(arr.length); //ํ์ ๊ธธ์ด: 2
System.out.println(arr[0].length); //์ด์ ๊ธธ์ด: 3
}
}
ํ๋ฆฐ ๋ถ๋ถ์ด๋ ์ด์ํ ๋ถ๋ถ์ด ์์ผ๋ฉด ๋๊ธ๋ก ์ง์ ํด์ฃผ์ธ์!
๊ฐ์ฌํฉ๋๋ค :)
'๐ป > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] ์์ฑ์์ this. (1) | 2024.08.10 |
---|---|
[JAVA] ๊ธฐ๋ณธํ๊ณผ ์ฐธ์กฐํ | ๋ฉ์๋ ํธ์ถ | ๋ฐ์ดํฐ ํ์ (1) | 2024.07.30 |
[JAVA] ํด๋์ค(Class), ๊ฐ์ฒด, ์ธ์คํด์ค (0) | 2024.07.18 |
[JAVA] ๋ฉ์๋(Method), ๋งค๊ฐ๋ณ์(Parameter), ๋ฉ์๋ ์ค๋ฒ๋ก๋ฉ (0) | 2024.07.15 |
[JAVA] Scanner ์ด์ฉํด ์ ๋ ฅ ๋ฐ๊ธฐ (2) | 2024.07.14 |