Array List 구조

  • 물리적으로 연속적인 구조
  • 순차검색으로는 가장 빠르다.
  • 검색시 운나쁘면 맨뒤까지 검색해야한다 but 인덱스를 알면 바로 찾을수도 있음
  • 삽입시 뒤의 데이터를 1칸씩 뒤로 밀어야함
  • 삭제시 뒤의 데이터를 전부 1칸씩 당겨야함
  • 확장시 공간이 부족하면 더 큰 새 공간을 할당하고 전부 복사해야함
    ->유연성이 떨어짐


Java ArrayList class uses a dynamic array for storing the elements. It inherits AbstractList class and implements List interface.

The important points about Java ArrayList class are:

  • Java ArrayList class can contain duplicate elements.
  • Java ArrayList class maintains insertion order.
  • Java ArrayList class is non synchronized.
  • Java ArrayList allows random access because array works at the index basis.
  • In Java ArrayList class, manipulation is slow because a lot of shifting needs to occur if any element is removed from the array list.

-ex1

import java.util.*;  
class Book {  
int id;  
String name,author,publisher;  
int quantity;  
public Book(int id, String name, String author, String publisher, int quantity) {  
    this.id = id;  
    this.name = name;  
    this.author = author;  
    this.publisher = publisher;  
    this.quantity = quantity;  
}  
}  
public class ArrayListExample {  
public static void main(String[] args) {  
    //Creating list of Books  
    List<Book> list=new ArrayList<Book>();  
    //Creating Books  
    Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);  
    Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);  
    Book b3=new Book(103,"Operating System","Galvin","Wiley",6);  
    //Adding Books to list  
    list.add(b1);  
    list.add(b2);  
    list.add(b3);  
    //Traversing list  
    for(Book b:list){  
        System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);  
    }  
}  
}  
101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6

 

 

-ex2

class Human {
	String name;
	int age;

	Human(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String toString() {
		return name + ", " + age;
	}
}

public class UseArrayList {
	public static void main(String[] args) {
		
		ArrayList<Human> hList = new ArrayList<Human>();
		hList.add(new Human("장그레이스", 24));
		hList.add(new Human("브리트니 점례", 21));
		hList.add(new Human("저스틴 옥", 21));

		for (Human hu : hList)
			System.out.println(hu);

	}
}

 

'java' 카테고리의 다른 글

Java/ 추상화와 추상클래스  (0) 2020.02.28
Java/ ArrayList vs LinkedList  (0) 2020.02.28
Java/ 자료구조 - Linked List  (0) 2020.02.28
Java/ Math & Random  (0) 2020.02.28
Java/ Interface의 개념  (0) 2020.02.10