블로그 상단광고2




[Data Structures in Java]1.Bag 자바 자료구조 공부

Bag 이란?


원소들을 단순히 모아 놓은 것


        *원소들 간에 아무 순서도 없다.

        *중복된 원소들도 있을 수 있다.


정보를 모아 놓은 일은 컴퓨터에서 자주 발생하는 일이다. 보통은 동일 자료형을 갖는 정보를 모아 놓게 된다.

간단한 것으로는 학생들의 성적만 모아 놓는다면 정수들이 될 것이며, 복잡하게는 성적을

비롯한 학번, 이름 등 학생 개인정보를 포함하는 경우에는 학생정보의 객체들을

모아 놓을 수도 있다.

저금통이라면 동전이나 지폐들을 모아 놓게 될 것이다.

이러한 모아 놓은 대상에 따라 모음의 특성이 존재하게 된다.

저금통의 경우에는 100원짜리 동전이 구분 없이 여러 개 들어가 있을 수 있다.

그러나 학생 정보의 경우에는 동일한 학생이 둘이 있을 수는 없다.


컴퓨터에 어떤 그룹의 정보를 모아놓으면, 이러한 그룹의 특성에 따라 그 그룹의 사용법이 달라질 것이며,

또한 그 정보를 처리하는 방법 또한 달라질 것이다.

이러한 그룹에 들어 있는 개개 정보를 원소(element)라고 한다.

원소가 동일한 것이 중복하여 들어가 있을 수 있는 그룹을 가방(bag)이라고 한다.

중복이 허락되지 않는 그룹을 집합(set)이라고 한다. 동일 원소의 중복 여부는 필요에 따라 정해지며,

그런데 원소들 사이에 순서가 있으면 리스트(list)라고 한다.



구현 방법



모든 객체는 크게 다음과 같이 그 사용법을 구분해 볼 수 있다.


        *첫째, 객체는 필요하면 생성하고, 사용이 끝나면 소멸시킨다.

          Java 언어를 사용할 경우에는 객체의 소멸은 사용자가 책임지지 않도록 되어 있다.

          그러므로 프로그램에서 사용이 끝난 객체를 소멸시키는 일은 하지 않는다.

          그러나 다른 언어에서는 사용자가 소멸시키는 책임이 있기도 하다.


        *둘째,객체의 상태를 알아보는 행위이다.


        *셋째 객체의 내용을 알아보는 행위이다.


        *넷째 객체의 내용을 변경시키는 행위이다.


우리는 앞으로 객체의 사용법을 정의하는 클래스의 공개함수를 이 네 가지 관점에서 살펴본다.


[가방 객체의 사용법]


가방 객체의 대표적인 사용법을 네 가지 관점에서 나열한 것이다.


*Bag 객체 생성과 소멸

        *Bag 객체 생성


*Bag 상태 알아보기

        *Bag에 들어있는 원소의 개수를 알기

        *Bag이 비어 있는지 알기

        *Bag이 가득 찾는지 알기

        *주어진 원소가 Bag에 있는지 알기

        *주어진 원소가 Bag에 몇 개 있는지 알기


*Bag 내용 알아보기

        *Bag에서 아무 원소 하나를 얻기


*Bag 내용 바꾸기

        *Bag에 주어진 원소를 넣기

        *Bag에서 아무 원소 하나를 제거

        *Bag에서 지정된 원소를 찾아서 제거

        *Bag Clear


Bag의 공개 함수


Bag 객체 사용법을 Java로 public method를 나열해 보았다.


        *public  ArrayBag();


        *public int size();

        *public boolean isEmpty();

        *public boolean isFull

        *public boolean doesCotain(Element givenElement);

        *public int frequencyOf(Element givenElement);


        *public Element any();


        *public boolean add(Element anElement);

        *public Element removeAny();

        *public boolean remove(Element givenElement);

        *public void clear();


[Class 구현]


public class ArrayBag{

        //비공개 인스턴스 함수

        private static final int DEFAULT_MAX = 100;

        private int _maxSize;

        private int _size;

        private Element _Elements[]; //ArrayBag의 원소를 담을 java 배열

        

        //기본 생성자

        public ArrayBag() {

                this._maxSize = DEFAULT_MAX;

                this._elements = new Element[DEFAULT_MAX];

        


        //오버로딩 생성자

        public ArrayBag(int givenMaxSize) {

                this._elements = new Element[givenMaxSize];

                this._maxSize = givenMaxSize;

        }


        public int size() {

                return this._size;

        }


        /*

        * Bag이 비어 있을 시 true를 return else false   

        */

        public boolean isEmpty() {

                if (this._size == 0)

                        return true;

                else

                        return false;

        }

        /*

        *Bag이 가득 차 있을 시 return true else false

        */

        public boolean isFull() {

                if (this._size == this._maxSize)

                        return true;

                else

                        return false;

        }


        public boolean doesCotain(Coin givenElement) {

                int i = 0;

                while (i < this._size) { // 일일이 찾아 다니는 방식

                        if (this._Elements[i].theSameValueAs(givenElement)) {

                                return true;

                        }

                        i++;

                }

                return false;

        }


        public int frequencyOf(Coin givenElement) {

                int frequencyofCount = 0;



                for (Element c : this._Elements) {

                        if (c != null && c.theSameValueAs(givenElement)) {

                                frequencyofCount++;

                        }

                }

                return frequencyofCount;

        }



        public Coin maxElementValues() {

                int max = 0;

                Coin maxElement = null;

                for (Element c : this._Elements) {

                        if (c != null && c.value() > max) {

                                max = c.value();

                                maxElement = c;

                        }

                }

                return maxElement;

        }


        public int sumElementValues() {

                int sum = 0;

                for (Element c : this._Elements) {

                        if (c != null) {

                                sum += c.value();

                        }

                }

                return sum;

        }


        public boolean add(Element aElement) {

                if (!this.isFull() && aElement.value() >= 0 && aElement.value() <= 1000) {

                        this._Elements[this._size] = aCoin;

                        this._size++;

                        return true;

                } else

                        return false;

        }


        public boolean remove(Element givenElement) {

                int foundElement = this._size;

                if (this._size == 0) {

                        return false;


                } else {

                        for (int i = 0; i < this._size; i++) { // 같은 것 찾기

                                if (this._Elements[i].theSameValueAs(givenElement)) {

                                        foundElement = i;

                                        break;

                                }

                        }


                        if (foundElement == this._size) { // 가방 안에 없음

                                return false;

                        } else { // 있으면 한칸씩 땡기고 마지막 null

                                for (int i = foundElement; i < this._size - 1; i++) {

                                        this._Elements[i] = this._Elements[i + 1];

                                }

                                this._size--;

                                this._Elements[this._size] = null;

                                return true;


                        }


                }

        }


        public void clear() {


                this._Elements = new Element[this._maxSize];

                this._size = 0;

        }


}


Element Class


public class Element {


        // 비공개 인스턴스 변수

        private int _value;


        public Element() {


        }


        public Element(int aValue) {

                this._value = aValue;

        }


        public int value() {

                return this._value;

        }


        public void setValue(int aValue) {

                this._value = aValue;

        }


        public boolean theSameValueAs(Element givenElement) {

                if (givenElement.value() == this._value) {

                        return true;

                } else

                        return false;

        }


}



제가 만들었던 걸 수정한 코드라 중간에 수정하다가 이상한 곳이 있을 수 도 있지만

그건 알아서 참고하세요..^^


덧글

댓글 입력 영역



호무리 시계 위젯(디지털)

구글광고

구글 서치

통계 위젯 (화이트)

00
8
107057