1

I want to have an ArrayList, and to restrict element delinting as well. How can I do it?

3
  • Do you want an ArrayList where you cannot ever delete an element from it? Why is this tagged as magic? Commented Apr 29, 2014 at 15:57
  • I think it's very difficult case. Like a magic for me. Commented Apr 29, 2014 at 15:58
  • 4
    @Arkasha nothing is the programming world is magic (except for good decent optimal readable code from a newcomer, that is magic). Commented Apr 29, 2014 at 16:02

4 Answers 4

6

Create a wrapper over List interface that doesn't allow removal, just the desired methods:

class MyArrayList<T> {
    private List<T> list;

    public MyArrayList() {
        list = new ArrayList<T>();
    }

    public boolean add(T t) {
        return list.add(t);
    }
    //add other desired methods as well
}

Another non recommendable option is to extend from ArrayList and override remove method(s) to do nothing, but it is not desired because you should not extend from these classes unless it is really needed:

public MyArrayList<T> extends ArrayList<T> {
    @Override
    public boolean remove() {
        //do nothing...
    }
}

Also, you could wrap your current List using Collections#unmodifiableList to avoid elements removal (that is a wrapper class like mentioned above except that implements List) but note that you cannot add elements either.

Sign up to request clarification or add additional context in comments.

2 Comments

to make it work better i would add implementation of List interface, so wrapper could be put in List<T> fields and it presence would be transparent.
@T.G that would depend on OP's design. I don't want to bind the proposed wrapper to List interface (note the comment at bottom of my answer).
3

Create a subclass of ArrayList and override the remove methods.

If you want to forbid all modifications use Collections.unmodifiableList.

Comments

0

Use the ImmutableList class from the google guava library.

BTW: this is a duplciate of: make ArrayList Read only

1 Comment

The TO wants prevent delete only ... not all actions :)
0

You can create your own class extending ArrayList with will block all remove methods like in example

public class NoDeleteArray extends ArrayList{

    @Override
    public boolean remove(Object o) {
        return super.remove(o); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean removeAll(Collection c) {
        return false;
    }

    @Override
    public Object remove(int index) {
        return null;
    }

    @Override
    protected void removeRange(int fromIndex, int toIndex) {

    }

    @Override
    public boolean retainAll(Collection c) {
         return false;
    }

}

here all remove methods are covering original methods and does nothing. you can also perform some verification to check if object can be deleted from collection for example

@Override
public Object remove(int index) {
    MyClass tmp = (MyClass) get(index);
    if (tmp.isDeletable()) {
        return super.remove(index);
    } else {
        return null;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.