«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

ITGenerations

자바_벡터 클래스 본문

자바공부/자바

자바_벡터 클래스

ITGenerations 2018. 7. 13. 14:52
import java.util.*;


public class MyVector implements List {
Object[] data = null; //객체를 담기 위한 객체배열 선언
int capacity = 0; // 용량
int size = 0; // 크기

public MyVector(int capacity){
if(capacity<0)
throw new IllegalArgumentException("유효하지 않은 값입니다.:"+capacity);
this.capacity = capacity;
data = new Object[capacity];
}

public MyVector(){
this(10); // 크기를 지정하지 않으면 크기를 10으로 한다.
}

// 최소한의 저장공간(capacity)를 확보하는 메소드
public void ensureCapacity(int minCapacity){
if(minCapacity - data.length > 0)
setCapacity(minCapacity);
}
public boolean add(Object obj){
//새로운 객체를 저장하기 전에 저장할 공간을 확보한다.
ensureCapacity(size+1);
data[size++] = obj;
return true;
}

public Object get(int index){
if(index<0 || index>=size)
throw new IndexOutOfBoundsException("범위를 벗어났습니다.");
return data[index];
}

public Object remmove(int index){
Object oldObj = null;
if(index != size -1){
System.arraycopy(data,index+1,data,index,size-index-1);
}
// 마지막 데이터를 null로 한다. 배열은 0부터 시작하므로 마지막 요소는 index가 size -1 읻.
data[size-1] = null;
size--;
return oldObj;
}


public boolean remove(Object obj){
for(int i=0; i<size; i++){
if(obj.equals(data[i])){
remove(i);
return true;
}
}
return false;
}

public void trimToSize(){
setCapacity(size);
}

private void setCapacity(int capacity){
if(this.capacity==capacity) return;
Object[] temp =new Object[capacity];
System.arraycopy(data,0,tmp,0,size);
data = tmp;
this.capacity = capacity;
}

public void clear(){
for(int i = 0; i<size; i++)
data[i] = null;
size = 0;
}

public Object[] toArray(){
Object[] result = new Object[size];
System.arraycopy(data,0,result,0,size);
return result;
}

public boolean isEmpty(){return size==0;}
public int capacity(){ return capacity;}
public int size(){ return size;}
//
// List 인터페이스로부터 상속받은 메소드들
//
// public int size();
// public boolean isEmpty();

@Override
public boolean contains(Object o) {
return false;
}

@Override
public Iterator iterator() {
return null;
}
// public Object[] toArray();
public Object[] toArray(Object a[]){return null;}
// public boolean add(Object o);
// public boolean remove(Object o);
public boolean contatinsAll(Collection c){return false;}
public boolean addAll(Collection c){return false;}

@Override
public boolean addAll(int index, Collection c) {
return false;
}

@Override
public boolean removeAll(Collection c) {
return false;
}

@Override
public boolean retainAll(Collection c) {
return false;
}
// public void clear();
public boolean equals (Object o){return false;}
// public int hashCode();
// public Object get(int index);
// public Object set(int index, Object element){return null;}
public void add(int index, Object element){}
// public Object remove(int index);
public int indexOf(Object o){return -1;}
public int lastIndexOf(Object o){return -1;}
public ListIterator listIterator(){return null;}
public ListIterator listIterator(int index){ return null;}
public List subList(int fromIndex, int toIndex){return null;}

// default void sort(Comparator c){/* 내용 생략*/}
// default Spliterator spliterator(){/* 내용 생략*/}
// default void replaceAll(UnaryOperator operator){/* 내용 생략*/}


}


'자바공부 > 자바' 카테고리의 다른 글

1 to 100 합과 곱 구하기  (0) 2018.05.22
프론트엔드/백엔드 공부 과정 간략화  (0) 2018.05.10
객체 공부  (0) 2018.05.05
자바 개발자 면접시 키워드  (0) 2018.05.01
String 메소드  (0) 2018.04.28