티스토리 뷰

Java

Best Data Container

techbard 2016. 7. 8. 21:59



# Animal.java


public class Animal {

    

    private String name;

    

    public Animal() {

    }


    public Animal(String name) {

this.name = name;

    }

    

    public String getName() {

        return name;

    }


    @Override

    public String toString() {

        return String.format("%s, %s\n", getClass(), this.name);

    }


    @Override

    public boolean equals(Object obj) {

if (obj == null) {

   return false;

}

if (!(obj instanceof Animal)) {

   return false;

}

return (this.getName().equals(((Animal)obj).getName()));

    }

}



# BestDataContainer.java


import java.util.NoSuchElementException;


public class BestDataContainer<T> {


    private Object[] data;

    public int countItems;


    public BestDataContainer() {

this(10);

    }


    public BestDataContainer(int capacity) {

data = new Object[capacity];

countItems = 0;

    }


    public int size() {

return countItems;

    }


    public boolean add(T item) {

if (item == null) {

   return false;

}

if (countItems == data.length) {

   return false;

}

int itemIndex = findItem(item);

if (itemIndex > 0) {

   return false;

}

data[countItems++] = item;

return true;

    }


    public boolean remove(int index) {

validateIndex(index);

int i = index;

int j = i + 1;


while (j < countItems) {

   data[j - 1] = data[j];

   j++;

}

countItems--;

return true;

    }


    public boolean remove(T item) {

if (item == null) {

   return false;


}


int index = findItem(item);

if (index < 0) {

   return false;

}


return remove(index);

    }


    public int findItem(T item) {

for (int i = 0; i < countItems; i++) {

   if (data[i].equals(item)) {

return i;

   }

}


return -1;

    }


    public T get(int index) {

validateIndex(index);

return (T) data[index];

    }


    private void validateIndex(int index) {

if (index < 0 && index >= countItems) {

   throw new NoSuchElementException("Invalid Index");

}

    }


    @Override

    public String toString() {

StringBuilder sb = new StringBuilder();


for (int i = 0; i < countItems; i++) {

   sb.append("Next: Item: " + data[i] + "\n");

}


return sb.toString();

    }

}



# Main.java


public class Main {


    public static void main(String[] args) {

BestDataContainer<Animal> bdc = new BestDataContainer<>(10);

bdc.add(new Animal("Bear"));

bdc.add(new Animal("Dog"));

bdc.add(new Animal("Eagle"));

bdc.add(new Animal("Frog"));

bdc.add(new Animal("Jaguar"));

System.out.println(bdc);

bdc.remove(2);

System.out.println(bdc);

bdc.remove(new Animal("Frog"));

System.out.println(bdc);

System.out.println(bdc.get(2));

    }

}


# 결과


Next: Item: Animal, Bear


Next: Item: Animal, Dog


Next: Item: Animal, Eagle


Next: Item: Animal, Frog


Next: Item: Animal, Jaguar



Next: Item: Animal, Bear


Next: Item: Animal, Dog


Next: Item: Animal, Frog


Next: Item: Animal, Jaguar



Next: Item: Animal, Bear


Next: Item: Animal, Dog


Next: Item: Animal, Jaguar



class Animal, Jaguar



댓글
댓글쓰기 폼
공지사항
Total
410,766
Today
35
Yesterday
44
«   2019/12   »
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 31        
글 보관함