Programing

ArrayList의 특정 위치에서 요소를 어떻게 업데이트합니까?

lottogame 2020. 7. 3. 18:40
반응형

ArrayList의 특정 위치에서 요소를 어떻게 업데이트합니까? [복제]


이 질문에는 이미 답변이 있습니다.

ArrayList10 String중 하나 있습니다. 5다른 String값으로 인덱스 어떻게 업데이트 합니까?


하자 arrListArrayListnewValue새를 String, 그럼 그냥 할 :

arrList.set(5, newValue);

이것은 java api reference here 에서 찾을 수 있습니다 .


list.set(5,"newString");  

 arrList.set(5,newValue);

그리고 업데이트하고 싶다면이 줄도 추가하십시오.

 youradapater.NotifyDataSetChanged();

 import java.util.ArrayList;
 import java.util.Iterator;


 public class javaClass {

public static void main(String args[]) {


    ArrayList<String> alstr = new ArrayList<>();
    alstr.add("irfan");
    alstr.add("yogesh");
    alstr.add("kapil");
    alstr.add("rajoria");

    for(String str : alstr) {
        System.out.println(str);
    }
    // update value here
    alstr.set(3, "Ramveer");
    System.out.println("with Iterator");
    Iterator<String>  itr = alstr.iterator();

    while (itr.hasNext()) {
        Object obj = itr.next();
        System.out.println(obj);

    }
}}

arrayList.set (location, newValue); location = 여기서는 삽입, newValue = 삽입하려는 새 요소입니다.

통지는 선택 사항이며 조건에 따라 다릅니다.

참고 URL : https://stackoverflow.com/questions/4352885/how-do-i-update-the-element-at-a-certain-position-in-an-arraylist

반응형