简体   繁体   English

将数组列出为单独的数组

[英]List with arrays into seperate arrays

I have a constructor which uses a List parameter. 我有一个使用List参数的构造函数。 Inside that list I have three items which are 3 integer arrays. 在该列表中,我有3个项目,它们是3个整数数组。

public HistogramLogic(List<Tuple<int[], int[], int[]>> separateRGB)

Now I want to initialize three new integer arrays with the arrays inside that list. 现在,我想用该列表中的数组初始化三个新的整数数组。 But when I try this code, the index of my new arrays stay 0. 但是,当我尝试这段代码时,新数组的索引保持为0。

for (int i = 0; i < histogramXValues.Length; i++)
{
     rArray = separateRGB.Select(obj => obj.Item1[i]).ToArray();
     gArray = separateRGB.Select(obj => obj.Item2[i]).ToArray();
     bArray = separateRGB.Select(obj => obj.Item3[i]).ToArray();
}

Anyone got any suggestions on how to fix this? 有人对如何解决此问题有任何建议吗?

你可以从touple获得物品

rArray = separateRGB.Select(obj => obj.Item1);

Bear in mind, if you have a list of N tuples, you start with 3 x N arrays. 请记住,如果您有N个元组的列表,则以3 x N个数组开始。 Sounds like you want them combined into 3 arrays, each containing all of the elements throughout the list. 听起来好像您希望它们组合成3个数组,每个数组包含整个列表中的所有元素。 Which you can do with SelectMany . 您可以使用SelectMany

rArray = separateRGB.SelectMany(obj => obj.Item1).ToArray();
gArray = separateRGB.SelectMany(obj => obj.Item2).ToArray();
bArray = separateRGB.SelectMany(obj => obj.Item3).ToArray();

Full example: 完整示例:

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    static public void HistogramLogic(List<Tuple<int[], int[], int[]>> separateRGB)
    {
        var rArray = separateRGB.SelectMany(obj => obj.Item1).ToArray();
        var gArray = separateRGB.SelectMany(obj => obj.Item2).ToArray();
        var bArray = separateRGB.SelectMany(obj => obj.Item3).ToArray();

        Console.WriteLine("rArray = {{{0}}}", string.Join(",", rArray));
        Console.WriteLine("gArray = {{{0}}}", string.Join(",", gArray));
        Console.WriteLine("bArray = {{{0}}}", string.Join(",", bArray));
    }

    public static void Main()
    {
        var mockData = new List<Tuple<int[], int[], int[]>>
        {
            Tuple.Create(new[] {11,12,13}, new[] {14,15,16}, new[] {17,18,19}),
            Tuple.Create(new[] {21,22,23}, new[] {24,25,26}, new[] {27,28,29}),
            Tuple.Create(new[] {31,32,33}, new[] {34,35,36}, new[] {37,38,39})
        };

        HistogramLogic(mockData);
    }
}

Output: 输出:

rArray = {11,12,13,21,22,23,31,32,33}
gArray = {14,15,16,24,25,26,34,35,36}
bArray = {17,18,19,27,28,29,37,38,39}

Click here for code on DotNetFiddle 单击此处获取DotNetFiddle上的代码

Like they sad in comments you did reassign local member in each loop. 就像他们在评论中感到悲伤一样,您确实在每个循环中都重新分配了本地成员。 You could use something like this. 您可以使用类似这样的东西。

public HistogramLogic(List<Tuple<int[], int[], int[]>> separateRGB)
{
    List<int> rList = new List<int>();
    List<int> gList = new List<int>();
    List<int> bList = new List<int>();

    separateRGB.ForEach((Tuple<int[], int[], int[]> tpl) =>
    {
        rList.AddRange(tpl.Item1);
        gList.AddRange(tpl.Item1);
        bList.AddRange(tpl.Item1);

    });

    rArray = rList.ToArray();
    gArray = gList.ToArray();
    bArray = bList.ToArray();

}

If you wish to not use temp List object you should know final count of elements in tuple, create local array member to desired size. 如果您不想使用临时列表对象,则应该知道元组中元素的最终计数,请创建所需大小的本地数组成员。 and fill it. 并填充它。 List are more suitable for adding and expanding elements. List更适合添加和扩展元素。 Maybe you could use one Linq Statement but if I understand goal is to get one int[] array per color. 也许您可以使用一个Linq语句,但是如果我理解目标是每种颜色获得一个int []数组。 If you take 如果你拿

separateRGB.AsQueryable().Select(m => m.Item1).ToArray();

as a result you get int[][] result instead of simple int array; 结果,您得到的是int [] []结果,而不是简单的int数组;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM