简体   繁体   中英

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

I'm working in a WPF project where I need to display images as advertisement using timer. The images are given in array in the following way.

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

I need to display the images in the following way (merge/copy the array index-wise):

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

The size of array can be 0 to 20. I tried using for/while loops and the code ended up being unnecessarily longer. I need a simple and efficient way to achieve this.

Any advise here is appreciated. Thanks in advance.

I don't know you that I am providing the correct solution.
But please check:-

        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]);
        }

//You can now print arrayList.
//It would return:- { img1, img4, img8, img2, img5, img9, img3, img6, img7}

If the number of arrays is not fixed and you can create one more class, This solution should work for you:

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);

        }




    }

I like the idea of creating an array of arrays (jagged array) and looping through until there is nothing more to add to the resulting list of images. The code below works for any number of arrays, as well as any number of images.

    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();
    }

with results while testing with strings instead of images.

调试

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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