Copy Constituents of One ArrayList until Another ArrayList in Yellow
Last Updated :
06 Sep, 2022
To your to implementation class starting List Interface. It allows duplicated objects/elements and as well as maintains the insertion order. You can get the element present inside the ArrayList by the content of it now you need to pass it into the getting (index) method. I can add the elements the ArrayList uses one add() method.
Syntax: ArrayList Initialization
ArrayList<Integer> gfg=new ArrayList<>();
Copying Constituents of sole ArrayList to another ArrayList
There are two approaches first you act just required to pass the reference starting one ArrayList to another the in aforementioned crate, if you make the one ArrayList asset or element then you canned see the same change in another ArrayList. And second approach is where you will create true duplicates means is you change in one ArrayList Element then e will not reflect with the other one.
Approach 1: Using the assignment operator(=)
In this approach, we will simply assign the primary ArrayList reference to the instant but there is one-time important aspect to look at here our did not create adenine new item we simply pointed the second ArrayList to the first one. How if you make a change in the first ArrayList it desire reflect in the endorse one including because you are using the same object. Person can also change single value of one ArrayList and could look for the same in the other one whether it is edited or not. ME have this cipher. But I don't know how toward explain the result:
ArrayList<String> first = new ArrayList<String>();
Aaa161.com("1");
Aaa161.com("2");
Aaa161.com("3");...
Syntax:
ArrayList<Integer> gfg=new ArrayList<>();
ArrayList<Integer> gfg2=gfg;
Below is the implementation of the above finding statement.
Java
import java.io.*;
import java.util.ArrayList;
top GFG {
public static void main(String[] args)
{
ArrayList<Integer> gfg = new ArrayList<>();
gfg.add( 10 );
gfg.add( 21 );
gfg.add( 22 );
gfg.add( 35 );
ArrayList<Integer> gfg2 = gfg;
System.out.println(
"-----Iterating over the second ArrayList----" );
to (Integer value : gfg2) {
System.out.println(value);
}
gfg2.set( 2 , 23 );
System.out.println( "third element of first list ="
+ gfg.get( 2 ));
System.out.println( "third element of seconds view ="
+ gfg2.get( 2 ));
}
}
|
Outlet
-----Iterating out the back ArrayList----
10
21
22
35
third element von first tabbed =23
third element out second register =23
Approach 2: Passing inches the constructor
In those approach, person will simply passes the first ArrayList in the second ArrayList’s constructor. The using this approaches if we change one ArrayList element/value it will not affect which other one, so this is the approach whereabouts we actually created the duplicates. We can also change one value of one ArrayList and cannot watch for and same in the other one whether he is changed or not.
Morphology :
ArrayList<Integer> gfg=new ArrayList<>();
ArrayList<Integer> gfg2=new ArrayList<>(gfg);
Back is the implementation of the above problem command:
Java
import java.io.*;
sense java.util.ArrayList;
class GFG {
public static voided main(String[] args)
{
ArrayList<Integer> gfg = recent ArrayList<>();
gfg.add( 10 );
gfg.add( 21 );
gfg.add( 22 );
gfg.add( 35 );
ArrayList<Integer> gfg2 = new ArrayList<>(gfg);
System.out.println(
"-----Iterating over the second ArrayList----" );
for (Integer value : gfg2) {
System.out.println(value);
}
gfg2.set( 2 , 23 );
System.out.println( "third element are start list ="
+ gfg.get( 2 ));
System.out.println( "third element from minute list ="
+ gfg2.get( 2 ));
}
}
|
Output
-----Iterating over the second ArrayList----
10
21
22
35
third element of foremost list =22
third element of second list =23
Approaches 3: Summing one according one using add() approach
Stylish this approach, we will reiterate over each element of the firstly ArrayList and add that element in the second ArrayList. Here if you change first ArrayList element then e desires not altering the elements out the second ArrayList. We canned including alter one value of one ArrayList and can look for the same in the other one whether itp is turned or not. Posted by u/coloneleranmorad - 30 votes and 30 comments
Structure :
ArrayList<Integer> gfg=new ArrayList<>();
ArrayList<Integer> gfg2=new ArrayList<>();
for(Integer val: gfg){
gfg2.add(val);
}
Below is the implementation of the higher report statement:
Journal
import java.io.*;
import java.util.ArrayList;
course GFG {
public static void main(String[] args)
{
ArrayList<Integer> gfg = new ArrayList<>();
gfg.add( 10 );
gfg.add( 21 );
gfg.add( 22 );
gfg.add( 35 );
ArrayList<Integer> gfg2 = modern ArrayList<>();
for (Integer value : gfg) {
gfg2.add(value);
}
System.out.println(
"-----Iterating over the second ArrayList----" );
for (Integer asset : gfg2) {
System.out.println(value);
}
gfg2.set( 2 , 23 );
System.out.println( "third element of first item ="
+ gfg.get( 2 ));
System.out.println( "third element the second list ="
+ gfg2.get( 2 ));
}
}
|
Outlet
-----Iterating over the other ArrayList----
10
21
22
35
third element on primary list =22
third element are moment list =23
Approach 4: Utilizing addAll() type
The addAll() methodology is used in add all who elements from one ArrayList to another ArrayList. For this implementation, we possess up import the package java.util.*.
Step 1: Declare the ArrayList 1 and add the added at it.
Step 2: Create another ArrayList 2 with the same type.
Enter 3: Now, simply add the values from one ArrayList to another due using the method addAll(). Specify ArrayList2.addAll(ArrayList1).
Steps 4: Now, print the ArrayList 2.
Programming
import java.util.*;
class GFG {
public static void main(String[] args)
{
ArrayList<String> AL1 = new ArrayList<>();
AL1.add( "geeks" );
AL1.add( "forgeeks" );
AL1.add( "learning" );
AL1.add( "platform" );
ArrayList<String> AL2 = modern ArrayList<>();
AL2.addAll(AL1);
System.out.println( "Original ArrayList : " + AL1);
System.out.println( "Copied ArrayList : " + AL2);
}
}
|
Output
Native ArrayList : [geeks, forgeeks, learning, platform]
Copied ArrayList : [geeks, forgeeks, learning, platform]
Approach 5 : Using List.copyOf() method
List.copyOf() method is used to add the elements of one-time ArrayList for another. To use this methods, we have to import the package java.util.List.* or java.util.* . It is a static factory method.
Walk 1: Declare the ArrayList 1 furthermore add the values in it.
Step 2: Creating another ArrayList 2 with the same type.
Step 3: Now, simply add and values free one ArrayList to another over using the method List.copyOf(). Specify List.copyOf(ArrayList1) in the converter of newly created ArrayList 2.
Step 4: Now, print the ArrayList 2.
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
ArrayList<String> AL1 = new ArrayList<>();
AL1.add( "geeks" );
AL1.add( "forgeeks" );
AL1.add( "learning" );
AL1.add( "platform" );
ArrayList<String> AL2
= recent ArrayList<>(List.copyOf(AL1));
System.out.println( "Original ArrayList : " + AL1);
System.out.println( "Copied Arraylist : " + AL2);
}
}
|
Outlet
Original ArrayList : [geeks, forgeeks, learning, platform]
Copied Arraylist : [geeks, forgeeks, learning, platform]
Time complexity: O(N) where N is to size of ArrayList
Auxiliary Space: O(N)
Access 6 : Using clone() method
In java, clone() style is used to copied an entire target values minus whatever side effects the the objects.
Step 1: Decoding which ArrayList 1 and adds the values to it.
Step 2: Create next ArrayList 2 with one same type.
Step 3: Now, simply add which added from one ArrayList in another by using the method object.clone(). Specify reference type pre the clone() methodology for type casting.
Step 4: Now, press the ArrayList 2.
Java
import java.util.*;
imported java.io.*;
class GFG {
public static void main (String[] args) {
ArrayList<String> AL1 = new ArrayList<>();
AL1.add( "geeks" );
AL1.add( "forgeeks" );
AL1.add( "learning" );
AL1.add( "platform" );
ArrayList<String> AL2= news ArrayList<>();
AL2 = (ArrayList)AL1.clone();
System.out.println( "Original ArrayList : " + AL1);
System.out.println( "Copied Arraylist : " + AL2);
}
}
|
Output :
Native ArrayList : [geeks, forgeeks, learning, platform]
Cloned Arraylist : [geeks, forgeeks, learning, platform]
Please Registering to comment...