한번에 끝내는 Java/Spring 웹 개발 마스터 초격차 패키지_패스트 캠퍼스 챌린지 22일차

2021. 9. 27. 21:26IT공부/Java&Spring 이용한 웹프로그래밍

02. 배열(Array) 구현하기

Array의 특징

  • 동일한 데이터 타입을 순서에 따라 관리하는 자료 구조
  • 정해진 크기가 있음
  • 요소의 추가와 제거시 다른 요소들의 이동이 필요함
  • 배열의 i 번째 요소를 찾는 인덱스 연산이 빠름
  • jdk 클래스 : ArrayList, Vector

Array 구현하기

MyArray.java

public class MyArray {

          int[] intArr; //int array

          int count; //개수

 

          public int ARRAY_SIZE;

          public static final int ERROR_NUM = -999999999;

         

         public MyArray() {

              count = 0;

              ARRAY_SIZE = 10;

              intArr = new int[ARRAY_SIZE];

         }

         public MyArray(int size) {

            count = 0;

            ARRAY_SIZE = size;

            intArr = new int[size];

         }

        public void addElement(int num) {

           if(count >= ARRAY_SIZE){

             System.out.println("not enough memory");

             return;

           }

            intArr[count++] = num;

        }

        public void insertElement(int position, int num) {

              int i;

             

             if(count >= ARRAY_SIZE){    //꽉 찬 경우

                  System.out.println("not enough memory");

                  return;

             }

             if(position < 0 || position > count ){ //index error

                     System.out.println("insert Error");

                     return;

             }

             for( i = count-1; i >= position ; i--){

                  intArr[i+1] = intArr[i]; // 하나씩 이동

             }

 

              intArr[position] = num;
              count++;

}

public int removeElement(int position) {

             int ret = ERROR_NUM;

             if( isEmpty() ){

                     System.out.println("There is no element");

                     return ret;

             }

             if(position < 0 || position >= count ){ //index error

                   System.out.println("remove Error");

                   return ret;

              }

              ret = intArr[position];

             

              for(int i = position; i<count -1; i++ )

              {

                     intArr[i] = intArr[i+1];

               }

              count--;

              return ret;

}

 

public int getSize() {

           return count;

}

 

public boolean isEmpty() {

              if(count == 0){

                      return true;

              }

              else return false;

 }

 

public int getElement(int position) {

            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<count; i++){

                   System.out.println(intArr[i]);

            }

}

  public void removeAll()

  {

         for(int i=0; i<count; i++){

               intArr[i] = 0;

         }

         count = 0;

     }

}

 

 

MyArrayTest.java

public class MyArrayTest {

            public static void main(String[] args) {

                    MyArray array = new MyArray();

                    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("===============");

 

                   array.addElement(70);

                   array.printAll();

                   System.out.println("===============");

                   array.removeElement(1);

                   array.printAll();

 

                  System.out.println("===============");

                  System.out.println(array.getElement(2));

                  }

}

 

MyObjectArray.java

public class MyObjectArray {

                     private int cout;

                     private Object[] array;

                     public int ARRAY_SIZE;

                     

                      public MyObjectArray() {

                        ARRAY_SIZE = 10;

                         array = new Object[ARRAY_SIZE];

                      }

                       public MyObjectArray(int size) {

                       ARRAY_SIZE = size;

                         array = new Object[ARRAY_SIZE];

                       }

}

--- 배열은 크기를 정의해야한다는 것을 알았고 , 만약 원래 정한 크기보다 많은 양의 데이터를 추가할려면 

     새로 정의해서 그 배열에 넣어야 한다는 것을 알았습니다.,

본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성되었습니다.

강의에 대해 정확하게 알고 싶다면 

https://bit.ly/37BpXiC