P
P
Paul2016-08-24 09:15:14
C++ / C#
Paul, 2016-08-24 09:15:14

How to clone sublists for a list of lists in C#?

For example, there is this code:

LinkedList<LinkedList<String>> l = new LinkedList<LinkedList
<String>>();
LinkedList<String> ll = new LinkedList<String>();
ll.AddLast("Masha");
ll.AddLast("Masha");
ll.AddLast("Masha");
ll.AddLast("Masha");
LinkedList<String> lll = new LinkedList<String>();
lll.AddLast("Sveta");
lll.AddLast("Sveta");
lll.AddLast("Sveta");
lll.AddLast("Sveta");
l.AddLast(ll);
l.AddLast(lll);
ll.AddFirst("WARNING!!!"); \\ Как сделать так, чтобы ll в l при этом не изменялся?
foreach (LinkedList<string> i in l)
{
    foreach(string s in i)
    {
        System.Console.Write(s+ ' ');    
    }
}

WARNING!!! Masha Masha Masha Masha Sveta Sveta Sveta Sveta

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ananiev, 2016-08-24
@PashaKrizskiy

Write your own class that will implement the desired logic for adding elements

D
Dmitry Kovalsky, 2016-08-24
@dmitryKovalskiy

What are you actually trying to implement? Wood? Or what in general? Your implementation now looks at least strange, and it's not obvious what you're trying to implement. Without a task to say how to do it right is difficult.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question