- ArrayList 동적 배열
- ArrayList <참조타입> 참조변수 = new ArrayList<>();
참조변수.add(데이터)
참조변수.remove(인덱스번호)
참조변수.get(인덱스번호)
참조변수.size(인덱스번호)
import java.util.ArrayList;
public class ListDemo2 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>() ;
for (int i = 10; i < 15; i++) {
list.add(i);
}
System.out.println(list);
System.out.println(list.get(0));
list.set(0, 100);
System.out.println(list.get(0));
for (int i = 0; i < list.size() ; i++) {
System.out.print(list.get(i) + " ");
}
}
}
- ArrayList 응용 ( while문 진화! )
import java.util.ArrayList;
import java.util.Scanner;
public class ListDemo4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0 ;
double avg = 0.0 ;
int data = 0;
System.out.print("input data: ");
ArrayList<Integer> ary2 = new ArrayList<Integer>();
while ( (data = sc.nextInt() )>0) {
ary2.add(data);
sum += data ;
System.out.print("input data: ");
}
avg = (double) sum / ary2.size();
System.out.println(" - ".repeat(20));
System.out.println(avg);
}
}