Reputation: 13600
If I have: linkedlist1= 1,2,3,4;
and linkedlist2= 5,6,7;
Am I able to attach linkedlist2 to the end of linkedlist1 in such a way if I invoke: linkedlist2.set(0,9999)
it changes to linkedlist2 = [999,6,7]
and linkedlist1
becomes [1,2,3,4,9999,7,8];
?
Is that possible ? Or I do need another structure ?
The following code didn't work:
List<Double> l1 = new LinkedList<Double>(Arrays.asList(1.0,2.0));
List<Double> l2 = new LinkedList<Double>(Arrays.asList(3.0,4.0));
l1.addAll(l2);
System.out.println(l1);
l2.set(0, 9.0);
System.out.println(l1);
OUTPUT:
[1.0, 2.0, 3.0, 4.0]
[1.0, 2.0, 3.0, 4.0]
Upvotes: 0
Views: 5008
Reputation: 8932
This problem seems to be a nice application for the subList(int, int) method:
List<Double> l1 = new LinkedList<Double>(Arrays.asList(1.0, 2.0, 3.0, 4.0));
List<Double> l2 = l1.subList(2, 4);
Alas, the JavaDoc of subList states:
The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
So if you want to use a method like .add
on l1, then you may be out of luck. In fact, for all fail-fast list classes derived from AbstractList
(such as as LinkedList
), subList should throw a ConcurrentModificationException
after the parent list of a subList was structurally modified.
However, if you stay away from structural modifications in the parent list (or if you re-create the sub-list after such modifications), then everything will work as expected.
Upvotes: 0
Reputation: 199215
You can't but as an alternative you can have a list of list, but I don't know if that would work as you need:
import java.util.*;
import static java.lang.System.out;
class ListOfLists {
public static void main( String ... args ) {
List<Integer> a = new LinkedList<Integer>(Arrays.asList(1,2,3,4));
List<Integer> b = new LinkedList<Integer>(Arrays.asList(5,6,7));
List<List> list = new LinkedList<List>();
list.add( a );
list.add( b );
out.println("Before : " + list );
b.set( 0, 999 );
out.println("After : " + list );
}
}
Before : [[1, 2, 3, 4], [5, 6, 7]]
After : [[1, 2, 3, 4], [999, 6, 7]]
Upvotes: 1
Reputation: 4122
The easiest way to achieve this is to have a proper class for your items and have instances of this class MyInt
link to each other in stead of relying on LinkedList, i.e. add a MyInt next;
field to the custom class that holds your integers.
Then link them as described.
A change to one instance can then be done without even touching the list structure and is reflected accordingly when iterating over this list through the next
references.
Upvotes: 0
Reputation: 3079
To answer the question about updating one list and seeing the updates reflected in the other, the answer is it depends.
If you update an object in the first list you will see a change. but if you replace the object you will not see the change. Calling .set() on the first list is replacing the object in the list. Unfortunately, it is not possible to update the value of a primitive (or its class representation, in this case Float) without replacing it.
To append two lists, assuming you are using java.util.LinkedList, you can call addAll
Upvotes: 0
Reputation: 23629
addAll()
just takes the values from the second linked list. Changes to the indexes and set values in the second list will not affect the first. (Note: changes to the objects themselves will stay because they both point to the same object).
You can make a List of Lists if that is what you are looking for.
Upvotes: 0
Reputation: 21628
The standard LinkedList classes provided with Java lack this capability.
As Donal Boyle posts you can add the contents of one list to another but this doesn't maintain the linkage as your describe.
Upvotes: 5