1. 배열 리스트
- 동일한 데이터 타입을 순서에 따라 관리하는 자료 구조
- 정해진 크기가 있음(fixed-length)
- 물리적으로 연속된 메모리를 사용하므로 자료의 추가와 제거시 다른 요소들의 이동이 필요함 O(n)
- 물리적으로 연속된 메모리를 사용하므로 배열의 i 번째 자료를 찾는 연산이 빠름 : 인덱스 연산 O(1)
2. 배열 리스트에 자료 추가하고 삭제하기
- 중간에 추가 삭제 시 배열의 이동이 필요함
public interface ListInterface<E> {
public void insertElement(int i, E data);
public void addElement(E data);
public E removeElement(int i);
public E getElement(int i);
public int getSize();
public boolean isEmpty();
public void removeAll();
public void printAll();
}
public class MyArrayList<E> implements ListInterface<E> {
private int count;
private E[] objectList;
private int DEFAULT_CAPACITY = 10;
public static final int ERROR_NUM = -999999999;
public MyArrayList() {
objectList = (E[])new Object[DEFAULT_CAPACITY];
}
public MyArrayList(int size) {
DEFAULT_CAPACITY = size;
objectList = (E[])new Object[size];
}
@Override
public void insertElement(int position, E data) {
if(count >= DEFAULT_CAPACITY) {
System.out.println("not enough memory");
return;
}
if(position < 0 || position > count){
System.out.println("insert position error");
return;
}
if (position == count) {
objectList[count++] = data;
return;
}
for (int i = count - 1; i >= position; i--) {
objectList[i + 1] = objectList[i];
}
objectList[position] = data;
count++;
}
@Override
public void addElement(E data) {
if(count >= DEFAULT_CAPACITY) {
System.out.println("not enough memory");
return;
}
objectList[count++] = data;
}
@Override
public E removeElement(int position) {
if (isEmpty()) {
System.out.println("no element");
return null;
}
if(position < 0 || position > count){
System.out.println("remove position error");
return null;
}
E ret = objectList[position];
for (int i = position; i < count - 1; i++) {
objectList[i] = objectList[i + 1];
}
count--;
return ret;
}
@Override
public E getElement(int i) {
return objectList[i];
}
@Override
public int getSize() {
return count;
}
@Override
public boolean isEmpty() {
if(count == 0)
return true;
else
return false;
}
@Override
public void removeAll() {
objectList = (E[])new Object[DEFAULT_CAPACITY];
count = 0;
}
@Override
public void printAll() {
if (count == 0) {
System.out.println("Array is empty");
return;
}
for (int i = 0; i < count; i++) {
System.out.println(objectList[i]);
}
}
}
public class MyArrayListTest {
public static void main(String[] args) {
MyArrayList array = new MyArrayList();
array.addElement(10);
array.addElement(20);
array.addElement(30);
array.insertElement(1, 50);
array.printAll();
System.out.println("================");
array.removeElement(1);
array.printAll();
System.out.println("================");
MyArrayList array2 = new MyArrayList();
array2.addElement(10);
array2.printAll();
System.out.println("================");
array2.removeElement(1);
array2.printAll();
System.out.println(array.getElement(2));
}
}
'알고리즘' 카테고리의 다른 글
6. 큐(Queue) - 패스트캠퍼스 백엔드 부트캠프 3기 (0) | 2025.01.23 |
---|---|
5. 스택 구현 - 패스트캠퍼스 백엔드 부트캠프 3기 (0) | 2025.01.23 |
4. 연결 리스트 - 패스트캠퍼스 백엔드 부트캠프 3기 (2) | 2025.01.22 |
2. 기본적인 자료구조 리스트 - 패스트캠퍼스 백엔드 부트캠프 3기 (0) | 2025.01.21 |
1. 자료구조와 알고리즘 - 패스트캠퍼스 백엔드 부트캠프 3기 (0) | 2025.01.21 |