블로그 상단광고2




[Data Structures in Java]2.Set 자바 자료구조 공부

Set 이란?

원소들의 집합

-원소들 사이에 암무 순서도 없다.

-중복된 원소는 존재하지 않는다.


집합(set)은 가방(bag)과 유사하지만, 중요한 차이가 있다. 즉, 집합은 중복된 원소를 허락하지 않는다. 그러므로 집합의 공개함수들은 가방에서 공개함수들과 사용법이 같은 것도 있지만, 일부 공개함수는 집합의 특성이 반영되게 된다.


[구현 방법]


Set 객체 사용법


Set 객체 생성과 소멸

        -Set객체 생성

Set 상태 알아보기

        -Set에 들어있는 원소의 개수는?

        -Set이 비어 있는지?

        -Set이 가득 찾는지?

        -주어진 원소가 Set에 있는지?

Set 내용 알아보기

        -Set에서 아무 원소 하나를 얻으시오.

Set 내용 바꾸기

        -Set에 주어진 원소를 삽입

        -Set에서 지정된 원소 하나를 제거

        -Set에서 아무 원소 하나 제거

        -Set Clear


Set의 공개함수

        public ArraySet();

        

        public int size();

        public boolean isEmpty();

        public boolean isFull();

        public boolean doesContain(Element givenElement);


        public Element any();

        

        public boolean add(Element anElement);

        public Element removeAny();

        public boolean remove(Element anElement);

        public void clear();


집합 객체를 사용하기 위한 공개함수들이다. 이중에서 add()의 경우, 가방이라면 단순이 가방에 넣기만 하면 되었다. 그러나 집합에서는 만일 넣으려고 하는 원소가 이미 집합 중에 존재할 경우에는 넣기를 할 수 없게 된다. 그러므로 결과로서 넣기를 실패하면 false 값을 얻게 된다.


[Class 구현]


public class ArraySet {

        private static final int DEFAULT_MAX_SIZE = 100;

        private static final int DEFAULT_MAX_COORDINATE_SIZE = 100;

        private int _maxSize;

        private int _size;

        private Star _stars[]; // ArraySet의 원소들을 담을 java 배열


        public ArraySet() {

                this._stars = new Star[DEFAULT_MAX_SIZE];

                this._maxSize = DEFAULT_MAX_SIZE;

        }


        public ArraySet(int givenMaxSize) {

                this._stars = new Star[givenMaxSize];

                this._maxSize = givenMaxSize;

        }


        public int size() {

                return this._size;

        }


        public boolean isEmpty() {

                if (this._size == 0)

                        return true;

                else

                        return false;

        }


        public boolean isFull() {

                if (this._size == this._maxSize)

                        return true;

                else

                        return false;

        }


        public boolean doesContain(Star givenStar) {

                int i = 0;

                while (i < this._size) {

                        if (this._stars[i].theSameValueAs(givenStar)) {

                                return true;

                        }

                        i++;

                }

                return false;

        }


        public boolean add(Star aStar) {

                if (!(this.isFull() || doesContain(aStar)) || ((aStar.xCoordinate() >= -100 && aStar.xCoordinate() <= 100) && (aStar.yCoordinate() >= -100 && aStar.yCoordinate() <= 100))) { // 꽉차거나 별이 있으면 false

                        this._stars[this._size] = aStar;

                        this._size++;

                        return true;

                } else

                        return false;

        }


        public boolean remove(Star givenStar) {

                int foundCoin = this._size;

                if (this._size == 0) {

                        return false;


                } else {

                        for (int i = 0; i < this._size; i++) { // 주어진 코인과 같은 코인 찾기

                                if (this._stars[i].theSameValueAs(givenStar)) {

                                        foundCoin = i;

                                        break;

                                }

                        }


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

                                return false;

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

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

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

                                }

                                this._size--;

                                this._stars[this._size] = null;

                                return true;


                        }


                }

        }


        public boolean removeAny() {

                if (isEmpty()) {

                        return false;

                } else {

                        this._stars[this._size - 1] = null;

                        return true;

                }

        }


        public void clear() {

                this._stars = new Star[this._maxSize];

                this._size = 0;

        }


}


public class Star {

        private int _xCoordinate;

        private int _yCoordinate;

        private String _starName;


        public Star() {

                // TODO Auto-generated constructor stub

                this._starName = null;

                this._xCoordinate = 0;

                this._yCoordinate = 0;

        }


        public Star(int aX, int aY) {

                this._xCoordinate = aX;

                this._yCoordinate = aY;

        }


        public Star(String aStarName) {

                this._starName = aStarName;

        }


        public Star(int aX, int aY, String aStarName) {

                this._xCoordinate = aX;

                this._yCoordinate = aY;

                this._starName = aStarName;

        }


        public int xCoordinate() {

                return this._xCoordinate;

        }


        public int yCoordinate() {

                return this._yCoordinate;

        }


        public String starName() {

                return this._starName;

        }


        public void setXCoordinate(int aX) {

                this._xCoordinate = aX;

        }


        public void setYCoordinate(int aY) {

                this._yCoordinate = aY;

        }


        public void setStarName(String aStarName) {

                this._starName = aStarName;

        }


        public boolean theSameValueAs(Star givenStar) {

                if (this._xCoordinate == givenStar.xCoordinate()

                                && this._yCoordinate == givenStar.yCoordinate()) {

                        return true;

                } else if (this._starName.equals(givenStar.starName())) {

                        return true;

                } else {

                        return false;

                }

        }

}



덧글

댓글 입력 영역



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

구글광고

구글 서치

통계 위젯 (화이트)

00
8
107057