int[] arry = {1000,2000,3000}; // 일반 배열의 형태
Food[] obArray = {new Food01("짜장면",6000), new Food01("짬뽕",6500),new Food01("탕수육",7000)};
for(int i=0; i<obArray.length; i++){
System.out.println(i+":"+obArray[idx]); // 객체의 주소가 출력
Food f01 = obArray[i];
System.out.println(f01.getName()+":"+f01.getPrice());
}
class Food{
private String name;
private int price;
public Food(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
ArrayList arr = new ArrayList();
list.add("사과"); // .add(추가할 데이터);
list.add("바나나");
list.add("키위");
System.out.println(list.size()); // .size(); 동적배열의 크기
for(int idx=0;idx<list.size();idx++) {
System.out.println(idx+":"+list.get(idx));
}
list.remove(1); // 삭제처리
cf) 향상된 반복문
for(단위데이터 : 배열형객체)
for(Object fruit : arr){ //ArrList의 기본 유형 Object
System.out.println(fruit);
}
ArrayList<Student> arr = new ArrayList<Student>(); //선언
arr.add(new Student("홍길동",80,50); // 데이터 추가
arr.add(new Student("금붕어",60,80);
for(int i=0; i<arr.size();i++){
Student s1 = arr.get(i);
System.out.println("이름: "+s1.getName);
System.out.println("국어점수: "+s1.getKor);
System.out.println("영어점수: "+s1.getEng);
}
class Student{
private String name;
private int kor;
private int eng;
public Student(String name, int kor, int eng) {
this.name = name;
this.kor = kor;
this.eng = eng;
}
public String getName() {
return name;
}
public int getKor() {
return kor;
}
public int getEng() {
return eng;
}
}