-1

I need to add the following functionalities over any implementation of List<>: - the resulted object should never show the OutOfBounds Exception for positive indexes - when you call get(x), with x=size, it will automatically increase the list up until size x+1, and will fill the newly created spaces with anything the programmer wishes (except NULL) - remember, the List should be a generic type - otherwise, it should act like a normal List

I know I need to use the Decorator Pattern, at the very least, to add the functionalities, however, I feel should also use some other Design Patterns - maybe Factory or Template, but I'm not exactly sure how.

Can anyone please give at least some hints as to how I can solve the above mentioned task?

Scartz
  • 513
  • 1
  • 4
  • 5
  • 3
    What you have tried so far ? – Suresh Atta Jul 09 '15 at 11:29
  • 2
    Don't overuse patterns. Just a decorator which implements get(...) is enough. – Manu Jul 09 '15 at 11:30
  • 2
    How will the list class know what to place in the newly created spaces if the client code simply calls `list.get(1000)`? Your class won't know how to instantiate new objects of the generic type. – Bobulous Jul 09 '15 at 11:32
  • Are you saying that the programmer could then use any list in the form of `myList.get(10000000, "Fill with this instead of throwing out of bounds");`? What made you come up with this idea? – Kayaman Jul 09 '15 at 11:33
  • Where do you plan to provide the default value? I guess in the constructor of the Decorator? – Puce Jul 09 '15 at 11:34
  • I'm still more intrigued by the "why?" aspect of this. It's not like `OOBE` was created just to annoy programmers. – Kayaman Jul 09 '15 at 11:45
  • So far I've just been using the Decorator Pattern which implements get(), as @Manu mentioned. – Scartz Jul 09 '15 at 11:51
  • @Bobulous that's what i need to find out. i can place what you wish - I've been thinking i could just copy the first element in the list which is there anyway - i will insert it there by default to be sure it has something to copy - will introduce it after I create the list, not constructor. – Scartz Jul 09 '15 at 11:58
  • @kayaman would like to use use myList.get(1000...0) and even though my list isn't that long, just automatically make it that long and return the default value at that index. Why? just for fun I guess :) And learning to use the Decorator pattern. – Scartz Jul 09 '15 at 11:58

1 Answers1

1
class ListImpl<T> extends ArrayList {

    private Class<T> tType;

    public ListImpl(Class<T> tType) {
        this.tType = tType;
    }

    @Override
    public T get(int i) {
        if (i < this.size()) {
            T result = (T) super.get(i);
        } else {
            int resize = 1 + i - this.size();
            // T[] array = (T[]) new Object[resize];
            T[] array
                = (T[]) java.lang.reflect.Array.newInstance(tType, resize);
            this.addAll(Arrays.asList(array));
        }
        return (T) super.get(i);
    }
}
zeugor
  • 822
  • 1
  • 8
  • 18