- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to copy value from one list to another list
Let’s say the following is our array −
String[] str = { "P", "Q", "R", "S", "T", "U", "V", "W" };
Now set the elements of the above array to a new List −
int len = str.length; List<String>list1 = new ArrayList<String>(); for (int i = 0; i < len; i++) list1.add(str[i]);
Consider a new List with no elements −
List<String>list2 = new ArrayList<String>(); for (int i = 0; i < len; i++) list2.add("");
Now copy value from one list to another −
Collections.copy(list2,list1);
Example
import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class Demo { public static void main(String[] args) { String[] str = { "P", "Q", "R", "S", "T", "U", "V", "W" }; int len = str.length; List<String>list1 = new ArrayList<String>(); for (int i = 0; i < len; i++) list1.add(str[i]); List<String>list2 = new ArrayList<String>(); for (int i = 0; i < len; i++) list2.add(""); Collections.copy(list2,list1); ListIterator<String>iterator = list2.listIterator(); System.out.println("New List..."); while (iterator.hasNext()) System.out.println(iterator.next()); } }
Output
New List... P Q R S T U V W
- Related Articles
- How do you copy an element from one list to another in Java?
- How to copy a list to another list in Java?
- Java Program to copy all the key-value pairs from one Map into another
- How to write a program to copy characters from one file to another in Java?
- Python program to mask a list using values from another list
- Copy all the elements from one set to another in Java
- C# program to copy a range of bytes from one array to another
- Python program to get the indices of each element of one list in another list
- Java Program to construct one String from another
- Java Program to Call One Constructor from another
- Java Program to put value to a Property list
- Copy values from one array to another in Numpy
- Golang program to copy one file into another file
- C# program to clone or copy a list
- Python program to clone or copy a list.

Advertisements