2. src 우클릭 new -> class
Item class
Main class
2개를 생성
3. 각 클래스 파일에 다음을 copy
Item.class
public class Item { int number; String name; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Main.class
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<Item> listOfItems = new ArrayList<Item>(); Item item1 = new Item(); item1.setNumber(0);; item1.setName("item1"); listOfItems.add(item1); Item item2 = new Item(); item2.setNumber(1);; item2.setName("item2"); listOfItems.add(item2); for (Item item : listOfItems) { System.out.println(item.getNumber() + " " + item.getName()); } } }
댓글 없음:
댓글 쓰기