“Daftar Array Salinan Dalam” Kode Jawaban

Daftar Array Salinan Dalam

 
public class CloneArrayListMain {
 
    public static void main(String[] args) {
        ArrayList<Student> studentList = new ArrayList<>();
        studentList.add(new Student("Rohan","102",new Date(2002, 02, 12)));
        studentList.add(new Student("David","105",new Date(1999, 05, 30)));
 
        ArrayList<Student> clonedStudentList = new ArrayList<>();
 
        for(Student st:studentList)
        {
            clonedStudentList.add(st.clone());
        }
        // Update list
        clonedStudentList.get(0).setName("John");
        System.out.println("-----------Original List-----------");
        studentList.forEach(e->System.out.println(e.getName()+" "+e.getId()));
        System.out.println("-----------Copied List----------");
        clonedStudentList.forEach(e->System.out.println(e.getName()+" "+e.getId()));
    }
}
 
Testy Tarsier

Daftar Array Salinan Dalam

ArrayList clone() example
import java.util.ArrayList;
 
public class ArrayListExample 
{
    @SuppressWarnings("unchecked")
    public static void main(String[] args) 
    {
        ArrayList<String> arrayListObject = new ArrayList<>(); 
         
        arrayListObject.add("A");
        arrayListObject.add("B");
        arrayListObject.add("C");
        arrayListObject.add("D");
         
        System.out.println(arrayListObject);   
         
        ArrayList<String> arrayListClone =  (ArrayList<String>) arrayListObject.clone();
         
        System.out.println(arrayListClone);   
    }
}
Clever Crossbill

Jawaban yang mirip dengan “Daftar Array Salinan Dalam”

Pertanyaan yang mirip dengan “Daftar Array Salinan Dalam”

Lebih banyak jawaban terkait untuk “Daftar Array Salinan Dalam” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya