2

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?

Community
  • 1
  • 1
Nozim
  • 587
  • 3
  • 14
  • 34
  • Just to clarify; do you want array elements 2 to 5 to get assigned, or element with *values* between 2 and 5? – Fredrik Mörk Dec 10 '10 at 21:08
  • I want the elements INDEXES 2 and 5 were set to 8 not the VALUES of 2 and 5. – Nozim Dec 10 '10 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. – jcolebrand Dec 10 '10 at 21:39

7 Answers7

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.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
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.

Community
  • 1
  • 1
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
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;
}
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
0

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

Femaref
  • 60,705
  • 7
  • 138
  • 176
0
for (i=0;i<100;i++){
if(i=0)
{array[i]=2}
else{array[i]=(array[i-1]+3)}
}
m.qayyum
  • 409
  • 2
  • 9
  • 24
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;
}
phillip
  • 2,618
  • 19
  • 22
  • ArraySegment? I've never heard about it before. Anyway it's also very cute way. Thanks :) – Nozim Dec 10 '10 at 21:34
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;
}
Cory
  • 1
  • It's nice for small arrays but what would you do with big arrays? It will fill your stack up ;) – Nozim Dec 11 '10 at 05:47