Bhushan Firake
Bhushan Firake

Reputation: 9448

Merging three arrays with respect to their indices to single array

I have three arrays each having different data. I want to join them as a single array where element at 0 index in each array must be at the same index in the newly created array.

For example:

 arr1[0]="Trailor";
 arr1[1]="Total Recall";

 arr2[0]="Life of Pi";
 arr2[1]="BDRIP";


 arr3[0]="350MB";
 arr3[1]="4.37GB"

Just I want the new array with any dimensions but the elements in above arrays should be accessible through the loops.

I want to print the elements in a table like below:

 <table>
      <thead>
            <tr>
                 <td>Film Title</td>
                 <td>Type</td>
                 <td>Size</td>    
           </tr>
      <thead>
             <tr>
                 <td>Trailor</td>
                 <td>Life of Pi</td>
                 <td>350MB</td>    
           </tr>
           <tr>
                 <td>Total Recall</td>
                 <td>BDRIP</td>
                 <td>4.37GB</td>    
           </tr>


 <table>

Upvotes: 3

Views: 1289

Answers (5)

Prabhu Murthy
Prabhu Murthy

Reputation: 9261

Here is my take on the problem with a LINQ Solution.This will produce a single array ,with elements on the same index joined together.

List<string[]> movies = new List<string[]>() {arr1, arr2, arr3};


var mergedArray = movies.Aggregate((sArrOne, sArrtwo) => 
                               sArrOne.Zip(sArrtwo, (one, two) => one += " "+two)
                        .ToArray());

mergedArray[0] --> Trailor Life of Pi 350MB

Upvotes: 0

dthorpe
dthorpe

Reputation: 36072

Define a class type to hold one element of data from each of the three arrays:

public class Data
{
    public string Item1 { get; set; }
    public string Item2 { get; set; }
    public string Item3 { get; set; }
}

Then construct an array of these objects, populating the fields with values from the original arrays:

var list = new List<Data>();
for (int i = 0; i < arr1.Length; i++)
{
    list.Add(new Data() { Item1 = arr1[i], Item2 = arr2[i], Item3 = arr3[i] };
}

var dataArray = list.ToArray();

This loop assumes that arr1, arr2, and arr3 all have exactly the same length.

You can then access the data under one index, referring to the particular fields of interest:

Console.WriteLine(dataArray[x].Item1, dataArray[x].Item2, dataArray[x].Item3);

Upvotes: 0

SAJ14SAJ
SAJ14SAJ

Reputation: 1708

For simplicity, I didn't use your HTML example, but this code shows you one viable technique:

void Main()
{
    var one = new string[] { "one", "two" };
    var two = new string[] { "apple", "pear" };
    var three = new string[] { "cat", "dog" };

    GroupFormatDemo(one, two, three);

}
void GroupFormatDemo(params string[][] args) 
{
    foreach (var a in args) 
    {
        Console.WriteLine("First={0}, Second={1}", a[0], a[1]);
    }
}

Upvotes: 0

Bal&#225;zs &#201;des
Bal&#225;zs &#201;des

Reputation: 13807

It would be much easier if you would create a class (or struct) for storing data. Much more understandable if you read it later:

class Record
{
    public String Title {get; set;}
    public String Type {get; set;}
    public String Size {get; set;}

    public Record(String title, String type, String size)
    {
        Title = title;
        Type = type;
        Size = size;
    }
}

And then just fill a list of those objects with your data:

List<Record> records = new List<Record>();

for(int i=0; i<elementcount; i++)
    records[i] = new Record(arr1[i], arr2[i], arr3[i]);

Upvotes: 2

McGarnagle
McGarnagle

Reputation: 102723

You could use Concat if you just want to join all the arrays together:

arr1.Concat(arr2).Concat(arr3);

If you want a new two-dimensional array, then simply create and populate it:

var setOfArrays = new string[][] { arr1, arr2, arr3 };

Edit Access them using the same square-bracket syntax setOfArrays[setIndex][itemIndex]...

for (int i=0 ; i<setOfArrays.Length ; i++)
{
    for (int j=0 ; j<setOfArrays[i].Length ; j++)
    {
        Console.WriteLine(setOfArrays[i][j]);
    }
}

Upvotes: 2

Related Questions