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?
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.
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].
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[,]
)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With