Duplicating a LinkedList
Hi fellows,
I've just started to toy around with the Linked Lists in Java, so I most of the time have no clue whatsoever on what I'm doing.
The trouble is of the following: I've managed to make a linked list on data that I've given on a GUI-interface. The LL is living fine. It's doing well, it's a happy linked list.
Now what I'd like to do to this list is to duplicate it. I.e. make a new list with a different name, but take all the data from the first list and put it in to the new list. A copy/paste with a new name, if you will.
The problem is that umm... how. Do I need to write a loop in which it takes one object at a time from the list and re-writes it to another list, or is there an easier way to duplicate the list with just one method?
Thanks in advance,
Good Ole' Me
# 1
You can either use Serialization or Clone the Linked list.
# 2
Look at the clone() method: http://java.sun.com/j2se/1.4.2/docs/api/java/util/LinkedList.html#clone().Do I understand correctly that you don't require the elements to be copied?
# 3
I actually need the contents of the list cloned too. So If we say I have a list like "Cars" with name and a price in it like this:
Cars
BMW, 1300.0
Audi, 900.0
Ferrari, 9999.9
And I want to make a list named "Autos", it would also duplicate the values within the list, meaning after this duplication is done "Autos" would look like
Autos
BMW, 1300.0
Audi, 900.0
Ferrari, 9999.9
But I'll look in to the Serialize.
# 4
> I actually need the contents of the list cloned too.
> So If we say I have a list like "Cars" with name and
> a price in it like this:
>
> Cars
> BMW, 1300.0
> Audi, 900.0
> Ferrari, 9999.9
>
> And I want to make a list named "Autos", it would
> also duplicate the values within the list, meaning
> after this duplication is done "Autos" would look
> like
>
> Autos
> BMW, 1300.0
> Audi, 900.0
> Ferrari, 9999.9
You,'d obtain the above with the shallow copy that clone() does. The potential problem is, if after you clone, you lower the price (or what the number is) of the Ferrari to, say 8000.00, then this will be reflected in the copy too, because they both refer to the same Car object.
> But I'll look in to the Serialize.
That should do the deep copy, so that you'll be able to keep separate Ferraris in the two lists after the copy. The way I know serialization is to a file, which may be quite an overhead to pay, but maybe this is not necessary, I don't know.
# 5
hi, its really good to use clone() method
# 6
With this i am attaching one code. I hope this is the solution which you want. Please give feedback to me even if it is not the solution which you have expected
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class CopyLinkedList {
public static void main(String[] args) {
List cars = new LinkedList();
cars.add("BMW, 1300.0");
cars.add("Audi, 900.0");
cars.add("Ferrari, 9999.9");
List auto = new LinkedList(cars);
Iterator iterator = auto.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
# 7
> With this i am attaching one code. I hope this is the
> solution which you want. Please give feedback to me
> even if it is not the solution which you have
> expected
> ...
Probably not: you're not cloning the objects inside the list.
@OP: try this demo:import java.util.List;
import java.util.LinkedList;
class Test {
public Test() throws CloneNotSupportedException {
CarList carList = new CarList();
carList.add(new Car("BMW", 1300.0));
carList.add(new Car("Audi", 900.0));
carList.add(new Car("Ferrari", 9999.9));
CarList copy = (CarList)carList.clone();
copy.cars.get(0).price = 666.0;
System.out.println(carList);
System.out.println(copy);
}
public static void main(String[] args) throws CloneNotSupportedException {
new Test();
}
}
class CarList implements Cloneable {
List<Car> cars;
public CarList() {
this.cars = new LinkedList<Car>();
}
private CarList(List<Car> cars) {
this.cars = cars;
}
public void add(Car car) {
cars.add(car);
}
public Object clone() throws CloneNotSupportedException {
List<Car> copy = new LinkedList<Car>();
for(Car c : cars) {
copy.add((Car)c.clone());
}
return new CarList(copy);
}
public String toString() {
return cars.toString();
}
}
class Car implements Cloneable {
String brand;
double price;
public Car(String brand, double price) {
this.brand = brand;
this.price = price;
}
public Object clone() throws CloneNotSupportedException {
return new Car(brand, price);
}
public String toString() {
return "{brand="+brand+", price="+price+"}";
}
}
# 8
Very simple solution:
To duplicate a LinkedList,
Use :
Collections.copy(List dest, List src)
Javadoc summary:
Copies all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list. The destination list must be at least as long as the source list. If it is longer, the remaining elements in the destination list are unaffected.
# 9
There are still many ways to do it:1. Collections.copy(List dest, List src)2. LinkedList constructor : LinkedList(Collection c) 3. LinkedList newLinkedList = new LinkedList();newLinkedList.addAll(oldLinkedList);
# 10
> There are still many ways to do it:
>
> 1. Collections.copy(List dest, List src)
>
> 2. LinkedList constructor : LinkedList(Collection c)
>
>
> 3. LinkedList newLinkedList = new LinkedList();
>newLinkedList.addAll(oldLinkedList);
All of which will not clone the items in the collection, what the OP wanted to do, if I'm not mistaken.
So, changing item i in list dest, will also cause item i to change in list src (note that item i is just the same reference in both lists).