Home > OS >  Combining two arrays into one with combined elements
Combining two arrays into one with combined elements

Time:10-31

C# .net windows form framework. I was trying to combine two 1-D arrays into one 1-D arrays, that combines element from first and second into one new element pair in third array (like "a" and "b" into "a b"), that have the length of one with lower element count. I getting error about exceptions but from my understanding it should work.



public partial class Form1 : Form
    {
        string[] chlopcy = new string[] { "Tomek", "Michał", "Grzegorz", "Damian", "Daniel", "Kamil", "Paweł", "Krzysiu", "Wojtek", "Kuba" };
        string[] dziewczyny = new string[] { "Iwona", "Marta", "Ania", "Martyna", "Agnieszka", "Zofia", "Angelika", "Asia", "Joanna", "Ola" };
        string[] pary;

  private void button4_Click(object sender, EventArgs e)
        {
            listBox3.Items.Clear();
            for (int y = 0; y <= (listBox1.Items.Count - 1); y  )
            {
                for (int z = 0; z <= (listBox2.Items.Count - 1); z  )
                {
                    if (z == y)
                    {
                        string[] pary = new string[z] ;
                        pary[z] = listBox1.Items[y]   " i "   listBox2.Items[z];
                        listBox3.Items.Add(pary[z]);
                    }
                    if (z > y)
                    {
                        string[] pary = new string[y];
                        pary[y] = listBox1.Items[y]   " i "   listBox2.Items[y];
                        listBox3.Items.Add(pary[z]);
                    }
                    if (z < y)
                    {
                        string[] pary = new string[z];
                        pary[y] = listBox1.Items[z]   " i "   listBox2.Items[z];
                        listBox3.Items.Add(pary[z]);
                    }
                }
            }
        }

CodePudding user response:

I guess this is what you need.

public partial class Form1 : Form
{
    string[] chlopcy = new string[] { "Tomek", "Michał", "Grzegorz", "Damian", "Daniel", "Kamil", "Paweł", "Krzysiu", "Wojtek", "Kuba" };
    string[] dziewczyny = new string[] { "Iwona", "Marta", "Ania", "Martyna", "Agnieszka", "Zofia", "Angelika", "Asia", "Joanna", "Ola" };
    string[] pary;

    private void button4_Click(object sender, EventArgs e)
    {
        pary = chlopcy.Zip(dziewczyny, (a, b) => $"{a} i {b}").ToArray();

        listBox3.Items.Clear();
        listBox3.Items.AddRange(pary);
    }
}

CodePudding user response:

TL;DR; Go to bottom for your solution. However I recommend to read my explaination below to enhance your development skills.

Lets break down your code so that you understand what is actually going on.

1. the for loop inside another for loop.
To translate that to "human language" it means.

  1. Take item 1 of array 1
  2. Then loop over all items from array 2 while still being in the FIRST cycle of array 1
  3. Then take item 2 from array 1
  4. Then loop over all items from array 2 AGAIN while being in the SECOND cycle of array 1

Your code (if you correct all other issues) will result in: (array1 * array2 = 100 items)

"Tomek i Iwona", "Tomek i Marta", "Tomek i Ania", .... , "Tomek i Ola",
"Michał i Iwona", "Michał i Marta", "Michał i Ania", .... , "Michał i Ola",
...
"Kuba i Iwona", "Kuba i Marta", "Kuba i Ania", .... , "Kuba i Ola",

Your request in your comment explains that you want to go side by side. And take item N from array 1 together with item N from array 2 (where N is the index in your iteration)

This means you want ONE loop without an inner loop.

2. your first if-statement will compare first time if 0 == 0
The first time you enter array 1 and immedately enter array 2, your x and y variables are 0. So that means you create pary as new string[0]. After that you try to assign a value to this array which does simply allow zero items.

Think of what happens the second-tenth time that x and y are equal.

// example y and z = 10
string[] pary = new string[10]; // do you need an array???
pary[10] = listBox1.Items[10]   " i "   listBox2.Items[10];  
// Ah!!! no you don't. Since you only use index=10. So you skip index 0-9 = 10 items in the array
listBox3.Items.Add(pary[10]);

// Would this work? ;)
string value = listBox1.Items[10]   " i "   listBox2.Items[10];
listBox3.Items.Add(value);

3. y <= (listBox1.Items.Count - 1)
Yes it works but you can also do y < listBox1.Items.Count. Makes your code slightly more readable.

Possible solutions
Remark. I did not include listBox 1, 2 and 3 since I assumed that chlopcy, dziewczyny and pary are the listboxes you are refering to.


Short approach

string[] chlopcy = new string[] { "Tomek", "Michał", "Grzegorz", "Damian", "Daniel", "Kamil", "Paweł", "Krzysiu", "Wojtek", "Kuba"};
string[] dziewczyny = new string[] { "Iwona", "Marta", "Ania", "Martyna", "Agnieszka", "Zofia", "Angelika", "Asia", "Joanna", "Ola", "One item more" };

string[] largestArray = chlopcy.Length > dziewczyny.Length ? chlopcy : dziewczyny; // Save largest array
int zipAmount = chlopcy.Length < dziewczyny.Length ? chlopcy.Length : dziewczyny.Length; // save shortest amount

string[] pary = new string[largestArray.Length];

pary = chlopcy.Zip(dziewczyny, (a, b) => $"{a} i {b}").Union(largestArray.Skip(zipAmount).Take(largestArray.Length - zipAmount)).ToArray();

More readable approach

string[] chlopcy = new string[] { "Tomek", "Michał", "Grzegorz", "Damian", "Daniel", "Kamil", "Paweł", "Krzysiu", "Wojtek", "Kuba"};
string[] dziewczyny = new string[] { "Iwona", "Marta", "Ania", "Martyna", "Agnieszka", "Zofia", "Angelika", "Asia", "Joanna", "Ola", "One item more" };

int largestAmout = chlopcy.Length > dziewczyny.Length ? chlopcy.Length : dziewczyny.Length; // Save largest array

string[] pary = new string[largestAmout];

for(int index = 0; index < largestAmout; index  )
{
    if(index < chlopcy.Length && index < dziewczyny.Length)
    {
        // Both arrays has elements at index
        pary[index] = $"{chlopcy[index]} i {dziewczyny[index]}";
    }
    else if(index >= chlopcy.Length)
    {
        // chlopcy has less items than dziewczyny so dziewczyny should add the remaining items
        pary[index] = $"<nothing> i {dziewczyny[index]}";
    }
    else
    {
        // dziewczyny has less items than chlopcy so chlopcy should add the remaining items
        pary[index] = $"{chlopcy[index]} i <nothing>";
    }
}
  •  Tags:  
  • c#
  • Related