티스토리 뷰

Java

Iterable BestDataContainer

techbard 2016. 7. 10. 11:37

IterableBestDataContainer.zip



# 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\n", 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()));

    }

}



# IterableBestDataContainer.java


import java.util.Iterator;

import java.util.NoSuchElementException;


public class IterableBestDataContainer<E> implements Iterable<E> {

    private Object[] data;

    private int countItems;


    public IterableBestDataContainer() {

this(10);

    }


    public IterableBestDataContainer(int capacity) {

data = new Object[capacity];

countItems = 0;

    }


    public int size() {

return countItems;

    }


    public boolean add(E item) {

if (item == null)

   return false;

if (countItems == data.length)

   return false;


// don't add duplicate

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] = data[j - 1];

   j++;

}

countItems--;

return true;

    }


    public boolean remove(E item) {

if (item == null)

   return false;


int index = findItem(item);

if (index < 0)

   return false;


return remove(index);

    }


    public int findItem(E item) {

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

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

return i;

}


return -1;

    }


    public E get(int index) {

validateIndex(index);


return (E) 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();

    }


    @Override

    public Iterator<E> iterator() {

return new BestDataContainerIterator<E>();

    }


    public Iterator<E> iterator(int index) {

return new BestDataContainerIterator<E>(index);

    }


    private class BestDataContainerIterator<E> implements Iterator<E> {

private int currentIndex;

private int priorIndex;

private final int BAD_INDEX = -1;


public BestDataContainerIterator() {

   this(0);

}


public BestDataContainerIterator(int index) {

   validateIndex(index);

   currentIndex = index;

   priorIndex = BAD_INDEX;

}


@Override

public boolean hasNext() {

   return currentIndex < size();

}


@Override

public E next() {

   if (!hasNext())

throw new NoSuchElementException();


   priorIndex = currentIndex;

   @SuppressWarnings("unchecked")

   E element = (E) IterableBestDataContainer.this.data[currentIndex++];

   return element;

}


@Override

public void remove() {

   validateRemoveState();

   

   IterableBestDataContainer.this.remove(priorIndex);

   priorIndex = BAD_INDEX;

}


private void validateRemoveState() {

   if (priorIndex == BAD_INDEX) throw new IllegalStateException("Must move next before removing items");

}

    }

}



# Zoo.java


import java.util.Iterator;


public class Zoo {


    public static void main(String[] args) {

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


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

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

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

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

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

IterableBestDataContainer<Animal> ibdc = new IterableBestDataContainer<Animal>();

ibdc.add(new Animal("Chipmunk"));

ibdc.add(new Animal("Hedgehog"));

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

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

ibdc.add(new Animal("Eagele"));

for (Animal a : ibdc) {

   System.out.println(a);

}

System.out.println("====================");

Iterator<Animal> itr = ibdc.iterator();

while (itr.hasNext()) {

   System.out.println(itr.next());

}

    }

}



# 결과


Chipmunk


Hedgehog


Dog


Frog


Eagele


====================

Chipmunk


Hedgehog


Dog


Frog


Eagele


댓글
댓글쓰기 폼
공지사항
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        
글 보관함