반응형
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() 메서드 내에서 전달하고 있는 인덱스에 요소가 존재해야 합니다.그렇지 않으면 예외가 발생합니다.
또한 오라클 문서를 여기에서 확인할 수 있습니다.
arraylist.set(index,newvalue);
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]
레퍼런스:
언급URL : https://stackoverflow.com/questions/7452772/java-arraylist-replace-at-specific-index
반응형
'sourcecode' 카테고리의 다른 글
vue-router의 "$router.push()"에 사용자 지정 데이터 전달 (0) | 2022.08.31 |
---|---|
@RequestParam 목록 바인딩 (0) | 2022.08.31 |
함수에서 char*를 반환하는 것과 char[]를 반환하는 것의 차이점은 무엇입니까? (0) | 2022.08.31 |
VueX를 통한 데이터 액세스 (0) | 2022.08.31 |
x == (x = y)가 (x = y) == x와 같지 않은 이유는 무엇입니까? (0) | 2022.08.31 |