如果在给定索引的ArrayList中存在元素,该如何替换?
Answers:
arrayList.set(index i,String replaceElement);
如果您需要不同的设置功能,建议您使用自己的类扩展ArrayList。这样,您就不必在多个地方定义行为。
// 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
}