2013년 8월 13일 화요일

Java ArrayList

예제1


오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(신입사원채용무료교육, 오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷)  



===================================================================
//under flow및 over flow 고려하지 않은 배열기반의 
//Collection (Array List형태)
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(); 
}

class MyArray implements ArrayInterface {
Object[] myArray ;
MyArray() {
myArray = new Object[20];
}
public int size() {
for(int i=0;i<myArray.length;i++) {
if(myArray[i]==null) return i;
}
return myArray.length;
}

public void add(Object  s) {
myArray[size()]=s;
}

public void add(int index, Object  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;
}
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;
}
return true;
}
return false;
}

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

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] 의 출력 요구.     
    System.out.println("Array Size : " + array.size());
    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]의 출력 요구.
    array.add(1, "First");
    System.out.println(array);  // [Zero, First, Second]의 출력 요구.
 }
}


예제2
===================================================================
//under flow및 over flow 고려한
//Collection (Array List형태)
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(); 
}

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]의 출력 요구.
 }
}

댓글 없음:

댓글 쓰기