数组列表替换元素是否存在于给定的索引中?

如果数组列表中存在给定索引的元素,如何替换它?

89763 次浏览

If you're going to be requiring different set functionaltiy, I'd advise extending ArrayList with your own class. This way, you won't have to define your behavior in more than one place.

// You can come up with a more appropriate name
public class SizeGenerousArrayList<E> extends java.util.ArrayList<E> {


@Override
public E set(int index, E element) {
this.ensureCapacity(index+1); // make sure we have room to set at index
return super.set(index,element); // now go as normal
}


// all other methods aren't defined, so they use ArrayList's version by default


}

An element is over-written if it already exists at an index, that is the default behaviour: Javadoc.

Or am I missing your point completely?

  arrayList.set(index i,String replaceElement);

Just add a break after your remove() statement

just use this method inside arraylist

list.set(/*index*/,/*value*/)