3

How can I set one value to several elements of the array in C#?

Example:

I have an array initialized as follows:

int[] array=new int[]{2,3,5,3,7,2,9}

and I want to set the values between the 2-nd and 5-th indices to 8. How it can be done?

3
  • Just to clarify; do you want array elements 2 to 5 to get assigned, or element with values between 2 and 5? Commented Dec 10, 2010 at 21:08
  • I want the elements INDEXES 2 and 5 were set to 8 not the VALUES of 2 and 5. Commented Dec 10, 2010 at 21:17
  • Not looping when a loop is clearly the most efficient way to do what you want seems to me to be bad form. I smell bad code. We try to promote good code as a matter of course on SO. I contributed to the answer and gave you some ways to do bad code, but I don't have to like it. I'm just voicing this for the next person who thinks "oh yes, I'll avoid iterating explictly in my code" because I don't want to have to support that code later. Commented Dec 10, 2010 at 21:39

7 Answers 7

5

Well, if you wanted to get cute, you could create another array that has the value repeated N times, and Copy that to the array:

int[] a = new int[]{2,3,5,3,7,2,9}
int[] replacement = new int[]{8, 8, 8, 8};
Array.Copy(replacement, 0, a, 1, 4);

There's no explicit loop there. But you can bet that there's an implicit loop.

And, if you really wanted to get cute, you could use LINQ to create the replacement array.

Still, this is all academic. As somebody else pointed out, there's no non-looping way to do what you're asking--just highly obfuscated ways that attempt to hide the fact that a loop is taking place.

Sign up to request clarification or add additional context in comments.

Comments

1

Just put it in a loop (assuming you want to set the second through fifth elements):

for (int i = 1; i < 5; i++)
{
    array[i] = 8;
}

3 Comments

Linq is not magic; it will iterate over the array under the hood.
LINQ isn't magic? Are you going to tell me Santa isn't real too? :)
I know LINQ isn't magic. I just tried to find out what I could implement with it avoiding redundancy in code :)
1

There are a lot of options in this post: Array slices in C#

There's no magic to set all the options to a value. You're going to have to iterate. But you could always do something like .Take(2) then loop to add 8 for three places, then .Skip(3).Take(rest) or something. Just depends how large the array is.

For large arrays this might help, but it's going to be a lot uglier code if you're only working with Ints or the like. I would just iterate.

Comments

0

Iterate over it and set the appropiate values. There is no shortcut for that.

Comments

0
for (i=0;i<100;i++){
if(i=0)
{array[i]=2}
else{array[i]=(array[i-1]+3)}
}

1 Comment

No, it looks very ugly, I still don't understand what it does :(
0

I thought I'd give an alternate approach which is pretty cool.

int[] array = new int[] { 2,3,5,3,7,2,9 };
var segment = new ArraySegment<int>( array, 2, 5 );
for (int i = segment.Offset; i <= segment.Count; i++)
{
    segment.Array[i] = 8;
}

1 Comment

ArraySegment? I've never heard about it before. Anyway it's also very cute way. Thanks :)
0

I choose recursion.

int[] array = new int[] { 2, 3, 5, 3, 7, 2, 9 };
public void caller()
{
    Array8(2, 5);
}

public void Array8 (int start, int end)
{
    if (start <= end)
        Array8(start, --end);
    array[end] = 8;
}

1 Comment

It's nice for small arrays but what would you do with big arrays? It will fill your stack up ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.