# ๋ฐฐ์—ด (Array)

written by sohyeon, hyemin ๐Ÿ’ก


# 1. ๋ฐฐ์—ด์ด๋ž€?

๊ฐ™์€ ์ž๋ฃŒํ˜•์˜ ๊ตฌ์„ฑ์š”์†Œ๊ฐ€ ์ง์„ ๋ชจ์–‘์œผ๋กœ ์—ฐ์†ํ•˜์—ฌ ์ค„์ง€์–ด ์žˆ๋Š” ๋‹จ์ˆœํ•œ ์ž๋ฃŒ๊ตฌ์กฐ ์ด๋‹ค.

# ex) ์‚ฌ์šฉ ์˜ˆ์‹œ

// ๋‘๊ฐ€์ง€ ์„ ์–ธ ๋ฐฉ๋ฒ•์ด ์žˆ์Œ
int[] a;
int a[];

// ๋ฐฐ์—ด ์ƒ์„ฑ
a = new int[5];

a[1] = 37;
a[2] = 20;
a[3] = 11;
a[4] = 1;

for(int i=0; i<a.length; i++)
    System.out.println("a["+i+"]="+a[i]);

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ

a[0] = 0 
a[1] = 37
a[2] = 20
a[3] = 11
a[4] = 1

new int[5]๋กœ ๋ฐฐ์—ด ๋ณธ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ณ€์ˆ˜ a๊ฐ€ ๋ฐฐ์—ด ๋ณธ์ฒด๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.

์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด a[0]์— ๊ฐ’์„ ๋Œ€์ž…ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ ์ž๋™์œผ๋กœ 0 ๊ฐ’์ด ๋Œ€์ž…๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜์žˆ๋‹ค.
๋ฐฐ์—ด์ด ์ƒ์„ฑ๋˜๋ฉด ์ž๋™์œผ๋กœ ๊ฐ ์š”์†Œ๋“ค์ด 0์œผ๋กœ ์ดˆ๊ธฐํ™” ๋œ๋‹ค.

๋˜ํ•œ ๋ฐฐ์—ด a์˜ ์ž๋ฃŒํ˜•๊ณผ a[i] (i๋Š” ์š”์†Œ์˜ index๊ฐ’)์˜ ์ž๋ฃŒํ˜•์€ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„๋‘์ž. ๋ฐฐ์—ด a๋Š” int[5]ํ˜• ์ž๋ฃŒํ˜•, ์ด 5๊ฐœ์˜ intํ˜• ์ €์žฅ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๋Š” ๊ฒƒ์ด๊ณ  a[i]๋Š” intํ˜• ์ž๋ฃŒํ˜•์„ ๊ฐ–๋Š”๋‹ค.

# 2. ๋ฉ”์†Œ๋“œ

  • ๋ฐฐ์—ด ๋ณ€์ˆ˜ ์ด๋ฆ„.length : ๊ธธ์ด๋ฅผ ๊ตฌํ•œ๋‹ค.

  • ๋ฐฐ์—ด ์ด๋ฆ„.clone() : ๋ฐฐ์—ด์„ ๋ณต์ œํ•œ๋‹ค.

  • maxOf(๋ฐฐ์—ด ์ด๋ฆ„) : ๋ฐฐ์—ด ๊ตฌ์„ฑ์š”์†Œ ์ค‘ ์ตœ๋Œ€ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.

Last Updated: 12/5/2020, 11:36:44 AM