package com.example.MyJavaProject;
class Main {
public static void main(String[] args) {
ArrayList al = new ArrayList();
System.out.println("al.size() : " + al.size());
// 출력 => al.size() : 0
al.add(100);
System.out.println("al.get(0) : " + al.get(0));
// 출력 => al.get(0) : 100
al.add(200);
al.add(300);
System.out.println("al.size() : " + al.size());
// 출력 => al.size() : 3
System.out.println("al.get(1) : " + al.get(1));
// 출력 => al.get(1) : 200
al.removeAt(1);
System.out.println("al.size() : " + al.size());
// 출력 => al.size() : 2
System.out.println("al.get(1) : " + al.get(1));
// 출력 => al.get(1) : 300
al.add(400);
al.add(500);
al.add(600);
System.out.println("al.get(3) + al.get(4) : " + (al.get(3) + al.get(4)));
// 출력 => al.get(3) + al.get(4) : 1100
al.showAllValues();
// 출력 =>
/*
== 전체 데이터 출력 ==
0 : 100
1 : 300
2 : 400
3 : 500
4 : 600
*/
al.add(700, 1);
al.add(750, 1);
al.showAllValues();
}
}
class ArrayList{
int[] arr = new int[2];
public int superIndex = 0;
int size(){
return superIndex;
}
void add(int value) {
if (superIndex > 1) {
int[] arrTmp = new int[superIndex + 1];
for (int i = 0; i < superIndex; i++) {
arrTmp[i] = arr[i];
}
arrTmp[superIndex] = value;
arr = arrTmp;
} else {
arr[superIndex] = value;
}
superIndex++;
}
void add(int value, int index) {
int[] arrTmp = new int[arr.length+1];
for(int t=0; t<arrTmp.length; t++){
if(t<index) {
arrTmp[t] = arr[t];
}else if(t==index){
arrTmp[t] = value;
}else if(t>index){
arrTmp[t] = arr[t-1];
}
}
arr = arrTmp;
superIndex = arr.length;
}
int get(int index){
return arr[index];
}
void removeAt(int index){
int[] arrTmp = new int[arr.length-1];
int k = 0;
while(true){
if(k<index) {
arrTmp[k] = arr[k];
k++;
}else if(k>=index){
arrTmp[k] = arr[k+1];
k++;
} else {
return;
}
if(k==arrTmp.length){
break;
}
}
arr = arrTmp;
superIndex = arr.length;
}
void showAllValues(){
System.out.println("===전체 데이터 출력===");
int j = 0;
while(true){
if(j==arr.length){break;}
System.out.printf("%d : %d \n",j, arr[j]);
j++;
}
}
}
댓글