Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying a portion of a list to a new list

Tags:

string

c#

list

Hey all. Is there a way to copy only a portion of a single (or better yet, a two) dimensional list of strings into a new temporary list of strings?

like image 579
bitcycle Avatar asked May 29 '09 15:05

bitcycle


People also ask

How do you copy one list into another list?

Another approach to copying elements is using the addAll method: List<Integer> copy = new ArrayList<>(); copy. addAll(list); It's important to keep in mind whenever using this method that, as with the constructor, the contents of both lists will reference the same objects.

How do you copy part of an array to another Python?

ALGORITHM: STEP 1: Declare and initialize an array. STEP 2: Declare another array of the same size as of the first one. STEP 3: Loop through the first array from 0 to length of the array and copy an element from the first array to the second array that is arr1[i] = arr2[i].


1 Answers

Even though LINQ does make this easy and more general than just lists (using Skip and Take), List<T> has the GetRange method which makes it a breeze:

List<string> newList = oldList.GetRange(index, count);

(Where index is the index of the first element to copy, and count is how many items to copy.)

When you say "two dimensional list of strings" - do you mean an array? If so, do you mean a jagged array (string[][]) or a rectangular array (string[,])?

like image 66
Jon Skeet Avatar answered Oct 01 '22 23:10

Jon Skeet