Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Array list deep copy

 
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()));
    }
}
 
Comment

Array list deep copy

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);   
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: min height bst leetcode 
Java :: Java Insert Elements to HashSet 
Java :: java count to 10 
Java :: viewpager androidx dependencies 
Java :: start activity for result deprecate 
Java :: fill two dimensional array 
Java :: how to move a marker on google maps in android studio 
Java :: java convert array to list 
Java :: factors 
Java :: arraylist of double 
Java :: java constructor chaining 
Java :: android:windowLightStatusBar programmatically 
Java :: passing array by reference java 
Java :: java timer 
Java :: how to initialize array in java with 0 
Java :: difference between two LocalDateTime java 
Java :: how to import class from another file in java 
Java :: java random int between 2 numbers 
Java :: java print type of variable 
Java :: Java Writer Using FileWriter 
Java :: string to int in java 
Java :: Access items from the ArrayList using get() function 
Java :: load contents of file into string java 
Java :: java random double between 0 and 1 
Java :: java combobox get selected item 
Java :: esponente in java 
Java :: java check if path exists 
Java :: convert int to byte java 
Java :: sorting list in java 
Java :: how to call a function in java 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =