Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 덴드로그램
- DataAccess
- del
- Python
- 최솟값
- hierarchical_clustering
- len()
- string
- matplotlib
- append()
- dendrogram
- 최댓값
- DataFrame
- IN
- function
- 반복문
- elbow method
- wcss
- Machine Learning
- list
- insert()
- 분류 결과표
- analizer
- pandas
- nan
- count()
- Dictionary
- data
- sklearn
- numpy
Archives
- Today
- Total
개발공부
[Java] ArrayList 사용법 본문
ArrayList
개수를 정하지 않고 데이터를 마음대로 추가할 수 있고, 삭제할 수 있는 것
생성하기
ArrayList<데이터타입> 변수명 = new ArrayList<데이터타입>();
ArrayList<String> nameList = new ArrayList<String>();
추가하기
변수명.add(추가할 데이터)
nameList.add("홍길동");
예제
import java.util.ArrayList;
public class ArrayListTest {
public static void main(String[] args) {
// 배열은, 생성시에, 갯수를 정해줘야한다.
// 배열은 한번 개수를 정하면,
// 그 개수 이상으로는 데이터를 추가 불가.
String[] nameArray = new String[5];
// 개수 정하지 않고 데이터를 마음대로
// 추가할 수 도 있고, 삭제할 수 도 있는 것이
// 어레이 리스트다.
// 담고 싶은 데이터 타입을 <> 안에 적어줘야 한다.
// 비어있는 리스트를 만든다.
ArrayList<String> nameList = new ArrayList<String>();
nameList.add("홍길동");
nameList.add("김나나");
nameList.add("Mike");
System.out.println(nameList.get(1));
System.out.println("----------------------");
// 리스트에 있는 데이터를 모두 출력
for(int i=0; i<nameList.size();i++) {
System.out.println(nameList.get(i));
}
// 홍길동, 김나나, Mike
// Harry를 홍길동과 김나나 사이에 추가.
// add(인덱스, 데이터)
nameList.add(1, "Harry");
System.out.println("----------------------");
for(int i=0;i<nameList.size();i++) {
System.out.println(nameList.get(i));
}
//데이터 삭제
// remove()
System.out.println("----------------------");
nameList.remove(0);
for(int i=0;i<nameList.size();i++) {
System.out.println(nameList.get(i));
}
// 데이터 전체 삭제
// clear()
System.out.println("----------------------");
nameList.clear();
System.out.println(nameList.size());
// 리스트가 비어있는지 확인
// isEmpty()
System.out.println("----------------------");
if(nameList.isEmpty()) {
System.out.println("리스트가 비어있습니다.");
}else {
System.out.println("리스트가 비어있지 않습니다.");
}
}
}

'Java > Basic' 카테고리의 다른 글
| [Java] try catch finally 문법 (0) | 2022.07.07 |
|---|---|
| [Java] HashMap 사용법 (0) | 2022.07.06 |
| [Java] Interface 인터페이스 (0) | 2022.07.06 |
| [Java] Abstract Class 추상 클래스 (0) | 2022.07.06 |
| [Java] UpCasting / DownCasting (0) | 2022.07.05 |