Collection 1-2) ArrayList?
https://kwanghyung.tistory.com/entry/Collection-1-1-Collection-%ED%81%B4%EB%9E%98%EC%8A%A4
Collection 1-1) Collection 클래스?
컬렉션(Collection) -자료구조가 내장되어 있는 클래스 -자바에서 제공하는 "자료구조"를 담당하는 "프레임워크"입니다. -자료구조 : 데이터들을 효율적으로 다룰때 필요한 개념 -프레임워크 : 효율
kwanghyung.tistory.com
ArrayList의 선언 방법
[표현법]
ArrayList 이름 = new ArrayList(); ->크기 지정 안함
ArrayList 이름 = new ArrayList(크기); ->크기 지정
ArrayList list = new ArrayList(3);
System.out.println(list);
크기가 3인 ArrayList 선언
add 메소드
ArrayList list = new ArrayList(3);
System.out.println(list);
list.add(50);
list.add("바보");
list.add(15.5);
list.add(true);
list.add('A');
System.out.println(list);
Remove 메소드
미리 선언한 ArrayList에서 Remove로 요소값 삭제하기
list.remove("바보"); //요소값 삭제
System.out.println(list);
add 메소드 객체로 넣기
Music 클래스 선언
public class Music {
private String title;//제목
private String artist;//가수명
public Music() {
super();
}
public Music(String title, String artist) {
super();
this.title = title;
this.artist = artist;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
@Override
public String toString() {
return "Music [title=" + title + ", artist=" + artist + "]";
}
}
선언된 Class 클래스에 객체를 생성해서 list에 넣어보기
Music m = new Music("사랑비","김태우");
list.add(m);
list.add(new Music("호랑나비","김흥국"));
System.out.println(list);
객체를 생성하고 그 값을 넣어주면 객체와 속성값이 다음과 같이 들어갑니다.
인덱스로 객체추가하기
list.add(2,new Music("가을아침","아이유"));
System.out.println(list);
인덱스 2번째 즉 0부터 시작해서 2 인덱스에 가을아침,아이유 속성을 가진 객체가 추가
값 수정해보기
list.set(0, new Music("Lonely","Imagine Dragons"));
System.out.println(list);
0번째 인덱스 즉 제일 처음에 값이 50에서 Lonely,Imagine Dragons로 바뀐것을 확인 할 수 있습니다.
값 삭제해보기(인덱스)
list.remove(1);
System.out.println(list);
출력 값 : [Music [title=Lonely, artist=Imagine Dragons], Music [title=가을아침, artist=아이유], true, A, Music [title=사랑비, artist=김태우], Music [title=호랑나비, artist=김흥국]]
으로 출력이 됩니다.
그냥 추가하면 제일 마지막 인덱스에 추가
Music m1 =new Music("Lonely","Imagine Dragons");
list.add(m1);
System.out.println(list);
출력 값 : [Music [title=Lonely, artist=Imagine Dragons], Music [title=가을아침, artist=아이유], true, A, Music [title=사랑비, artist=김태우], Music [title=호랑나비, artist=김흥국]]
System.out.println(list.size());
출력 값 : 6
System.out.println(list.get(0));
Music [title=Lonely, artist=Imagine Dragons]
System.out.println(((Music)list.get(0)).getTitle()); //0번인덱스에 있는 객체의 제목 뽑기
System.out.println(((Music)list.get(0)).getArtist()); //0번인덱스에 있는 객체의 가수명 뽑기
출력 값 :Lonely
출력 값 :Imagine Dragons
-비어있는지 확인하는 메소드 isEmpty() 비어있으면 true 아니면 false
-리스트를 통째로 비워주는 메소드 clear() 데이터를 다 날려버릴수 있기때문에 신중하게 사용하기
list.addAll(0, sub);
리스트+리스트 addAll()
'자바-자린이의 도전기' 카테고리의 다른 글
Collection 1-4)Collection을 이용한 MVC 패턴 예제 (0) | 2022.05.16 |
---|---|
Collection 1-3) ArrayList 추가메소드/제네릭 (0) | 2022.05.14 |
Collection 1-1) Collection 클래스? (0) | 2022.05.10 |
예외처리 Exception 1-2) Checked Exception (0) | 2022.05.09 |
예외처리 Exception 1-1) RuntimeException (0) | 2022.05.08 |
댓글
이 글 공유하기
다른 글
-
Collection 1-4)Collection을 이용한 MVC 패턴 예제
Collection 1-4)Collection을 이용한 MVC 패턴 예제
2022.05.16 -
Collection 1-3) ArrayList 추가메소드/제네릭
Collection 1-3) ArrayList 추가메소드/제네릭
2022.05.14 -
Collection 1-1) Collection 클래스?
Collection 1-1) Collection 클래스?
2022.05.10 -
예외처리 Exception 1-2) Checked Exception
예외처리 Exception 1-2) Checked Exception
2022.05.09