簡體   English   中英

在 C# 中按索引合並/復制數組的有效方法

[英]An efficient way to merge / copy array index-wise in C#

我在 WPF 項目中工作,我需要使用計時器將圖像顯示為廣告。 圖像以下列方式以數組形式給出。

array1 = { img1, img2, img3 }
array2 = { img4, img5, img6, img7 }
array3 = { img8, img9 }

我需要按以下方式顯示圖像(按索引合並/復制數組):

finalarray = { img1, img4, img8, img2, img5, img9, img3, img6, img7}

數組的大小可以是 0 到 20。我嘗試使用 for/while 循環,結果代碼變得不必要地長了。 我需要一種簡單有效的方法來實現這一點。

任何建議在這里表示贊賞。 提前致謝。

我不知道您是否提供了正確的解決方案。
但請檢查:-

        string[] array1 = new string[] { "img1", "img2", "img3" };
        string[] array2 = new string[] { "img4", "img5", "img6", "img7" };
        string[] array3 = new string[] { "img8", "img9" };
        List<string> arrayList = new List<string>();
        for (int i = 0; i < 20; i++)
        {
            if (array1.Length > i)
                arrayList.Add(array1[i]);
            if (array2.Length > i)
                arrayList.Add(array2[i]);
            if (array3.Length > i)
                arrayList.Add(array3[i]);
        }

//您現在可以打印 arrayList。
//它將返回:- { img1, img4, img8, img2, img5, img9, img3, img6, img7}

如果 arrays 的數量不固定,您可以再創建一個 class,此解決方案應該適合您:

class Program
    {
        public class Image
        {
            public string Name { get; set; }
            public Image(string name)
            {
                Name = name;
            }
        }

        public class ImageWithIndex : Image
        {
            public ImageWithIndex (string name, int index) : base(name)
            {
                Index = index;
            }

            public int Index { get; set; }
        }


        static void Main(string[] args)
        {            
            var images = new List<List<Image>>
            {
                new List<Image> { new Image("1"), new Image("2"), new Image("3") },
                new List<Image> {  new Image("4"), new Image("5"), new Image("6"), new Image("7") },
                new List<Image> {  new Image("8"), new Image("9") }
            };

            var sortedList = images
                .SelectMany(innerList => innerList.Select((image, index) => new ImageWithIndex(image.Name, index)))
                .OrderBy(i => i.Index)
                .Select(i => i as Image);

        }




    }

我喜歡創建一個 arrays 數組(鋸齒狀數組)並循環遍歷直到沒有更多內容可添加到結果圖像列表的想法。 下面的代碼適用於任意數量的 arrays 以及任意數量的圖像。

    static void Main(string[] args)
    {

        var images = GetImages(
            new Image[] { img1, img2, img3 },
            new Image[] { img4, img5, img6, img7 },
            new Image[] { img8, img9 });

    }

    static Image[] GetImages(params Image[][] ads)
    {
        var list = new List<Image>();
        int index = 0;
        bool done;
        do
        {
            done = true;
            for (int i = 0; i < ads.Length; i++)
            {
                if (index<ads[i].Length)
                {
                    list.Add(ads[i][index]);
                    done = false;
                }
            }
            index++;
        } while (!done);
        return list.ToArray();
    }

在使用字符串而不是圖像進行測試時顯示結果。

調試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM