CodeGym /Java Course /All lectures for KO purposes /๋ฐฐ์—ด โ€” ํ—ฌํผ ํด๋ž˜์Šค

๋ฐฐ์—ด โ€” ํ—ฌํผ ํด๋ž˜์Šค

All lectures for KO purposes
๋ ˆ๋ฒจ 1 , ๋ ˆ์Šจ 529
์‚ฌ์šฉ ๊ฐ€๋Šฅ

"์•„๋ฏธ๊ณ , ์ด์ œ ๋ฐฐ์—ด์ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ์–ผ๋งˆ๋‚˜ ์œ ์šฉํ•œ์ง€ ๊นจ๋‹ฌ์•˜๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค."

"ํ™•์‹คํžˆ, ๋ฆฌ์‹œ! ๋‚œ ์ด๋ฏธ ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ณผ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์–ด."

"์ž‘์—…์— ๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์ด ์žˆ์—ˆ์Šต๋‹ˆ๊นŒ? ๋ฐ˜๋ณตํ•ด์„œ ์ˆ˜ํ–‰ํ•œ ์ž‘์—…์ž…๋‹ˆ๋‹ค."

"๋‚ด๊ฐ€ ๋‹น์‹ ์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ดํ•ดํ–ˆ๋‹ค๋ฉด ๋‹ค๋ฅธ ์ž‘์—…์—์„œ ์œ ์‚ฌํ•œ ์ž‘์—…์„ ์˜๋ฏธํ•ฉ๋‹ˆ๊นŒ? ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ๊ณณ์—์„œ โ€” ๊ทธ๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ๋„ˆ๋ฌด ์ง€๊ฒน์Šต๋‹ˆ๋‹ค!"

"์˜ˆ, ์ œ ๋ง์€ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. Java ์ œ์ž‘์ž๋Š” Java ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋ฐฐ์—ด ์ž‘์—…์„ ํ•  ๋•Œ ์ข…์ข… ๋™์ผํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ์•„์ฐจ๋ ธ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฐฐ์—ด์˜ ์ผ๋ถ€๋ฅผ ๋‹ค๋ฅธ ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•˜๋Š” ์ฝ”๋“œ ๋˜๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์…€์„ ๋™์ผํ•œ ๋‚ด์šฉ์œผ๋กœ ์ฑ„์šฐ๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” ์˜ˆ: ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์„ ํ™”๋ฉด์— ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ํ˜•์‹์œผ๋กœ ํ‘œ์‹œํ•˜๋Š” ์ฝ”๋“œ.

"์ง„์ •ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋จธ์—๊ฒŒ๋Š” ํ•œ ๊ฐ€์ง€ ๋งค์šฐ ์ค‘์š”ํ•œ ๊ทœ์น™์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์‹ ์„ ๋ฐ˜๋ณตํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค. ๋ถˆํ•„์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ž˜๋ชป์ด๋ผ๋Š” ๋ฐ ๋™์˜ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ž‘์—…์— ๋Œ€ํ•œ ๋Œ€๊ฐ€๋ฅผ ๋ฐ›์ง€ ๋ชปํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํšจ๊ณผ์ ์ธ ์ž‘์—…์€ ๋งŽ์€ ๋ณด์ˆ˜๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ €๋ฅผ ๋ฏฟ์œผ์„ธ์š”. ๋ง๋ถ™์—ฌ์„œ, ์ดˆ๋ณด์ž์˜ ์ฝ”๋“œ๋Š” ๋งŽ์€ ์–‘์˜ ๋ฐ˜๋ณต ์ฝ”๋“œ๋กœ ์ฆ‰์‹œ ์ธ์‹ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

"Java์˜ ์ œ์ž‘์ž๋Š” ์ด๊ฒƒ์„ ๊ณ ๋ฏผํ•˜๊ณ  ํŠน์ˆ˜ Arraysํด๋ž˜์Šค(์ •์‹ ์ด๋ฆ„์€ java.util.Arrays)๋ฅผ ๋งŒ๋“ค์–ด ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ๋ฐฐ์—ด ๊ด€๋ จ ์ž‘์—…์„ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค."

"์Œ... ์•„์ฃผ ํฅ๋ฏธ๋กญ๊ตฐ. ๊ทธ๋Ÿผ ์ •ํ™•ํžˆ ๋ญ๊ฐ€ ๋“ค์–ด์žˆ์ง€?

"๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ๋งŽ์€ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ ๋จผ์ € ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๊ณ  ๊ฐ€์žฅ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” 10๊ฐ€์ง€ ๋ฐฉ๋ฒ•๋งŒ ๊ณ ๋ คํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Arrays.toString()

"์šฐ๋ฆฌ๊ฐ€ ์‚ดํŽด๋ณผ ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ Arrays.toString(). ๊ทธ๋Ÿฌ๋‚˜ ๋จผ์ € ์•ฝ๊ฐ„์˜ ๋ฐฐ๊ฒฝ ์ง€์‹์ž…๋‹ˆ๋‹ค.

"Java์˜ ๊ฐ ๋ฐฐ์—ด์—๋Š” toString()'๋ฐฐ์—ด์˜ ํ…์ŠคํŠธ ํ‘œํ˜„'์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ช…๋ น๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์˜ ํ…์ŠคํŠธ ํ‘œํ˜„์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

String str = name.toString();

" name๋ฐฐ์—ด ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ ์–ด๋””์— ์žˆ๊ณ  str๋ฐฐ์—ด์˜ ๋ฌธ์ž์—ด ํ‘œํ˜„์„ ์ €์žฅํ•  ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค. ์•„๋ฏธ๊ณ , ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ํ™”๋ฉด์— ๋ฐฐ์—ด์„ ์ธ์‡„ํ•ด ๋ณด์…จ์Šต๋‹ˆ๊นŒ ?System.out.println(name)

๋‚˜๋Š” ๊ณ ๋ฐฑํ•œ๋‹ค, ๊ทธ๋Ÿฐ ์ผ์ด ์žˆ์—ˆ๋‹ค. ๋‚˜๋Š” ํšก์„ค์ˆ˜์„ค์„ ๋ณด์•˜๋‹ค. ๋‚˜๋Š” ์œ„ํ—˜์„ ํ”ผํ•˜๊ณ  ๋ฃจํ”„๋ฅผ ๊ณ„์† ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

"๋Œ€๋ถ€๋ถ„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ์„ ๋ณด์•˜์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

I@37afeb11

"์ฒซ ๊ธ€์ž๋Š” ๋ฐฐ์—ด I์ด๋ผ๋Š” ๋œป์ด๊ณ  ๊ทธ ๋’ค์˜ ๊ธฐํ˜ธ๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ์žˆ๋Š” ๋‹จ์ผ ๊ฐ์ฒด์˜ ์ฃผ์†Œ์ž…๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ์— ์žˆ๋Š” ๋ฐฐ์—ด์˜ ์ฃผ์†Œ๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•œํŽธ์œผ๋กœ๋Š” ์ด๊ฒƒ์ด ๋ฐ”๋กœ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋œ ์ •๋ณด์ž…๋‹ˆ๋‹ค. ๋ฐฐ์—ด ๋ณ€์ˆ˜์ง€๋งŒ ๋‹ค๋ฅธ ํ•œํŽธ์œผ๋กœ๋Š” ๋ญ”๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ์„ ์˜ˆ์ƒํ•˜์…จ์ฃ ?"int@

"์ •ํ™•ํ•ฉ๋‹ˆ๋‹ค! ๋ฐฐ์—ด์˜ ๊ฐ’์„ ๋ณผ ๊ณ„ํš์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์ œ๊ฐ€ ์˜ˆ์ƒํ–ˆ๋˜ ๋งˆ์ง€๋ง‰ ์ผ์ด์—ˆ์Šต๋‹ˆ๋‹ค."

Arrays.toString()"๊ทธ๋ฆฌ๊ณ  ๊ทธ๊ฒƒ์ด ๋ฐ”๋กœ ๊ทธ๋“ค์ด ๋ฐฐ์—ด์˜ ๊ฐ’์„ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ณ ์•ˆํ•œ ์ด์œ ์ž…๋‹ˆ๋‹ค . ์šฐ๋ฆฌ๋Š” ๊ทธ๊ฒƒ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

String str = Arrays.toString(name);

์˜ˆ:

int[] array = {1, 2, 3};
String str = Arrays.toString(array);
๋ณ€์ˆ˜ str์—๋Š” ๋ฌธ์ž์—ด์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค."[1, 2, 3]"
int[] array = {};
String str = Arrays.toString(array);
๋ณ€์ˆ˜ str์—๋Š” ๋ฌธ์ž์—ด์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค."[]"
String[] array = {"Hi", "How's", "life?"};
String str = Arrays.toString(array);
๋ณ€์ˆ˜ str์—๋Š” ๋ฌธ์ž์—ด์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค."[Hi, How's, life?]"

Arrays.deepToString()

"๊ทธ๋Ÿฌ๋‚˜ ์ด toString()๋ฐฉ๋ฒ•์€ 2์ฐจ์› ๋ฐฐ์—ด์— ๊ด€ํ•ด์„œ๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๊ณ  ํ•˜๋ฉด Arrays.toString()์ต์ˆ™ํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[I@37afeb11, I@37afeb21, I@37afeb31]

"์ด๊ฒƒ์€ ๋ชจ๋‘ 2์ฐจ์› ๋ฐฐ์—ด์˜ ์…€์ด 1์ฐจ์› ๋ฐฐ์—ด์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  1์ฐจ์› ๋ฐฐ์—ด์€ ์–ด๋–ป๊ฒŒ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๊นŒ? ์œ„์—์„œ ๋ณธ ๊ฒƒ๊ณผ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค.

"๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? 2์ฐจ์› ๋ฐฐ์—ด์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํ‘œ์‹œํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”?"

"Java์˜ ์ œ์ž‘์ž๋„ ์ด๊ฒƒ์„ ์˜ˆ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด Arraysํด๋ž˜์Šค์—๋Š” ๋˜ ๋‹ค๋ฅธ ํŠน์ˆ˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. deepToString(). ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

String str = Arrays.deepToString(name);

"์ด ๋ฉ”์„œ๋“œ๋Š” 2์ฐจ์›, 1์ฐจ์›, 3์ฐจ์› ๋˜๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋“  ์ฐจ์›์ธ ๋ฐฐ์—ด์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ•ญ์ƒ ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ:

int[] array = {1, 2, 3};
String str = Arrays.deepToString(array);
๋ณ€์ˆ˜ str์—๋Š” ๋ฌธ์ž์—ด์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค."[1, 2, 3]"
int[][] array = { {1, 1}, {2, 2}, {3, 3} };
String str = Arrays.deepToString(array);
๋ณ€์ˆ˜ str์—๋Š” ๋ฌธ์ž์—ด์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค."[[1, 1], [2, 2], [3, 3]]"
int[][][] array = { {{1, 2, 3}, {1}}, {{}} };
String str = Arrays.deepToString(array);
๋ณ€์ˆ˜ str์—๋Š” ๋ฌธ์ž์—ด์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค."[[[1, 2, 3], [1]], [[]]]"

Arrays.equals()

"์šฐ๋ฆฌ๋Š” ํ™”๋ฉด์— ๋ฐฐ์—ด์„ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ƒˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์—ด ๋น„๊ต๋Š” ์–ด๋–ป์Šต๋‹ˆ๊นŒ? ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ๊ธฐ์–ตํ•˜์‹ญ๋‹ˆ๊นŒ?"

"๋ณดํ†ต equals๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค!

"์˜ˆ, equals๋ฐ ๋˜ํ•œ equalsIgnoreCase(๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•จ).

"์ข‹์€ ์†Œ์‹: ๋ฐฐ์—ด์— ๋Œ€ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค equals. ๋‚˜์œ ์†Œ์‹: ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์„ ๋น„๊ตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. equals๋ฐฐ์—ด์˜ ๋ฉ”์„œ๋“œ๋Š” ์—ฐ์‚ฐ์ž์™€ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค ==. ์ฐธ์กฐ๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ:

int[] x1 = {1, 2, 3};
int[] x2 = {1, 2, 3};
x1 == x2;
false(์ฐธ์กฐ๊ฐ€ ๊ฐ™์ง€ ์•Š์Œ)
int[] x1 = {1, 2, 3};
int[] x2 = {1, 2, 3};
x1.equals(x2);
๋ฐฉ๋ฒ• equals์€ arrays๋‹จ์ˆœํžˆ ๋‘ ๋ฐฐ์—ด์˜ ์ฐธ์กฐ๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.

false(์ฐธ์กฐ๊ฐ€ ๊ฐ™์ง€ ์•Š์Œ)

"๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ๋ฌด์—‡์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๊นŒ? ๋‚ด์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐฐ์—ด์„ ๋น„๊ตํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•ฉ๋‹ˆ๊นŒ?"

"๋‹ค์‹œ Arraysํด๋ž˜์Šค๊ฐ€ ์šฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด์ค๋‹ˆ๋‹ค. ๋” ๊ตฌ์ฒด์ ์œผ๋กœ, ๊ทธ Arrays.equals()๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์šฐ๋ฆฌ๊ฐ€ ๋ถ€๋ฅด๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

Arrays.equals(name1, name2)

true" ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™๊ณ  ํ•ด๋‹น ์š”์†Œ๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค . ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค false.

์˜ˆ:

int[] x1 = {1, 2, 3};
int[] x2 = {1, 2, 3};
x1.equals(x2);
๋ฐฉ๋ฒ• equals์€ arrays๋‹จ์ˆœํžˆ ๋‘ ๋ฐฐ์—ด์˜ ์ฐธ์กฐ๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.

false(์ฐธ์กฐ๊ฐ€ ๊ฐ™์ง€ ์•Š์Œ)
int[] x1 = {1, 2, 3};
int[] x2 = {1, 2, 3};
Arrays.equals(x1, x2);


true(๋‚ด์šฉ์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค)
int[] x1 = {1, 2, 3};
int[] x2 = {1, 2, 3, 4};
Arrays.equals(x1, x2);


false(๋ฐฐ์—ด์˜ ๋‚ด์šฉ์ด ๋‹ค๋ฆ„)

Arrays.deepEquals()

"๊ทธ๋ฆฌ๊ณ  ์ด๋ฏธ ์ง์ž‘ํ•˜์…จ๊ฒ ์ง€๋งŒ ์ด Arrays.equals๋ฐฉ๋ฒ•์€ 2์ฐจ์› ๋ฐฐ์—ด์— ๋Œ€ํ•ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ 2์ฐจ์› ๋ฐฐ์—ด์„ 1์ฐจ์› ๋ฐฐ์—ด์˜ ์ฃผ์†Œ์ธ ์š”์†Œ๋ฅผ ๊ฐ€์ง„ 1์ฐจ์› ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.

"๋”ฐ๋ผ์„œ ๋‹ค์ฐจ์› ๋ฐฐ์—ด( n = 1, 2, 3,... )์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ๊ฐํ•ด ๋ƒˆ์Šต๋‹ˆ๋‹ค Arrays.deepEquals(). ํ˜ธ์ถœํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Arrays.deepEquals(name1, name2)

" true๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™๊ณ  ํ•ด๋‹น ์š”์†Œ๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.false. ๋ฐฐ์—ด ๋‚ด๋ถ€์˜ ์š”์†Œ๋„ ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ Arrays.deepEquals()๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฅผ ๋น„๊ตํ•˜๋Š” ์‹์ž…๋‹ˆ๋‹ค.

์˜ˆ:

int[][] x1 = {{1, 2, 3}, {4, 5, 6}};
int[][] x2 = {{1, 2, 3}, {4, 5, 6}};

x1.equals(x2);
๋ฐฉ๋ฒ• equals์€ arrays ๋‹จ์ˆœํžˆ ๋‘ ๋ฐฐ์—ด์˜ ์ฐธ์กฐ๋ฅผ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.

false(์ฐธ์กฐ๊ฐ€ ๊ฐ™์ง€ ์•Š์Œ)
int[][] x1 = {{1, 2, 3}, {4, 5, 6}};
int[][] x2 = {{1, 2, 3}, {4, 5, 6}};

Arrays.equals(x1, x2);
์ด ๋ฉ”์„œ๋“œ๋Š” ์ฐธ์กฐ๋ฅผ ์ €์žฅํ•˜๋Š” 1์ฐจ์› ๋ฐฐ์—ด๊ณผ ๋น„๊ต Arrays.equalsํ•ฉ๋‹ˆ๋‹ค . ๊ทธ๋“ค์€ ๋‹ค๋ฅธ ์ฐธ์กฐ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. (๋ฐฐ์—ด์˜ ๋‚ด์šฉ์ด ๊ฐ™์ง€ ์•Š์Œ) x1x2
false
int[][] x1 = {{1, 2, 3}, {4, 5, 6}};
int[][] x2 = {{1, 2, 3}, {4, 5, 6}};

Arrays.deepEquals(x1, x2);



true(๋‚ด์šฉ์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค)

"๊ณ ๋งˆ์›Œ์š”, Rishi! ์ด ์ˆ˜์—…์€ ๋ฏธ๋ž˜์˜ ๋‚ด ์‚ถ์„ ๋” ์‰ฝ๊ณ  ํ–‰๋ณตํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐ ๊ผญ ํ•„์š”ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ œ Arrays ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋‚ด ํ”„๋กœ๊ทธ๋žจ์„ ํ›จ์”ฌ ๋” ๋น ๋ฅด๊ฒŒ ์ž‘์„ฑํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

"๊ทธ๊ฒŒ ์ œ๊ฐ€ ๊ธฐ๋Œ€ํ•˜๊ณ  ์žˆ๋˜ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•˜ํ•˜. ํ•˜์ง€๋งŒ Arrays ํด๋ž˜์Šค์˜ ํฅ๋ฏธ๋กœ์šด ๋ฉ”์„œ๋“œ๊ฐ€ ์ „๋ถ€๋Š” ์•„๋‹™๋‹ˆ๋‹ค. ๋‹ค์Œ์— ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋ง์”€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค."

์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION