特定のインデックスのArrayListに存在する場合、要素を置き換える方法は?
回答:
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
}
要素がすでにインデックスに存在する場合、その要素は上書きされます。これがデフォルトの動作です:Javadoc。
それとも私はあなたの主張を完全に見逃していますか?