0

I am trying to figure out how does that method works, because i would like to make my own. So I came to this point:

public static deleteMe(int index){
 for(int i=0;i<arrayList.size;i++){
     if(i==index){
          // how to tell java to delete that member of list on the index i, 
          // but not to be arrayList.remove()
         }
   }
 }
16
  • 3
    Are you asking how to remove an element from java.util.ArrayList without calling remove() or how to implement remove(index) is some custom ArrayList like class you are implementing? Commented May 21, 2017 at 10:47
  • 2
    public static deleteMe(int index) this isn't even valid syntax... >_> Commented May 21, 2017 at 10:48
  • how to remove an element without calling remove() method.Thanks in advantage for your help Commented May 21, 2017 at 10:48
  • 2
    Just guessing, could it be that the assignment is to implement without using Java’s standard ArrayList class at all (not only without using its remove method)? Commented May 21, 2017 at 11:45
  • 1
    @javaprogrammewannabe you probably should have added your existing custom list class in order to provide the necessary information for others to answer this question. Commented May 21, 2017 at 12:20

1 Answer 1

2

ArrayList is based internally on a simple array, so when you delete by index you simply move everything that has higher index than the removed element one place down, look at te JDK implementation:

public E remove(int index) {
  rangeCheck(index);

  modCount++;
  E oldValue = elementData(index);

  int numMoved = size - index - 1;
  if (numMoved > 0)
  System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
  elementData[--size] = null; // clear to let GC do its work

  return oldValue;
}

Ofcourse the inner array (elementData) is package-access so you don't have access to it, that's the whole point. If you are implementing your own list I suggest extending the AbstractList. If you are not then this question doesn't make sense, like I said, that's the whole point of ArrayList to encapsulate the inner array so you can operate on it only through methods available to you.

If you want to delete not by index, but by passing some instance of type that the ArrayList is holding, that requires the equals check, so that type needs to properly override equals and hashCode methods.

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

3 Comments

note that the implementation you quote is from old version of java, nowadays it is a bit different. It doesn't change your point however which I agree with.
Yes i am implementing my own list. which should be modified with my own method that will delete some element which i would put in parameter. deleteMe(0)-for example..Thanks.
@javaprogrammewannabe on other comment you said you need to "remove from arraylist without using delete", now you say you're implementing your own list. Those are totally distinct things. Latter makes more sense, so that's probably what you're really doing.

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.