1

i am trying to copy parts of an array to a new array and searched for a solution, but al those solutions were allot of coding or were not exactly what i was looking for. it might be that i searched wrong on google, but sorry if the answer to this is to easy to find, i couldnt find it.

i have array A

string[] A = {"A", "B", "C", "D", "E", "F"};

i tried to copy parts of it to array B using

string[] B = new string[2]; Array.Copy(A, 0, B, 0, 2)

but this just gives a fixed part of array A back so array B consists of "A" and "B".

is there also a way to give array B values 0, 1, 4, 5. so it gives me "A" "B" "E" and "F"

also sorry for bad english it is not my native language, and i tried to search on google for :

allot of the results of my first searches got me to pages about how to get an 2D array but that is not what i need. "How to copy 2 parts of an array to a new array" what lead me to array.copy tag after that i tried to look up "array.copy manual" but that only lead me to beleave it cant be done with array.copy so does anyone know how to do this in a compact code?

again. sorry if this answer is to obvious.

1

3 Answers 3

3

is there also a way to give array B values 0, 1, 4, 5. so it gives me "A" "B" "E" and "F"

No, because B is an array of strings. However, you can start with a separate array of indexes, and copy from A based on them:

int[] indexes = new[] {0, 1, 4, 5};
string[] B = indexes.Select(i => A[i]).ToArray();
Sign up to request clarification or add additional context in comments.

3 Comments

nice solution. only could be wrong on bound checking. int[] = new[] { 0,1, 15 }; might be: string[] B = indexes.Where(i => i < A.Length).Select(i => A[i]).ToArray();
@JeroenvanLangen OP's the one constructing the array of indexes, so observing the proper bounds is on him.
yours is more profesional it works i found a solution myself just now, but ill use yours and put mine in comment under it for refference purposes thank you.
1

One way could be:

var indices = new[] { 0, 1, 4, 5 };
var copy = A.Where((s, i) => indices.Contains(i)).ToArray();

3 Comments

It's better to use HashSet of indexes, instead of integer array in this case, since Contains is not too fast on an array (full search each time)
A hashset is not usefull when using 5 items, But I would upgrade it, when I didn't know the numbers.
Sure, 5 items does not matter. Just thought of a more "generic" case :)
0

uhm i just went booging arround with my code and this works i guess:

string[] A = {"A", "B", "C", "D", "E", "F"}; string[] B = new string[4]; Array.Copy(A, 0, B, 0, 2) Array.Copy(A, 4, B, 2, 2)

it gives array B values A B E and F

2 Comments

I can hardly believe you can put 4 values in an array of length 3 :)
oh ye sorry hihi it works it had to be 4 ill edit it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.