2

i have:

int[] numbers = { 1, 2, 3};
string[] words = { "one", "two", "three" };

and need the output to be

1=one
2=two
3=three

thanks

6
  • 6
    Oh, come on. The straightforward solution is extremely trivial if you know the necessary stuff - and if you don't, you should be learning about it with a book and/or teacher instead of asking us for the solution. Commented Apr 30, 2011 at 11:08
  • 1
    @delnan - We all have to learn somewhere. If we all knew the answers to everything then Stack Overflow would have no reason to exist. Commented Apr 30, 2011 at 11:11
  • @Andrew: Of course. And that includes beginner questions. However, I read this question as "plz gimme teh codez for this simple thing I'm supposed to do but can't be bothered to or don't understand", and if that's the case, giving out a solution (especially one using relatively advanced concepts such as LINQ) doesn't help anyone. Commented Apr 30, 2011 at 11:14
  • @ Andrew: Sorry, but why have you removed your answer? Is there anything wrong ? Commented Apr 30, 2011 at 11:14
  • @Homam - Nothing wrong! @mBotros had the same answer as me but he was faster. :) Commented Apr 30, 2011 at 11:16

3 Answers 3

7

if they are the same size you can use

int[] numbers = { 1, 2, 3};
string[] words = { "one", "two", "three" };

var list = numbers.Zip (words, (n, w) => n + "=" + w);

but note if they differ in size the non match items will be ignored

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

Comments

6

I see that everybody is jumping to Linq or IEnumerable extensions when you don't grasp the basics. It will be like putting a child to college so I suggest you first learn using loops, like the for loop.

for (int i = 0; i < numbers.Length; i++) {
  Console.WriteLine(String.Format("{0}-{1}", numbers[i], words[i]));
}

And Math class basics

int total = Math.Min(numbers.Length, word.Length);
for (int i = 0; i < total; i++) {

1 Comment

Now this is the straightforward solution I was talking about! Agreed, +1.
3

LINQ example:

var query = numbers.Select((n, i) => string.Format("{0}={1}", n, words[i]));

Edit:

In .NET 4.0 you can use Zip (as posted by mBotros) - there's even almost same example as what you're asking for on Zip MSDN doc page.

8 Comments

I think Zip extension is much better than this.
@Homam: I agree, however since Zip is .NET 4 child, I decided to leave this answer as well, .NET 3.5 is still quite popular :)
@ jimmy_keen: I would agree and +1.
Pardon me gents for ignorance, but in this example, why does 'i' in yield a sequence from 0,1,2 etc? I would have used "words[n-1]" but accept that this is less elegant. Just curious. :-)
@Ron Weston: actually, overload I used takes Func<TSource, int, TResult> - but yes, when TSource is also int (as in our case) it's easy to confuse what goes where when you add index (which is int too) into the mix. Rule of thumb: whenever you can, use Zip instead :)
|

Your Answer

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