2024.05.02 자료구조(Data Structure) Java 배열을 활용한 객체 만들기
정훈52024. 5. 2. 09:50
학습 목표
1. 배열에 대한 기본 개념 복습
2. 배열을 활용한 객체를 만들어 보자.
배열에 대한 기본 개념 복습
동일한 데이터 타입을 순서에 따라 관리하는 자료 구조
정해진 크기가 있음(배열)
요소의 추가와 제거시 다른 요소들의 이동이 필요함
배열의 i 번째 요소를 찾는 인덱스 연산이 빠름
jdk 클래스 : ArrayList, Vector
package structure;
/*
* 배열을 활용한 클래스를 설계
* 물론 --> 이미 자바 표준 API 개발자들이
* 잘 만들어 준 클래스 들이 존재한다.
* 하지만 직접 기능을 확장해서 만들어 보자.
*/
public class TencointArray {
int[] intArr; // 배열
int count; // 배열안에 들어간 요소의 갯수
public final int ARRAY_SIZE; // 상수
public static final int ERROR_NUM = -9999999;
int size = 10;
public TencointArray() {
count = 0;
ARRAY_SIZE = size;
intArr = new int[ARRAY_SIZE];
}
// 기능 설계
// 1. 배열에 요소를 추가하는 기능
// 배열 요소에 제일 뒤에 값을 추가하는 기능을 가진다.
public void addElement(int inputData) {
// 방어적 코드 필요
if (count >= ARRAY_SIZE) {
System.out.println("메모리 공간이 가득 찼습니다.");
return; // 실행의 제어권을 반납
}
intArr[count] = inputData;
count++;
}
// 2. 배열에 지정된 인덱스 위치에 값을 추가하는 기능
public void insertElement(int position, int inputData) {
// 방어적 코드 작성 1
if (count >= ARRAY_SIZE) {
System.out.println("메모리 공간이 가득 찼습니다.");
return; // 실행의 제어권을 반납
}
// 방어적 코드 작성 2
// 10 < 0
if (position < 0 || ARRAY_SIZE < position) {
System.out.println("지정한 인덱스 번호가 잘못 지정되었습니다.");
return;
}
// 요청값 : position -> 3
// [11, 12, 13, [], 14, 15]
for (int i = count - 1; i >= position; i--) {
intArr[i + 1] = intArr[i]; // 하나씩
// intArr[5] = 15 수행 1
// intArr[4] = 14 수행 2
}
intArr[position] = inputData;
count++;
}
// 3. 지정한 인덱스 번호에 맞는 요소를 출력하는 기능
// 지정한 인덱스 번호에 요소를 꺼내 주기
public int getElement(int position) {
// 배열의 크기는 10개
// [0] [1] [2] 3 > 3 - 1
if (position < 0 || position > count - 1) {
System.out.println("검색 위치 오류. 현재 리스트의 갯수는 " + count + " 개 입니다.");
return ERROR_NUM;
}
return intArr[position];
}
// 요소를 전체 출력하는 기능 만들어 주기
public void printAll() {
if (count == 0) {
System.out.println("출력 할 내용이 없습니다.");
return;
}
for (int i = 0; i < intArr.length; i++) {
System.out.println(intArr[i]);
}
// for문은 카운터를 조절하는 기능이 없기에 시작하면 끝가지 돈다.
// for (int i : intArr) {
// System.out.println(intArr[i]);
// }
}
// 전체 삭제하는 기능
public void removeAll() {
for (int i = 0; i < intArr.length; i++) {
intArr[i] = 0;
}
// 요소에 갯수 상태를 항상 관리하고 처리해야한다.
count = 0;
}
// 배열에 크기가 아닌 현재 요소의 개수를 반환
public int getCountSize() {
return count;
}
// 현재 요소가 하나도 없는 상태이다.
// boolean 일때 get 대신 is 를 사용한다.
public boolean isEmpty() {
if (count == 0) {
return true;
} else {
return false;
}
}
// 4. 지정한 인덱스 번호에 요소를 삭제하는 기능
// 지정한 인덱스 번호에 요소를 삭제하기
public void removeElement(int position) {
// 방어적 코드
if (isEmpty()) {
System.out.println("삭제 할 요소가 없습니다.");
}
// 인덱스 범위를 잘못 지정햇다면 방어적 코드
if (position < 0 || position >= count) {
System.out.println("잘못된 요청 입니다.");
}
// intArr[position]; --> 사용자가 요청한 인덱스 번호는 0번 이라고 가정한다.
// [100] [200] [300] [400]
// [200] [300] [400]
for (int i = position; i < count - 1; i++) {
intArr[i] = intArr[i + 1];
}
count--;
}
}
package structure;
public class MainTest1 {
public static void main(String[] args) {
TencointArray tencointArray = new TencointArray();
tencointArray.addElement(100);
tencointArray.addElement(200);
tencointArray.addElement(300);
tencointArray.addElement(400);
// tencointArray.insertElement(5, 50); // 테스트 이후에 수정 - todo
tencointArray.printAll();
// System.out.println(tencointArray.getElement(4));
System.out.println("-----------------------");
System.out.println(tencointArray.getCountSize());
System.out.println("-----------------------");
System.out.println(tencointArray.isEmpty());
System.out.println("-----------------------");
tencointArray.removeAll();
tencointArray.printAll();
}
}