Java/디딤돌 Java 언어 Part2

[Java 소스] 정수 동적 배열, 제네릭 동적 배열

언제나휴일 2017. 1. 20. 12:04
반응형

[Java 소스] 정수 동적 배열, 제네릭 동적 배열


DArray.java

IntDArray.java

Program.java



//IntDArray.java

//정수 동적 배열

public class IntDArray {

        int[] buffer;

        int capacity;

        int usage;

        public IntDArray(int capacity){

               this.capacity = capacity;

               buffer = new int[capacity];

               usage = 0;

        }      

        public boolean isEmpty(){

               return usage == 0;

        }

        public boolean isFull(){

               return usage == capacity;

        }

        public int size(){

               return usage;

        }

        public boolean add(int value){

               if(isFull()){

                       return false;

               }                     

               buffer[usage] = value;

               usage++;

               return true;

        }

        public void viewAll(){

               String outstr = String.format("저장소 크기:%d 보관개수:%d",capacity,usage);

               System.out.println(outstr);

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

                       System.out.print(buffer[i]+" ");

               }

               System.out.println();

        }

}


//DArray.java

//제네릭 동적 배열

public class DArray<datatype> {

        Object[] buffer;

        int capacity;

        int usage;

        public DArray(int capacity){

               this.capacity = capacity;             

               buffer = new Object[capacity];

               usage = 0;

        }      

        public boolean isEmpty(){

               return usage == 0;

        }

        public boolean isFull(){

               return usage == capacity;

        }

        public int size(){

               return usage;

        }

        public boolean add(datatype value){

               if(isFull()){

                       return false;

               }                     

               buffer[usage] = value;

               usage++;

               return true;

        }

        public void viewAll(){

               String outstr = String.format("저장소 크기:%d 보관개수:%d",capacity,usage);

               System.out.println(outstr);

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

                       System.out.print(buffer[i]+" ");

               }

               System.out.println();

        }

}


//Program.java

//정수 동적 배열과 제네릭 동적 배열

public class Program {

        public static void main(String[] args){

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

               IntDArray idarr = new IntDArray(10);

               idarr.viewAll();

               idarr.add(3);

               idarr.viewAll();

               idarr.add(2);

               idarr.viewAll();

               idarr.add(6);

               idarr.viewAll();

              

               System.out.println("==Test DArray<integer>===");

               DArray<Integer> darr = new DArray<Integer>(10);

               darr.viewAll();

               darr.add(3);

               darr.viewAll();

               darr.add(2);

               darr.viewAll();

               darr.add(6);

               darr.viewAll();

              

               System.out.println("==Test DArray<String>===");

               DArray<String> darr2 = new DArray<String>(10);

               darr2.viewAll();

               darr2.add("Hello");

               darr2.viewAll();

               darr2.add("언제나 휴일");

               darr2.viewAll();

               darr2.add("ehpub.co.kr");

               darr2.viewAll();

        }

}


실행 결과 

==Test IntDArray===

저장소 크기:10 보관개수:0

 

저장소 크기:10 보관개수:1

3

저장소 크기:10 보관개수:2

3 2

저장소 크기:10 보관개수:3

3 2 6

==Test DArray<integer>===

저장소 크기:10 보관개수:0

 

저장소 크기:10 보관개수:1

3

저장소 크기:10 보관개수:2

3 2

저장소 크기:10 보관개수:3

3 2 6

==Test DArray<String>===

저장소 크기:10 보관개수:0

 

저장소 크기:10 보관개수:1

Hello

저장소 크기:10 보관개수:2

Hello 언제나 휴일

저장소 크기:10 보관개수:3

Hello 언제나 휴일 ehpub.co.kr 




본문

[Java 활용] 3. 제네릭과 컬렉션

반응형