-1

I have two arrays: Lines[] and Blocks[] I want to join them in a string parameters[][] and reach them easily.

Parameters[0][0] = Lines[0]Blocks[0] //or it's variations didn't work.

How can i do that?

( If it's possible another easy way, please share it with me)

This is how I've visualized the result:

image for explanation http://img38.imageshack.us/img38/8662/82103454.JPG

Servy
  • 202,030
  • 26
  • 332
  • 449
ithnegique
  • 139
  • 1
  • 1
  • 9
  • 2
    What have you tried so far? What problems have you run into in your attempts? Do you want to copy all of the information over, or create an object that simply redirects back to the info in the original two arrays? – Servy May 23 '13 at 20:26
  • I tried them in some loops, for second arrays length in a for first arrays lenght and assign them to another jagged array, as container. I wonder if a better way to handle the all information as a datatable or sth like that, like matrices. all members must be some string or integers. – ithnegique May 23 '13 at 20:30
  • Please edit the question accordingly. Show the actual code that you tried. If it worked just fine, then describe how you expect it to be improved. – Servy May 23 '13 at 20:31
  • You said that you had already coded a solution. Please provide that code. – Servy May 23 '13 at 21:12

3 Answers3

3

Something like this?

var linesAndBlocks =  Lines.Zip(Blocks, (l, b) => new Tuple<string, string>(l, b))
                           .ToList();

Then you can use it as

Console.WriteLine(linesAndBlocks[0].Item1 + " " + linesAndBlocks[0].Item2);
I4V
  • 34,891
  • 6
  • 67
  • 79
  • How is this like `Parameters[0][0]`? – JDB May 23 '13 at 20:43
  • @Cyborgx37 read the last sentense `If it's possible another easy way, please share it with me` – I4V May 23 '13 at 20:44
  • It would seem that the OP is looking for `Parameters[LineIndex][BlockIndex]`. Your solution is `Parameters[LineIndex * BlockIndex]`. I like it better, but it still doesn't quite match what the OP appears to be asking for. – JDB May 23 '13 at 20:48
  • @Cyborgx37 not even close to `Parameters[LineIndex * BlockIndex]` – I4V May 23 '13 at 20:49
  • Sorry - you're right. I got `Zip()` mixed up. This gives them `Parameters[0][0]`, but what about `Parameters[0][1]`? Also, the title contains "jagged array", suggesting the arrays may not necessarily be the same length (so the last few items in the longer array could be lost). – JDB May 23 '13 at 20:51
  • @Cyborgx37 You are right but the question is vague, I don't think( this is my assumption) that OP does want to use it that way. – I4V May 23 '13 at 20:53
  • See the OP's edit to the question; he's not looking to `Zip` the two arrays, he's looking for the Cartesian Product. (But with the results formatted in a 2D structure rather than having them flattened; if he wanted them flattened it would just be a call to `SelectMany`.) – Servy May 23 '13 at 21:19
  • @servy, Is this a reason to downvote, since I answered long before this edit. And I think you should have waited people honour your answer – I4V May 23 '13 at 21:23
  • @I4V Well, it's not a correct answer to the question, so yes, I would think so. Note that I was reasonably confident before the edit as to what he wanted. My answer was not posted after his edit. You shouldn't answer vague or unclear questions if you're not prepared to remove an answer if it is determined that it's incorrect. Why would you want to keep this answer, knowing full well that it's entirely wrong? – Servy May 23 '13 at 21:26
  • @Servy `You shouldn't answer vague or unclear questions if you're not prepared to remove an answer if it is determined that it's incorrect.` You can not say what should I answer or not. I am happy with it. And don't forget SO is full of answers `If I understand you correctly...` – I4V May 23 '13 at 21:29
  • @I4V You're happy leaving an answer even though you're fully aware that it's entirely wrong and is not actually answering the question that has been asked? Why do you want to leave it? I'm not saying that you shouldn't have answered, I'm saying that you should delete an answer if you are able to determine that you're wrong. It's only if you're unwilling to be wrong that you shouldn't try to answer such a question. – Servy May 23 '13 at 21:31
  • @Servy the problem is not to be wrong since, as i said, it depends on only an assumption. But i don't want to remove it because someone commented on it roughly. – I4V May 23 '13 at 21:34
  • I don't think answering a question is hardly wrong even if the answer wrong. If a page is not full of same answers, that means some are the right choice and some are not, so there is nothing wrong answering a question. It was a mistake of mine to ask question not clearly, but thank you both, I4V and Servy. – ithnegique May 23 '13 at 21:42
  • @bardakbaski I never said it was wrong to answer, I said the answer is wrong. It doesn't provide the solution that is requested. While that's understandable when a question is unclear, when a question is clarified (and after the edit it is quite clear) incorrect answers should be deleted, not left to lead future readers down an incorrect path. – Servy May 23 '13 at 22:56
  • 2
    @Servy Just forget about whether this answer is correct ot not? SO people decides which answer is good/bad/correct/wrong by their upvotes/downvotes/accept etc. I don't think you have the right to ask someone to delete his answer. it is rude. And I won't. – I4V May 23 '13 at 23:25
  • @I4V So you seriously don't care that your answer is wrong? You know it's wrong, you don't care, you refuse to fix it, and you're offended when someone else asks you to fix **the answer that you know is wrong**? How am *I* the bad guy here? If you don't want to delete the answer then simply *make it correct and answer the question*, assuming you're capable. – Servy May 24 '13 at 02:35
0

You might try something like:

string[] lines  = { "a" , "b" , "c" , "d" , } ;
string[] blocks = { "x" , "y" , "z" ,       } ;
string[][] parameters = new string[2][] ;

parameters[0] = lines ;
parameters[1] = blocks ;

Or something like

string[] lines  = { "a" , "b" , "c" , "d" , } ;
string[] blocks = { "x" , "y" , "z" ,       } ;
string[][] parameters = { lines , blocks , } ;

See the Arrays Tutorial on MSDN @ http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx

Also this question might help: All possible C# array initialization syntaxes

Or...since it's unclear what you want exactly...something like this:

static Tuple<TRow,TCol>[,] CreateMatrix<TRow,TCol>( TRow[] rows , TCol[] cols )
{
  Tuple<TRow,TCol>[,] matrix = new Tuple<TRow,TCol>[rows.Length,cols.Length];

  for ( int r = 0 ; r < rows.Length ; ++r )
  {
    for ( int c = 0 ; c < cols.Length ; ++c )
    {
      Tuple<TRow,TCol> cell = new Tuple<TRow,TCol>( rows[r] , cols[c] ) ;
      matrix[r,c] = cell ;
    }
  }

  return matrix ;
}
Community
  • 1
  • 1
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • Note that the type of the two arrays are not the same; your second answer will need to have two generic arguments to account for that. – Servy May 23 '13 at 21:32
  • The OP asked for a specific answer: how to combine 2 1-dimensional arrays into a 2-dimesional jagged array. I gave him 2 perfectly acceptable answers to his question. Since he's now changed his question completely, I've given him a third option. – Nicholas Carey May 23 '13 at 21:34
  • The OP didn't change his question completely; his question is exactly the same as it always was. You misunderstood the question until he clarified it... – Servy May 23 '13 at 22:53
-2

If you wanted to use LINQ you can do this with two nested Select calls:

var Parameters = Lines.Select(line => 
    Blocks.Select(block => new Parameter(line, block))
    .ToArray())
.ToArray();
Servy
  • 202,030
  • 26
  • 332
  • 449