๐ป ์์ค ์ฝ๋
package day_0615;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class ArrayList002 {
public static void main(String[] args) {
int index = 0;
ArrayList numbers = new ArrayList();
// ArrayList๋? ์๋ฐ์ List ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์ ํด๋์ค ์ค ํ๋
// ์ผ๋ฐ ๋ฐฐ์ด๊ณผ ๋์ผํ๊ฒ ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ฌ์ฉํ๊ณ index๋ 0๋ถํฐ ์์
// ๋ฐฐ์ด๊ณผ์ ์ฐจ์ด์ >> ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ ๊ณ ์ ์ ์ด์ง๋ง ArrayList๋ ํฌ๊ธฐ๊ฐ ๊ฐ๋ณ์ ์ด๋ค.
numbers.add("one");
// ์์ ์ถ๊ฐํ๊ธฐ ์ํด์ add ์ฐ์ฐ์ ์ฌ์ฉ
numbers.add("two");
// ์์ ์ถ๊ฐํ๊ธฐ ์ํด์ add ์ฐ์ฐ์ ์ฌ์ฉ
numbers.add("three");
// ์์ ์ถ๊ฐํ๊ธฐ ์ํด์ add ์ฐ์ฐ์ ์ฌ์ฉ
String num = JOptionPane.showInputDialog("์ซ์๋ฅผ ์
๋ ฅํ์ธ์");
// โ
JOptionPane.showInputDialog ์ฌ์ฉ๋ฒ โ
// Show a dialog asking the user to type in a String:
// String inputValue = JOptionPane.showInputDialog("Please input a value");
for (int i = 0; i < numbers.size(); i++) {
// for๊ตฌ๋ฌธ ๋ฐ๋ณต๋ฌธ i๊ฐ 0๋ถํฐ ArrayList numbers์ ํฌ๊ธฐ๋ณด๋ค ์์ ๋๊น์ง ๋ฐ๋ณต.
if (Integer.parseInt(num) - 1 == i) {
// ์
๋ ฅ๋ฐ์ ๊ฐ : 1 / 1 - 1 == 0 => index = 0
// ์
๋ ฅ๋ฐ์ ๊ฐ : 2 / 2 - 1 == 1 => index = 1
// ์
๋ ฅ๋ฐ์ ๊ฐ : 3 / 3 - 1 == 2 => index = 2
index = i;
}
}
System.out.println(numbers.get(index));
// numbers ArrayList์์ index์ ์นธ์ ๊ฐ์ ๊บผ๋.
}// end main
}// end class