2013년 9월 11일 수요일

자바 ArrayList구현, under flow및 over flow 오버플로우, 언더플로우 고려한 자바기반 ArrayList 자바 ArrayList구현, under flow및 over flow 고려한 자바기반 ArrayList

자바 ArrayList구현, under flow및 over flow 오버플로우, 언더플로우 고려한 자바기반 ArrayList


자바 ArrayList구현, under flow및 over flow 고려한 자바기반 ArrayList
 
 
interface ArrayInterface {
  public void add(Object o);
  public void add(int index, Object o);
  public void removeAll();
  public boolean remove(Object o);
  public boolean remove(int idx);
  public int size();
}
 
//under flow및 over flow 고려한
//Collection (Array List형태)
class MyArray implements ArrayInterface {
 Object[] myArray ;
 MyArray() {
  myArray = new Object[0]; 
 }
 MyArray(int i) {
  if (i < 0)  throw new IllegalArgumentException();
     myArray = new Object[i];
 } 
 public int size() {
  for(int i=0;i<myArray.length;i++) {
   if(myArray[i]==null) return i;
  }
  return myArray.length;
 }
 public void ensureCapacity(int minCapacity)   {
  //minCapacity가 현재 size보다 클경우에만 처리할수도 있슴 
       Object[] newArray = new Object[minCapacity];
    System.arraycopy(myArray, 0, newArray, 0, size());
        myArray = newArray;   
   }
 public void add(Object  s) {
  ensureCapacity(size() + 1);
        myArray[size()] = s;
 }
 public void add(int index, Object  s) {
  int size = size();
  if (index > size){
   System.out.println("Invalid Index...");
  }
  else {
   if (size == myArray.length) ensureCapacity(size + 1);
   //arraycopy로 대체가 가능하다.
   //if (index != size)
        //System.arraycopy(myArray, index, myArray, index + 1, size - index);
     //myArray[index] = s;
   if(index<size) {
    for(int i=size;i>index;i--) {
     myArray[i] = myArray[i-1];
    }
    myArray[index]=s;
   }
  }
 }
 public boolean remove(int idx) {
  int size = size();
 
  if(idx<size) {
   myArray[idx]=null;
   for(int i=idx;i<size-1;i++) {
    myArray[i] = myArray[i+1];
    myArray[i+1] = null;
   }
   //삭제의 경우라면 under flow를 생각안할수도 있슴.
   ensureCapacity(size-1);
   return true;
  }
 
  return false;
 }
 public boolean remove(Object s) {
  int i=0, idx=0, size=0;
  size = size();
  for(i=0;i<size;i++) {
   if (myArray[i].equals(s)) {
    idx = i;
    break;
   }
  }
  if (i == size) {
   System.out.println("Data Not Found....");
   return false;
  }
  if(idx<size) {
   myArray[idx]=null;
   for(i=idx;i<size-1;i++) {
    myArray[i] = myArray[i+1];
    myArray[i+1] = null;
   }
   ensureCapacity(size-1);
   return true;
  }
  return false;
 }
 public void removeAll() {
  Object[] myArray = new Object[0];
 }
 public String toString() { 
  String s = "[";
  for(int i=0;i<size();i++) {
   s = s + myArray[i];
   if (i != size()-1){
    s = s + ", ";
   }
  }
  s = s + "]";
  return s; 
 }
}
 
 
 
class MyArrayMain {
  public static void main(String[] args) {
    MyArray array = new MyArray();
    array.add("First");
    array.add("Second");
    array.add("Third");
    System.out.println(array);   // [First, Second, Third] 의 출력 요구.     
    array.remove("Third");
    System.out.println(array); // [First, Second] 의 출력 요구.
    array.remove(0);
    System.out.println(array);  // [Second]의 출력 요구.
 array.add(0, "Zero");
 System.out.println(array);  // [Zero, Second]의 출력 요구.
 }
}

댓글 없음:

댓글 쓰기