꾸준히 안타치기
*****array /List - ArrayList 본문
LIst란? - 목록, 명단
Arraylist란? 객체를 담기 위한 클래스이다.
Array(정적) | ArrayList(동적) |
사이즈 고정, 배열의 크기를 넣어준다. 크기를 정해두고 할때 사용 |
값을 계속 추가할수 있다. 같은 타입의 변수에 어떠한 값을 얼만큼 저장할지 알 수 없을때 사용 |
index 가능 | index가 없다. |
빠른검색가능 | 전체를 다뒤져야하기 때문에 탐색속도가 느리다. |
삭제시 크기가 고정되어있기에 메모리가 낭비되는 단점이 있다. | 종류 - ArrayList와 LinkedList |
https://stickode.com/detail.html?no=1657
https://stickode.com/detail.html?no=1657
stickode.com
ArrayLIst 는 데이터 처리의 유동성이 좋고, 사용하기 편한 자료형 클래스 이다.
객체를 담는 보관함이다! 개체를 저장하고 삭제할 수 있다.
ArrayList - add
SimpleArrayList list = new SimpleArraList();
list.add(“1”);
list.add(“2”);
list.add(“3”);
System.out.println(list);
for(int i =0, i<list.size(); i++){
list.get(i);
}
ArrayList - insert
SimpleArrayList list = new SimpleArraList();
list.add(“1”);
list.add(“2”);
list.add(“3”);
list.insert(2,9); // <-- 배열의3번째 자리에 9를 넣어라.
// 3번째에 9가 들어가고, 숫자가 밀린다./0부터 시작
System.out.println(list);
list = 1,2,9,3
ArrayList - remove
SimpleArrayList list = new SimpleArraList();
list.remove(4); // 3번째 자리에 삭제하고, 뒤에 숫자 자동으로 채워짐
ArrayList는 객체를 담기 위한 클래스
ArrayList에 객체를 담기 위해서는 객체의 타입을 명시해야한다.
// Employee 객체를 담기위한 ArrayList생성
ArrayList<Employee> list = new ArrayList<Employee>();
// Employee 객체 생성
Employee emp1 = new Employee();// 객체1
Employee emp2 = new Employee();// 객체2
Employee emp3 = new Employee();// 객체3
// 객체를 ArrayList에 담기
list.add(emp1);
list.add(emp2);
list.add(emp3);
// 결과값
list ->{emp1,emp2,emp3}
************데이터에 특정위치값 가져오기 *********
String valueA = listA.get(i).toString();
// 0위치값 가져오기
Arraylist al = new Arraylist();al.add("one"); // 괄호안의 데이터 타입은 object 형이어야 한다.al.add("two");al.add("three");for(int i =0; i< al.size(); i++){String value = (String)al.get(i); // 오브젝트를 스트링에 담고, string데이터 타입으로 형변환한다.System.out.println(al.get(value));} |
제네릭
Arraylist <String> al = new Arraylist<String> ();al.add("one"); //al.add("two");al.add("three");for(int i =0; i< al.size(); i++){String value = al.get(i); // 제네릭으로 이미 타입을 정해주었으므로, 형변환하지 않아도 된다.System.out.println(al.get(value));} |
*위의 데이터를 Iterator를 이용하여 데이터 조회방법
Interator integrator = listA.iteator();
while(iterator.hasNext()){
String value = (String)iterator.next();
}
**** haseNext(); : 순차적으로 다음으로 이동할 항목이 있는지 체크함
**** Next(); : 순차적으로 다음으로 이동하는것(실제다음 위치로 이동)
*위의 데이터를 for-loop를 이용하여 데이터 조회방법for(Object object : listA){String value = (String)object;} |
*list 데이터값 삭제하기
listA.remove(0); // 특정위치 삭제
listA.remove(“김삿갓”); // 특정값 삭제(위치도 삭제)
*값위치에 위치 알아내기
int index = listA.indexOf(“김삿갓”);
https://onlyfor-me-blog.tistory.com/249
[JAVA] ArrayList 사용법 정리
안드로이드 앱을 만들다 보면 자주 사용하게 되는 것 중 하나가 ArrayList라는 것이다. 이번 포스팅에선 ArrayList라는 것의 간단한 사용법을 적어보려고 한다. 먼저 ArrayList란 무엇일까? 이 단어 자체
onlyfor-me-blog.tistory.com
LinkedList? ArrayList와 데이터를 저장하는 방법이 조금 다름.
https://www.youtube.com/playlist?list=PLENYGEQnz1xoQTOexMGVb9CIVjEWtV5wz
LinkedList 앞뒤 주소 기반으로 가져올수있다. 중간에 삽입할때 유리
로또 번호 생성하기 예제
https://www.youtube.com/watch?v=nARtGOHdIXs&list=PLyebPLlVYXCiKweTN4a-xePbbY1Ta6Yu9&index=13
'CS > JAVA' 카테고리의 다른 글
Hashmap (0) | 2021.10.25 |
---|---|
접근 제어자 private / getter, setter (0) | 2021.09.23 |
if / while / do while (0) | 2021.02.19 |
숫자 출력 / 형식지정자 / 소수점 몇째자리 까지 표현 / formating (0) | 2021.02.19 |
is a 상속 / 추상클래스 / 공통클래스 (0) | 2021.02.19 |