1. 구현 기능
2. 구현 코드
public class MyArray {
int[] intArr;
public final int arraySize = 10;
int count;
public MyArray() {
intArr = new int[arraySize];
count = 0;
}
public MyArray(int size) {
intArr = new int[size];
count = 0;
}
public void addElement(int data) {
if(isFull()) {
System.out.println("배열 공간 부족!");
return;
}
else {
intArr[count] = data;
count++;
}
}
public void insertElement(int pos, int data) {
if(pos < 0 || pos > count) {
System.out.println("index 에러!");
return;
}
else if(isFull()){
System.out.println("배열 공간 부족!");
return;
}
else {
for (int i = count-1; i >= pos; i--) {
intArr[i+1] = intArr[i];
}
intArr[pos] = data;
count++;
}
}
public void removeElement(int pos) {
if(pos < 0 || pos > count-1) {
System.out.println("index 에러!");
return;
}
else {
for (int i = pos; i < count-1; i++) {
intArr[i] = intArr[i+1];
}
count--;
}
}
public int getElement(int pos) {
return intArr[pos];
}
public int getSize() {
return count;
}
public boolean isFull() {
if(count>=arraySize) {
return true;
}
else {
return false;
}
}
public boolean isEmpty() {
if(count == 0) {
return true;
}
else {
return false;
}
}
public void printAll() {
if (count == 0 ) {
System.out.println("출력할 요소 없음!");
}
else {
for(int i = 0; i < count; i++) {
System.out.println(intArr[i]);
}
}
}
}