기존배열
private VoterVO [] arr;
public VoterDAO(){
arr= new VoterVO [4];
arr[0] = new VoterVO(1, "홍길동");
arr[1] = new VoterVO(2, "고길동");
arr[2] = new VoterVO(3, "박길동");
arr[3] = new VoterVO(4, "이길동");
}
기존 배열 방식 각 배열에 내용을 지정해주어야 하고 처음 설정했던 크기만큼만 사용할 수 있음
ArrayList
길이의 제한이없다 int의 크기만큼 만들어진다
ArrayList 선언 , 초기화
ArrayList 선언 타입 이름
private ArrayList <VoteVO> voArrayList;
//ArrayList 선언 타입 이름
private ArrayList<VoteVO> voArrayList;
// ArrayList 초기화
public VoteDAO() {
voArrayList = new ArrayList<>();
}
add 라는 메서드를 사용하면 알아서 index 맞춰서 넣어준다
public void add(VoteVO voteVO) {
voArrayList.add(voteVO);
}
ArrayList 선언과 출력
타입 변수명 초기화
List<Integer> num = new ArrayList<>();
add 활용
num.add(1);
.
.
변수명 . add 해주고 ()안에는 넣어줄 값을 주면 그 값을 알아서 index 값을 준다
public class Main {
public static void main(String[] args) {
List<Integer> num = new ArrayList<>();
num.add(1);
num.add(2);
num.add(5);
num.add(4);
for(Integer a:num){
System.out.println(a);
}
}
}
⭐결과
1
2
5
4
'JAVA' 카테고리의 다른 글
상속 심화 (0) | 2022.03.14 |
---|---|
상속이란? (0) | 2022.03.14 |
메모장 파일 불러오기 File = new File (0) | 2022.03.09 |
배열과 사랑에 빠진 반복문 (0) | 2022.03.06 |
배열의 또 다른 모습 (0) | 2022.03.06 |