Program Tip

주어진 인덱스에 존재하는 경우 ArrayList 대체 요소?

programtip 2020. 10. 23. 08:18
반응형

주어진 인덱스에 존재하는 경우 ArrayList 대체 요소?


주어진 인덱스의 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 입니다.

아니면 내가 당신의 요지를 완전히 놓치고 있습니까?


remove ()뒤에 중단을 추가하십시오 .

참고 URL : https://stackoverflow.com/questions/5617175/arraylist-replace-element-if-exists-at-a-given-index

반응형