[Code Review][Java][220615] ArrayList

ss5Jngยท2022๋…„ 6์›” 15์ผ
0

Today I Learned

๋ชฉ๋ก ๋ณด๊ธฐ
4/13

๐Ÿ’ป ์†Œ์Šค ์ฝ”๋“œ

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
profile
๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค:)

0๊ฐœ์˜ ๋Œ“๊ธ€