21/06/13 JAVA <ArrayList>

yeoni·2021년 10월 26일

JAVA

목록 보기
2/18

이 코드는 지난 시간 객체 배열로 생성했던 코드.

Circle[] c = new Circle[3]; // 요소가 3개로 정해진 공간 구성

for (int i = 0; i < c.length; i++) {
	System.out.print("x, y, radius >> ");
	c[i] = new Circle(sc.nextDouble(), sc.nextDouble(), sc.nextInt());
	// 객체를 생성하는 코드 여기서는 입력받으려고 next~ 를 넣어줬음
	 c[1] = new Circle(1.1,2.2,3); 이렇게도 입력할 수 있다.
}
// 1. ArrayList<Circle> list = new ArrayList<>():
// 위 형식으로도 가능! 갯수가 정해지지 않은 배열을 만들 때 사용함.
// 2. Circle cc = new Circle(); Circle형 객체cc를 생성
// 3. list.add(cc); Circle형 객체를 생성한 cc를 ArrayList에 저장
// 4. list.add(cc); Circle자료형 객체에 add메소드를 통해 값을 넣어주는 것
// 5. list.add(new Cricle()); Circle형 객체를 생성하고 ArrayList에 저장

// 1은 2,3을 한 줄로 줄인 것과 같음.
ArrayList<String> strList = new ArrayList<>();
strList.add("모니터");
strList.add("키보드");
strList.add("마우스");

for(String k: strList)
	System.out.println(k);
ArrayList<Integer> intList = new ArrayList<>();
//int는 기본자료형, Integer는 정수형 객체
for(int k: intList)
	System.out.println(k);
profile
24시간이 부족한 개발자

0개의 댓글