์๋
! ์ง๋ ์์
์์ ์ฐ๋ฆฌ๋ ํด๋์ค์ ๋ํด ์๊ฒ ๋์๊ณ
์ผ๋ฐ ๋ฐฐ์ด์์ ์์๋ฅผ ์ญ์ ํ๋ ๊ฒ์ด ๊ทธ๋ค์ง ํธ๋ฆฌํ์ง ์๋ค๊ณ ์ด๋ฏธ ์ธ๊ธํ์ต๋๋ค. ์์ ์์ฒด๋ฅผ ์ญ์ ํ ์ ์๊ธฐ ๋๋ฌธ์ ๊ฐ์ "์ ๋ก์์"(null๋ก ์ค์ )ํ ์๋ง ์์ต๋๋ค.
ArrayList
์ด ํด๋์ค๋ก ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ์์
์ ์ํํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ ์ต๋๋ค. ๋ํ an ArrayList
๊ณผ ์ผ๋ฐ ๋ฐฐ์ด์ ๋ช ๊ฐ์ง ์ฐจ์ด์ ์ ์ง์ ํ์ต๋๋ค. ๊ทธ๋ฌ๋ ์ฐ๋ฆฌ๋ ํ ๊ฐ์ง ์ฃผ์ , ์ฆArrayList
. ์ง๊ธ ๋
ผ์ํ๊ฒ ์ต๋๋ค. 
public class Cat {
private String name;
public Cat(String name) {
this.name = name;
}
public static void main(String[] args) {
Cat[] cats = new Cat[3];
cats[0] = new Cat("Thomas");
cats[1] = new Cat("Behemoth");
cats[2] = new Cat("Lionel Messi");
cats[1] = null;
System.out.println(Arrays.toString(cats));
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
'}';
}
}
์ถ๋ ฅ: [Cat{name='Thomas'}, null, Cat{name='Lionel Messi'}] ๊ทธ๋ฌ๋ ๋ฐฐ์ด ์์๋ฅผ null๋ก ์ค์ ํ๋ฉด "๊ตฌ๋ฉ"์ด ์๊น๋๋ค. ๋ฐฐ์ด์์ ์์น๋ฅผ ์ ๊ฑฐํ์ง ์๊ณ ํด๋น ๋ด์ฉ๋ง ์ ๊ฑฐํ์ต๋๋ค. 50๋ง๋ฆฌ์ ๊ณ ์์ด ๋ฐฐ์ด์ด ์๊ณ ๊ทธ ์ค 17๋ง๋ฆฌ๋ฅผ ์ด๋ฐ ์์ผ๋ก ์ ๊ฑฐํ๋ฉด ์ด๋ค ์ผ์ด ์ผ์ด๋ ์ง ์์ํด ๋ณด์ญ์์ค. ์ฐ๋ฆฌ๋ 17๊ฐ์ ๊ตฌ๋ฉ์ ๊ฐ์ง ๋ฐฐ์ด์ ๊ฐ๊ฒ ๋ ๊ฒ์
๋๋ค. ๊ณ์ ์ถ์ ํด ๋ณด์ธ์! ์ ๊ฐ์ ์ธ ์ ์๋ ๋น ์
์ ์๋ฅผ ๊ธฐ์ตํ๋ ๊ฒ์ ๋นํ์ค์ ์
๋๋ค. ํ ๊ฐ์ง ์ค์๋ฅผ ํ๋ฉด ์ํ๋ ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ๋ฎ์ด์ฐ๊ฒ ๋ฉ๋๋ค. ๋ฌผ๋ก ์ด๋ฅผ ์ข ๋ ์ ์คํ๊ฒ ์ํํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. ์์๋ฅผ ์ ๊ฑฐํ ํ ์์๋ฅผ ๋ฐฐ์ด์ ๋งจ ์์ผ๋ก ์ด๋ํ์ฌ ๋์ "๊ตฌ๋ฉ"์ ๋ฃ์ต๋๋ค.
public static void main(String[] args) {
Cat[] cats = new Cat[4];
cats[0] = new Cat("Thomas");
cats[1] = new Cat("Behemoth");
cats[2] = new Cat("Lionel Messi");
cats[2] = new Cat("Fluffy");
cats[1] = null;
for (int i = 2; i < cats.length-1; i++) {
cats [i-1] = cats [i];// Move the elements to the front of the array, so the empty position is at the end
}
System.out.println(Arrays.toString(cats));
}
์ถ๋ ฅ: [Cat{name='Thomas'}, Cat{name='Fluffy'}, Cat{name='Fluffy'}, null] ์ด๊ฒ์ ๋ ์ข์ ๋ณด์ด์ง๋ง ๊ฐ๋ ฅํ ์๋ฃจ์
์ด๋ผ๊ณ ํ ์๋ ์์ต๋๋ค. ๋ฐฐ์ด์์ ์์๋ฅผ ์ญ์ ํ ๋๋ง๋ค ์ด ์ฝ๋๋ฅผ ์์ฑํด์ผ ํ๋ค๋ ์ฌ์ค ์ธ์ ๋ค๋ฅธ ์ด์ ๊ฐ ์๋ค๋ฉด! ์ด๊ฒ์ ๋์ ์ ํ์
๋๋ค. ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ๋ณ๋์ ๋ฉ์๋๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
public void deleteCat(Cat[] cats, int indexToDelete) {
//...delete the cat corresponding to the index and move the elements
}
๊ทธ๋ฌ๋ ์ด๊ฒ์ ๊ฑฐ์ ์ฌ์ฉ๋์ง ์์ต๋๋ค. ์ด ๋ฐฉ๋ฒ์ ๊ฐ์ฒด ์๋ง ์ฌ์ฉํ ์ Cat
์๊ณ ๋ค๋ฅธ ์ ํ์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ฆ, ํ๋ก๊ทธ๋จ์ ๋ฐฐ์ด๊ณผ ํจ๊ป ์ฌ์ฉํ๋ ค๋ ๋ ๋ค๋ฅธ 100๊ฐ์ ํด๋์ค๊ฐ ์๋ ๊ฒฝ์ฐ ๊ฐ ํด๋์ค์์ ์ ํํ ๋์ผํ ๋
ผ๋ฆฌ๋ก ๋์ผํ ๋ฉ์๋๋ฅผ ์์ฑํด์ผ ํฉ๋๋ค. ์ด๊ฒ์ ์์ ํ ์ฌ์์
๋๋ค -_- ํ์ง๋ง ArrayList
ํด๋์ค๋ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํฉ๋๋ค! ์์๋ฅผ ์ ๊ฑฐํ๊ธฐ ์ํ ํน์ ๋ฉ์๋๋ฅผ ๊ตฌํํฉ๋๋ค.remove()
public static void main(String[] args) {
ArrayList<Cat> cats = new ArrayList<>();
Cat thomas = new Cat("Thomas");
Cat behemoth = new Cat("Behemoth");
Cat lionel = new Cat("Lionel Messi");
Cat fluffy = new Cat ("Fluffy");
cats.add(thomas);
cats.add(behemoth);
cats.add(lionel);
cats.add(fluffy);
System.out.println(cats.toString());
cats.remove(1);
System.out.println(cats.toString());
}
๊ฐ์ฒด์ ์ธ๋ฑ์ค๋ฅผ ๋ฉ์๋์ ์ ๋ฌํ๋ฉด ๊ฐ์ฒด๋ฅผ ์ญ์ ํฉ๋๋ค(๋ฐฐ์ด์์์ฒ๋ผ). ์ด remove()
๋ฐฉ๋ฒ์๋ ๋ ๊ฐ์ง ํน๋ณํ ๊ธฐ๋ฅ์ด ์์ต๋๋ค. ์ฒซ์งธ, "๊ตฌ๋ฉ"์ ๋จ๊ธฐ์ง ์์ต๋๋ค. ์ด์ ์ ์ง์ ์์ฑํ ์์๊ฐ ์ค๊ฐ์์ ์ ๊ฑฐ๋ ๋ ์์๋ฅผ ์ด๋ํ๋ ๋ฐ ํ์ํ ๋
ผ๋ฆฌ๋ฅผ ์ด๋ฏธ ๊ตฌํํฉ๋๋ค . ์ด์ ์ฝ๋์ ์ถ๋ ฅ์ ์ดํด๋ณด์ญ์์ค.
[Cat{name='Thomas'}, Cat{name='Behemoth'}, Cat{name='Lionel Messi'}, Cat{name='Fluffy'}]
[Cat{name='Thomas'}, Cat{name='Lionel Messi'}, Cat{name='Fluffy'}]
๊ฐ์ด๋ฐ ๊ณ ์์ด ํ ๋ง๋ฆฌ๋ฅผ ๋นผ๊ณ ๋๋จธ์ง๋ ๋น ๊ณต๊ฐ์ด ์๋๋ก ์ฎ๊ฒผ์ต๋๋ค. ๋์งธ , ์ธ๋ฑ์ค(์ผ๋ฐ ๋ฐฐ์ด๊ณผ ๊ฐ์)๋ฟ๋ง ์๋๋ผ ์ฐธ์กฐ๋ก ๋ ๊ฐ์ฒด๋ฅผ ์ญ์ ํ ์ ์์ต๋๋ค .
public static void main(String[] args) {
ArrayList<Cat> cats = new ArrayList<>();
Cat thomas = new Cat("Thomas");
Cat behemoth = new Cat("Behemoth");
Cat lionel = new Cat("Lionel Messi");
Cat fluffy = new Cat ("Fluffy");
cats.add(thomas);
cats.add(behemoth);
cats.add(lionel);
cats.add(fluffy);
System.out.println(cats.toString());
cats.remove(lionel);
System.out.println(cats.toString());
}
์ถ๋ ฅ: [Cat{name='Thomas'}, Cat{name='Behemoth'}, Cat{name='Lionel Messi'}, Cat{name='Fluffy'}] [Cat{name='Thomas'}, Cat{name='Behemoth'}, Cat{name='Fluffy'}] ์ํ๋ ๊ฐ์ฒด์ ์ธ๋ฑ์ค๋ฅผ ํญ์ ์ถ์ ํ๊ณ ์ถ์ง ์์ ๊ฒฝ์ฐ ๋งค์ฐ ํธ๋ฆฌํ ์ ์์ต๋๋ค. ์ผ๋ฐ์ ์ธ ์ญ์ ๋ฐฉ๋ฒ์ ์์๋ธ ๊ฒ ๊ฐ์ต๋๋ค. ์ด์ ์ด ์ํฉ์ ์์ํด ๋ด
์๋ค. ๋ชฉ๋ก์ ๋ฐ๋ณตํ๊ณ ํน์ ์ด๋ฆ์ ๊ฐ์ง ๊ณ ์์ด๋ฅผ ์ ๊ฑฐํ๋ ค๊ณ ํฉ๋๋ค . for
์ด๋ฅผ ์ํด Rishi์ ์์
์์ ์๊ฐํ ๋น ๋ฅธ ๋ฃจํ(for-each ๋ฃจํ๋ผ๊ณ ๋ ํจ)๋ฅผ ์ฌ์ฉํฉ๋๋ค .
public static void main(String[] args) {
ArrayList<Cat> cats = new ArrayList<>();
Cat thomas = new Cat("Thomas");
Cat behemoth = new Cat("Behemoth");
Cat lionel = new Cat("Lionel Messi");
Cat fluffy = new Cat ("Fluffy");
cats.add(thomas);
cats.add(behemoth);
cats.add(lionel);
cats.add(fluffy);
for (Cat cat: cats) {
if (cat.name.equals("Behemoth")) {
cats.remove(cat);
}
}
System.out.println(cats);
}
์ฝ๋๋ ์๋ฒฝํ๊ฒ ๋
ผ๋ฆฌ์ ์ผ๋ก ๋ณด์
๋๋ค. ๊ทธ๋ฌ๋ ๊ทธ ๊ฒฐ๊ณผ๋ ๋งค์ฐ ๋๋์ต๋๋ค. java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)์ java.util.ArrayList$Itr.next(ArrayList. java:831) at Cat.main(Cat.java:25) ์ผ์ข
์ ์ค๋ฅ๊ฐ ์์ผ๋ฉฐ ๋ฐ์ํ ์ด์ ๊ฐ ๋ช
ํํ์ง ์์ต๋๋ค. ์ด ํ๋ก์ธ์ค์๋ ํด๊ฒฐํด์ผ ํ๋ ์ฌ๋ฌ ๊ฐ์ง ๋์์ค๊ฐ ํฌํจ๋ฉ๋๋ค. ๊ธฐ์ตํด์ผ ํ ์ผ๋ฐ์ ์ธ ๊ท์น์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. ์ปฌ๋ ์
์ ๋ฐ๋ณตํ๋ฉด์ ๋์์ ํด๋น ์์๋ฅผ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๋ ๋จ์ํ ์ ๊ฑฐ๊ฐ ์๋๋ผ ๋ชจ๋ ์ข
๋ฅ์ ๋ณํ๋ฅผ ์๋ฏธํฉ๋๋ค. ๊ณ ์์ด ์ ๊ฑฐ๋ฅผ ์ ๊ณ ์์ด๋ฅผ ์ฝ์
ํ๋ ค๋ ์๋๋ก ๋ฐ๊พธ๋ฉด ๊ฒฐ๊ณผ๋ ๋์ผํฉ๋๋ค.
for (Cat cat: cats) {
cats.add(new Cat("Salem Saberhagen"));
}
System.out.println(cats);
"main" ์ค๋ ๋์ ์์ธ java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859) at java.util.ArrayList$Itr.next(ArrayList.java:831) at Cat.main( Cat.java:25) ํ๋์ ์์
์ ๋ค๋ฅธ ์์
์ผ๋ก ๋ณ๊ฒฝํ์ง๋ง ๊ฒฐ๊ณผ๋ ๋ณ๊ฒฝ๋์ง ์์์ต๋๋ค. ๋์ผํ ConcurrentModificationException ์ด ๋ฐ์ํฉ๋๋ค . ๋ชฉ๋ก์ ๋ฐ๋ณตํ๋ ๋์ ๋ชฉ๋ก์ ๋ณ๊ฒฝํ์ฌ ์์ ๊ท์น์ ์ด๊ธฐ๋ ค๊ณ ํ ๋ ์ ํํ๊ฒ ๋ฐ์ํฉ๋๋ค. Java์์๋์ปฌ๋ ์
์ ๋ฐ๋ณตํ๋ ๋์ ํญ๋ชฉ์ ์ญ์ ํ๊ธฐ ์ํด iterator (Iterator
ํด๋์คIterator
๋ ์์ ๋ชฉ๋ก์ ์์ ํ๊ฒ ๋ฐ๋ณตํ ์ฑ
์์ด ์์ต๋๋ค. 3๊ฐ์ง ๋ฐฉ๋ฒ๋ง ์๊ธฐ ๋๋ฌธ์ ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค.
hasNext()
- ๋ชฉ๋ก์ ๋ค์ ํญ๋ชฉ์ด ์๋์ง ๋๋ ์ด๋ฏธ ๋ง์ง๋ง ํญ๋ชฉ์ ๋๋ฌํ๋์ง ์ฌ๋ถ์ ๋ฐ๋ผ true ๋๋ false๋ฅผ ๋ฐํํฉ๋๋ค.next()
- ๋ชฉ๋ก์ ๋ค์ ํญ๋ชฉ์ ๋ฐํํฉ๋๋ค.remove()
- ๋ชฉ๋ก์์ ํญ๋ชฉ์ ์ ๊ฑฐํฉ๋๋ค.
Iterator<Cat> catIterator = cats.iterator();// Create an iterator
while(catIterator.hasNext()) {// As long as there are elements in the list
Cat nextCat = catIterator.next();// Get the next element
System.out.println(nextCat);// Display it
}
์ถ๋ ฅ: Cat{name='Thomas'} Cat{name='Behemoth'} Cat{name='Lionel Messi'} Cat{name='Fluffy'} ๋ณด์๋ค์ํผ, ๋ ArrayList
์ด๋ฏธ ๋ฐ๋ณต์: iterator()
. ๋ํ ๋ฐ๋ณต์๋ฅผ ๋ง๋ค ๋ ์์
ํ ๊ฐ์ฒด์ ํด๋์ค๋ฅผ ์ง์ ํฉ๋๋ค( <Cat>
). ํต์ฌ์ ๋ฐ๋ณต์๊ฐ ์๋ ์์
์ ์ฝ๊ฒ ์ฒ๋ฆฌํ๋ค๋ ๊ฒ์
๋๋ค. ์๋ฅผ ๋ค์ด "Lionel Messi"๋ผ๋ ์ด๋ฆ์ ๊ณ ์์ด๋ฅผ ์ ๊ฑฐํฉ๋๋ค.
Iterator<Cat> catIterator = cats.iterator();// Create an iterator
while(catIterator.hasNext()) {// As long as there are elements in the list
Cat nextCat = catIterator.next();// Get the next element
if (nextCat.name.equals("Lionel Messi")) {
catIterator.remove();// Delete the cat with the specified name
}
}
System.out.println(cats);
์ถ๋ ฅ: [Cat{name='Thomas'}, Cat{name='Behemoth'}, Cat{name='Fluffy'}]remove()
๋ฐ๋ณต์์ ๋ฉ์๋ ์์ ์ธ๋ฑ์ค๋ ์ด๋ฆ์ ์ง์ ํ์ง ์์ ๊ฒ์ ๋์น์ฑ์
จ์ ๊ฒ์
๋๋ค. ! ๋ฐ๋ณต์๋ ๋ณด์ด๋ ๊ฒ๋ณด๋ค ๋ ๋๋ํฉ๋๋ค. remove()
๋ฐ๋ณต์๊ฐ ๋ฐํํ ๋ง์ง๋ง ์์๋ฅผ ์ ๊ฑฐํฉ๋๋ค. ๋ณด์๋ค์ํผ, ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๋๋ก ์๋ํ์ต๋๋ค. :) ์์น์ ์ผ๋ก ์์ ์์๋ฅผ ์ ๊ฑฐํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์์ผ ํ ๋ชจ๋ ๊ฒ์
๋๋ค ArrayList
. ๊ธ์, ๊ฑฐ์ ๋ชจ๋ ๊ฒ. ๋ค์ ๊ฐ์์์๋ ์ด ํด๋์ค ๋ด๋ถ๋ฅผ ์ดํด๋ณด๊ณ ๋ค์ํ ๋ฉ์๋ ํธ์ถ ์ค์ ์ด๋ค ์ผ์ด ๋ฐ์ํ๋์ง ์ดํด๋ณด๊ฒ ์ต๋๋ค :) ๊ทธ๋๊น์ง!
GO TO FULL VERSION