CodeGym /Java Course /๋ชจ๋“ˆ 1 /for-each ๋ฃจํ”„

for-each ๋ฃจํ”„

๋ชจ๋“ˆ 1
๋ ˆ๋ฒจ 17 , ๋ ˆ์Šจ 2
์‚ฌ์šฉ ๊ฐ€๋Šฅ

1. ์ดํ„ฐ๋ ˆ์ดํ„ฐ๊ฐ€ ์ƒ๊ฒจ๋‚œ ๋ฐฐ๊ฒฝ

๋‹น์‹ ์€ ์ด๋ฏธ ์— ์ต์ˆ™ํ•ฉ๋‹ˆ๋‹ค HashSet. ๊ฐ•์˜๋ฅผ ์ฝ๋Š” ๊ฒƒ ์ด์ƒ์œผ๋กœ ์‹ค์ œ๋กœ ์กฐ์‚ฌํ–ˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์งˆ๋ฌธ์„ ํ–ˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ํ™”๋ฉด์— ๋ชจ๋“  HashSet ์š”์†Œ ๋ชฉ๋ก์„ ์–ด๋–ป๊ฒŒ ํ‘œ์‹œํ•ฉ๋‹ˆ๊นŒ? ๊ฒฐ๊ตญ ์ธํ„ฐํŽ˜์ด์Šค์—๋Š” get()๋ฉ”์„œ๋“œ ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค set()!

๊ทธ๋ฆฌ๊ณ  HashSet์ด ํ•œ๊ณ„๋Š” ํ˜ผ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ์ด์™ธ์—๋„ HashSet์š”์†Œ์— ์ •์˜๋œ ์ˆœ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค๋กœ ์š”์†Œ๋ฅผ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์—†๋„๋ก ํ•˜๋Š” ๋‹ค๋ฅธ ๋งŽ์€ ์ปฌ๋ ‰์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ˆ˜๋…„ ๋™์•ˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ๊ทธ๋ž˜ํ”„ ๋ฐ ํŠธ๋ฆฌ์™€ ๊ฐ™์€ ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๋งŽ์ด ๋ฐœ๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜๋Š” ๋ชฉ๋ก ๋ชฉ๋ก.

๋งŽ์€ ์ปจํ…Œ์ด๋„ˆ๋Š” ์ƒˆ ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋˜๊ฑฐ๋‚˜ ๊ธฐ์กด ์š”์†Œ๊ฐ€ ์ œ๊ฑฐ๋  ๋•Œ ํ•ด๋‹น ์š”์†Œ์˜ ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ชฉ๋ก์€ ํŠน์ • ์ˆœ์„œ๋กœ ์š”์†Œ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ƒˆ ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด ๊ฑฐ์˜ ํ•ญ์ƒ ๋ชฉ๋ก ์ค‘๊ฐ„์— ์‚ฝ์ž…๋ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ ์š”์†Œ๋ฅผ ์ €์žฅํ•˜์ง€๋งŒ ๊ณ ์ •๋œ ์ˆœ์„œ๋กœ ์ €์žฅํ•˜์ง€ ์•Š๋Š” ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์žˆ๋Š” ์ƒํ™ฉ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์ œ ์ด๋Ÿฌํ•œ ์ปฌ๋ ‰์…˜์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ โ€‹โ€‹๋ฐฐ์—ด์ด๋‚˜ ๋ชฉ๋ก์— ๋ณต์‚ฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ์š”์†Œ๋ฅผ โ€‹โ€‹๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์š”์†Œ๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ์ˆœ์„œ๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ค‘์š”ํ•œ ๊ฒƒ์€ ๋™์ผํ•œ ์š”์†Œ๋ฅผ ๋‘ ๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ ํ•˜์ฃ ?


2. ์ปฌ๋ ‰์…˜์˜ ๋ฐ˜๋ณต์ž

๋ฐ˜๋ณต์ž๋Š” ์œ„์˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ํ•ด๊ฒฐ์ฑ…์œผ๋กœ ์ œ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋ฐ˜๋ณต์ž๋Š” ์ปฌ๋ ‰์…˜๊ณผ ์—ฐ๊ฒฐ๋œ ํŠน์ˆ˜ ๊ฐœ์ฒด๋กœ, ๋ฐ˜๋ณตํ•˜์ง€ ์•Š๊ณ  ์ปฌ๋ ‰์…˜์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ โ€‹โ€‹์ˆœํšŒํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ์ปฌ๋ ‰์…˜์— ๋Œ€ํ•œ ๋ฐ˜๋ณต์ž๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Iterator<Type> it = name.iterator();

์—ฌ๊ธฐ์„œ name์ปฌ๋ ‰์…˜ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ Type์ปฌ๋ ‰์…˜ ์š”์†Œ์˜ ์œ ํ˜•์ด๊ณ  iterator()์ปฌ๋ ‰์…˜์˜ ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜์ด๋ฉฐ it๋ฐ˜๋ณต์ž ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค.

๋ฐ˜๋ณต์ž ๊ฐ์ฒด์—๋Š” 3๊ฐ€์ง€ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ• ์„ค๋ช…
Type next()
์ปฌ๋ ‰์…˜์˜ ๋‹ค์Œ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
boolean hasNext()
์•„์ง ํ†ต๊ณผํ•˜์ง€ ์•Š์€ ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
void remove()
์ปฌ๋ ‰์…˜์˜ ํ˜„์žฌ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๋ฉ”์„œ๋“œ๋Š” Scanner ํด๋ž˜์Šค์˜ nextInt)๋ฐ hasNextInt()๋ฉ”์„œ๋“œ์™€ ๋‹ค์†Œ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์ด next()๋ฉ”์„œ๋“œ๋Š” ๋ฐ˜๋ณต์ž๋ฅผ ๊ฐ€์ ธ์˜จ ์ปฌ๋ ‰์…˜์˜ ๋‹ค์Œ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋ฉ”์„œ๋“œ hasNext()๋Š” ๋ฐ˜๋ณต์ž๊ฐ€ ์•„์ง ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์€ ์ถ”๊ฐ€ ์š”์†Œ๊ฐ€ ์ปฌ๋ ‰์…˜์— ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ a ์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ โ€‹โ€‹ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค HashSet.

์•”ํ˜ธ ๋…ธํŠธ
HashSet<String> set = new HashSet<String>();

set.add("Hallo");
set.add("Hello");
set.add("Hola");
set.add("Bonjour");
set.add("Ciao");
set.add("Namaste");

Iterator<String> it = set.iterator();
while (it.hasNext())
{
   String str = it.next();
   System.out.println(str);
}
HashSet์š”์†Œ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค String.


๋ณ€์ˆ˜ ์— ๋‹ค์–‘ํ•œ ์–ธ์–ด๋กœ ์ธ์‚ฌ๋ง์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค set.




์ง‘ํ•ฉ ์— ๋Œ€ํ•œ ๋ฐ˜๋ณต์ž ๊ฐœ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค set.
์•„์ง ์š”์†Œ๊ฐ€ ์žˆ๋Š” ํ•œ

๋‹ค์Œ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
์š”์†Œ๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.


3. For-each๋ฃจํ”„

๋ฐ˜๋ณต์ž์˜ ์ฃผ์š” ๋‹จ์ ์€ for๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์ฝ”๋“œ๊ฐ€ ๋” ๋ณต์žกํ•ด์ง„๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

for๋น„๊ต๋ฅผ ์œ„ํ•ด ๋ฃจํ”„์™€ ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชฉ๋ก์„ ํ‘œ์‹œํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค .

๋ฐ˜๋ณต์ž for ๋ฃจํ”„
ArrayList<String> list = new ArrayList<String>();

Iterator<String> it = list.iterator();
while (it.hasNext())
{
   String str = it.next();
   System.out.println(str);
}
ArrayList<String> list = new ArrayList<String>();

for (int i = 0; i < list.size(); i++)
{
   String str = list.get(i);
   System.out.println(str);
}

์˜ˆ, using ๋ฃจํ”„์˜ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ๋‚ซ์Šต๋‹ˆ๋‹ค ArrayList. ๋ชจ๋“  ๊ฒƒ์ด ๋” ์งง์•„์ง‘๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ Java ์ œ์ž‘์ž๋Š” ๋‹ค์‹œ ์šฐ๋ฆฌ์—๊ฒŒ ์•ฝ๊ฐ„์˜ ์„คํƒ•์„ ๋ถ“๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ์šด ์ข‹๊ฒŒ๋„ ๊ตฌ๋ฌธ ์„คํƒ• ์ด์—ˆ์Šต๋‹ˆ๋‹ค .

๊ทธ๋“ค์€ Java์— ์ƒˆ๋กœ์šด ์ข…๋ฅ˜์˜ ๋ฃจํ”„๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์ด๋ฅผ for-each๋ฃจํ”„๋ผ๊ณ  ๋ถˆ๋ €์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณด์ž…๋‹ˆ๋‹ค.

for(Type name:collection)

์—ฌ๊ธฐ์„œ collection๋Š” ์ปฌ๋ ‰์…˜ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด๊ณ , Type๋Š” ์ปฌ๋ ‰์…˜์— ์žˆ๋Š” ์š”์†Œ์˜ ์œ ํ˜•์ด๋ฉฐ, name๋ฃจํ”„๊ฐ€ ๋ฐ˜๋ณต๋  ๋•Œ๋งˆ๋‹ค ์ปฌ๋ ‰์…˜์—์„œ ๋‹ค์Œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์ข…๋ฅ˜์˜ ๋ฃจํ”„๋Š” ์•”์‹œ์  ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปฌ๋ ‰์…˜์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ โ€‹โ€‹๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์‹ค์ œ๋กœ ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

for-each ๋ฃจํ”„ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋ณด๋Š” ๊ฒƒ: ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•œ ๋ฃจํ”„
ArrayList<String> list = new ArrayList<String>();

for (String str: list)
{
   System.out.println(str);
}
ArrayList<String> list = new ArrayList<String>();
Iterator<String> it = list.iterator();

while (it.hasNext())
{
   String str = it.next();
   System.out.println(str);
}

์ปดํŒŒ์ผ๋Ÿฌ๋Š” for-each์ฝ”๋“œ์—์„œ ๋ฃจํ”„๋ฅผ ๋งŒ๋‚˜๋ฉด ๊ฐ„๋‹จํžˆ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ์ฝ”๋“œ๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. ๋ˆ„๋ฝ๋œ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ๊ณผ ํ•จ๊ป˜ ๋ฐ˜๋ณต์ž๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ˜ธ์ถœ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ๋ฃจํ”„๋ฅผ ์ข‹์•„ for-eachํ•˜๊ณ  ์ปฌ๋ ‰์…˜์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ โ€‹โ€‹๋ฐ˜๋ณตํ•ด์•ผ ํ•  ๋•Œ ๊ฑฐ์˜ ํ•ญ์ƒ ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

ArrayList๋ฃจํ”„ ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชฉ๋ก์„ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์กฐ์ฐจ for-each๋” ์งง์•„ ๋ณด์ž…๋‹ˆ๋‹ค.

for-each ๋ฃจํ”„ for ๋ฃจํ”„
ArrayList<String> list = new ArrayList<String>();

for (String str: list)
{
   System.out.println(str);
}
ArrayList<String> list = new ArrayList<String>();

for (int i = 0; i < list.size(); i++)
{
   String str = list.get(i);
   System.out.println(str);
}


for-each4. ๋ฃจํ”„ ์—์„œ ์š”์†Œ ์ œ๊ฑฐ

๋ฃจํ”„ for-each์—๋Š” ํ•œ ๊ฐ€์ง€ ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์š”์†Œ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ œ๊ฑฐํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์•”ํ˜ธ ๋ฉ”๋ชจ
ArrayList<String> list = new ArrayList<String>();

list.add("Hallo");
list.add("Hello");
list.add("Hola");
list.add("Bonjour");
list.add("Ciao");
list.add("Namaste");

for (String str: list)
{
   if (str.equals("Hello"))
      list.remove(str);
}












์ œ๊ฑฐ ์ž‘์—…์€ ์˜ค๋ฅ˜๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค!

์ด๊ฒƒ์€ ๋งค์šฐ ํ›Œ๋ฅญํ•˜๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šด ์ฝ”๋“œ์ด์ง€๋งŒ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ค‘์š”ํ•œ!

๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆœํšŒํ•˜๋Š” ๋™์•ˆ์—๋Š” ์ปฌ๋ ‰์…˜์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์ด ์ œํ•œ์„ ์šฐํšŒํ•˜๋Š” ์„ธ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

1. ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ๋ฃจํ”„ ์‚ฌ์šฉ

When traversing an ArrayList collection, ์นด์šดํ„ฐ ๋ณ€์ˆ˜์™€ ํ•จ๊ป˜ ์ผ๋ฐ˜ ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค i.

์•”ํ˜ธ
for (int i = 0; i < list.size(); i++)
{
   String str = list.get(i);

   if (str.equals("Hello"))
   {
      list.remove(str);
      i--; // We need to decrease i, because the remove operation shifted the elements
   }
}

๊ทธ๋Ÿฌ๋‚˜ ์ด ์˜ต์…˜์€ ์ปฌ๋ ‰์…˜์— ์ ํ•ฉํ•˜์ง€ HashSet์•Š์Šต๋‹ˆ๋‹ค HashMap.

2. ๋ช…์‹œ์  ์ดํ„ฐ๋ ˆ์ดํ„ฐ ์‚ฌ์šฉ

๋ฐ˜๋ณต์ž๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ํ•ด๋‹น remove()๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž‘๋™ํ•˜๋Š” ๋ฒ„์ „ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ๋ฒ„์ „
Iterator<String> it = set.iterator();
while (it.hasNext())
{
   String str = it.next();
   if (str.equals("Hello"))
       it.remove();
}

for (String str: list) { if (str.equals("Hello")) list.remove(str); }

remove()๋ฐ˜๋ณต์ž ๊ฐœ์ฒด์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜์„ธ์š” ! ๋ฐ˜๋ณต์ž๋Š” ํ•ญ๋ชฉ์ด ์ œ๊ฑฐ๋˜์—ˆ์Œ์„ ์ธ์‹ํ•˜๊ณ  ์ƒํ™ฉ์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3. ์ปฌ๋ ‰์…˜ ์‚ฌ๋ณธ ์‚ฌ์šฉ

์ปฌ๋ ‰์…˜์˜ ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“  ๋‹ค์Œ ๋ฃจํ”„์—์„œ ๋ณต์‚ฌ๋ณธ์„ ์‚ฌ์šฉํ•˜๊ณ  for-each์›๋ณธ ์ปฌ๋ ‰์…˜์—์„œ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์•”ํ˜ธ ๋ฉ”๋ชจ
ArrayList<String> listCopy = new ArrayList(list);

for (String str: listCopy)
{
   if (str.equals("Hello"))
      list.remove(str);
}
์ปฌ๋ ‰์…˜์˜ ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ๋งค์šฐ ์‰ฝ์Šต๋‹ˆ๋‹ค.



๋ฃจํ”„๋Š” ์ปฌ๋ ‰์…˜์˜ ๋ณต์‚ฌ๋ณธ์— ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
์ปฌ๋ ‰์…˜ ์—์„œ ์š”์†Œ๊ฐ€ ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค list.

์ปฌ๋ ‰์…˜์€ ์š”์†Œ ์ž์ฒด๊ฐ€ ๋ณต์ œ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์˜คํžˆ๋ ค ๋น ๋ฅด๊ฒŒ ๋ณต์‚ฌ๋ฉ๋‹ˆ๋‹ค. ๋Œ€์‹  ์ƒˆ ์ปฌ๋ ‰์…˜์€ ์ด์ „ ์ปฌ๋ ‰์…˜์— ์ด๋ฏธ ์žˆ๋Š” ์š”์†Œ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

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