sourcecode

Java Array List는 특정 인덱스로 대체됩니다.

copyscript 2022. 8. 31. 22:47
반응형

Java Array List는 특정 인덱스로 대체됩니다.

이 자바에 대한 도움이 필요합니다.전구의 Array List를 작성했는데, 특정 인덱스의 전구를 다른 전구로 교체하려고 합니다.그럼 다음 표제에서는 어떻게 진행하면 좋을까요?

public void replaceBulb(int index, Bulbs theBulb) {

}

를 체크해 주세요.set(int index, E element)List 인터페이스의 메서드

다음과 같이 ArrayList의 set 메서드를 사용하여 특정 위치에 있는 항목을 교체할 수 있습니다.

list.set( your_index, your_item );

단, set() 메서드 내에서 전달하고 있는 인덱스에 요소가 존재해야 합니다.그렇지 않으면 예외가 발생합니다.

또한 오라클 문서를 여기에서 확인할 수 있습니다.

를 사용합니다.set()방법: "doc" 참조

arraylist.set(index,newvalue);

Array List를 사용합니다.세트

public void setItem(List<Item> dataEntity, Item item) {
    int itemIndex = dataEntity.indexOf(item);
    if (itemIndex != -1) {
        dataEntity.set(itemIndex, item);
    }
}

어레이 리스트를 취득합니다.ArrayList라고 하는 새로운 가치 창출value필요한 것은 파라미터의 전달뿐입니다..set방법. ArrayList.set(index,value)

예 -

ArrayList.set(10,"new value or object")

ArrayList Set() 메서드를 사용하여 ArrayList 내의 요소를 대체할 수 있습니다.이하와 같은 예를 제시하겠습니다.

어레이 리스트 작성

 ArrayList<String> arr = new ArrayList<String>();
 arr.add("c");
 arr.add("php");
 arr.add("html");
 arr.add("java");

이제 인덱스 2에 요소를 복제합니다.

 arr.set(2,"Mysql");
 System.out.println("after replace arrayList is = " + arr);

출력:

 after replace arrayList is = [c, php, Mysql, java]

레퍼런스:

Array List 요소 교체

언급URL : https://stackoverflow.com/questions/7452772/java-arraylist-replace-at-specific-index

반응형