2014년 1월 1일 수요일

자바 ArrayList구현, under flow및 over flow 고려한 자바기반 ArrayList[재직자무료교육/프로그래머교육/구로디지털IT교육,오라클/자바/닷넷/C#/iOS/안드로이드/아이폰교육]

자바 ArrayList구현, under flow및 over flow 고려한 자바기반 ArrayList[재직자무료교육/프로그래머교육/구로디지털IT교육,오라클/자바/닷넷/C#/iOS/안드로이드/아이폰교육]


자바 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]의 출력 요구.
 }
}
 
 
<li tabindex="0" class="ui-state-default ui-corner-top ui-tabs-active ui-state-active" role="tab" aria-selected="true" aria-controls="tabs-1" aria-labelledby="ui-id-1">JAVA</li> <li tabindex="-1" class="ui-state-default ui-corner-top" role="tab" aria-selected="false" aria-controls="tabs-2" aria-labelledby="ui-id-2">ORACLE</li> <li tabindex="-1" class="ui-state-default ui-corner-top" role="tab" aria-selected="false" aria-controls="tabs-3" aria-labelledby="ui-id-3">iPhone/Android</li> <li tabindex="-1" class="ui-state-default ui-corner-top" role="tab" aria-selected="false" aria-controls="tabs-4" aria-labelledby="ui-id-4">.NET</li> <li tabindex="-1" class="ui-state-default ui-corner-top" role="tab" aria-selected="false" aria-controls="tabs-5" aria-labelledby="ui-id-5">표준웹/HTML5</li> <li tabindex="-1" class="ui-state-default ui-corner-top" role="tab" aria-selected="false" aria-controls="tabs-6" aria-labelledby="ui-id-6">채용/취업무료교육</li> <li tabindex="-1" class="ui-state-default ui-corner-top" role="tab" aria-selected="false" aria-controls="tabs-7" aria-labelledby="ui-id-7">초보자(재학생)코스</li>

댓글 없음:

댓글 쓰기