

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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?
- Copy all the elements from one set to another in Java
- Python program to mask a list using values from another list
- Copy values from one array to another in Numpy
- Java Program to construct one String from another
- Java Program to Call One Constructor from another
- C# program to copy a range of bytes from one array to another
- How can we copy one array from another in Java
- How to copy a table from one MySQL database to another?
- How to copy files from one folder to another using Python?
- How to copy files from one server to another using Python?
- How to copy rows from one table to another in MySQL?
Advertisements