-1

So I've got an array of integer. I want to use a loop and exclude integers that makes the equation true. So that would be like

for (int n = 0; n < first9char.Length; n++ ) {
                                        if (first9char[n] == clickValue) {
                                            first9char[n] = first9char[n + 1];

But then it only changes the value that is equal to not changing whole array. So is there any way to do this?

I want to use it in this loop.

            if (UserSquareMethod.clickedBoxes[0] == -1) {
            MessageBox.Show("You have not selected any box");
        } else {
            int i = 0;
            do {
                if (textButtonArray[clickedBox[i]].Text == "" || clickValue == "") {
                    textButtonArray[clickedBox[i]].Text = clickValue;
                    textButtonArray[clickedBox[i]].Font = new Font(textButtonArray[clickedBox[i]].Font.FontFamily, 14, FontStyle.Bold);
                }

                else
                {
                    textButtonArray[clickedBox[i]].Text += "," + clickValue;
                    textButtonArray[clickedBox[i]].Font = new Font(textButtonArray[clickedBox[i]].Font.FontFamily, 5, FontStyle.Regular);
                    string[] first9char = textButtonArray[clickedBox[i]].Text.Split(new string[] { "," }, StringSplitOptions.None);
                    for (int j = 1; j < first9char.Length; j++)
                    {
                        for (int k = j - 1; k >= 0; k--)
                        {
                            if (first9char[j] == first9char[k])
                            {
                                if (clearNumberClicked == true)
                                {
                                    first9char = Array.FindAll(first9char, x => x != clickValue);
                                    label2.Text = first9char[0];

                                    //int n = 0;

                                    //for (int p = 0; p < first9char.Length; p++)
                                    //{
                                      //  if (first9char[p] != clickValue)
                                      //  {
                                        //    first9char[n] = first9char[p];
                                        //    n++;
                                        //    label2.Text += "," + first9char[n];
                                       // }
                                   // }



                                    //for (int n = 0; n < first9char.Length; n++ ) {
                                        //if (first9char[n] == clickValue) {
                                          //  first9char[n] = first9char[n + 1];
                                          //  for ( int p = 0; p < n; p++) {

                                                //}

                                        //}
                                    //}
                                    MessageBox.Show("Clear the number" + first9char[(first9char.Length - 1)] + "and " + clickValue + " " + first9char.Length);

                                }

                                else {
                                first9char[j] = "";
                                textButtonArray[clickedBox[i]].Text = first9char[0]; 
                                MessageBox.Show("You cannot enter the same number again!"+ first9char[j]+j);
                                for (int m = 1; m < (first9char.Length - 1); m++) {
                                    textButtonArray[clickedBox[i]].Text += ","+ first9char[m]; 

                                }
                            }


                            }
                        }

                    }



                    if (textButtonArray[clickedBox[i]].Text.Length > 9)
                    {

                        textButtonArray[clickedBox[i]].Text = first9char[0] + "," + first9char[1] + "," + first9char[2] + "," + first9char[3] + "," + first9char[4];
                        MessageBox.Show("You cannot enter more than 5 numbers, please clear the box if you want to enter different number." + textButtonArray[clickedBox[i]].Text.Length);
                    }

                }
                i++;

            }
            while (clickedBox[i] != -1);


        }

    }
Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103
  • 2
    I suggest LINQ/IEnumerable (and non-Array types): e.g. `filteredSeq = originalSeq.Where(i => i != clickValue)`. It's just *much* easier to deal with in most cases. (Array inherits IEnumerable, and IEnumerable can be ToArray'ed as well.) –  May 28 '12 at 23:24
  • I don't get what you're trying to do. Right now you're just setting the value in the array to the next value when your condition is true. – Brendan Lesniak May 28 '12 at 23:25
  • possible duplicate of [Remove element of a regular array](http://stackoverflow.com/questions/457453/remove-element-of-a-regular-array) – Jason May 28 '12 at 23:25

1 Answers1

3

I would use LINQ for this:

first9char = first9char.Where(x => x != clickValue).ToArray();

It just means "pick the items that don't match". If you can't use LINQ for some reason, then just keep another counter, and make sure to only loop to n from there on in:

int n = 0;

for(int i = 0; i < first9char.Length; i++) {
    if(first9char[i] != clickValue) {
        first9char[n] = first9char[i];
        n++;
    }
}

Clean and efficient.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • You can replace the `for`-loop version with `first9char = Array.FindAll(first9Char, x => x != clickValue);` http://msdn.microsoft.com/en-us/library/1kkxfxdd.aspx – LukeH May 28 '12 at 23:44
  • @LukeH: Good point, thanks for commenting. You should make that your own answer :) – Ry- May 28 '12 at 23:45
  • LINQ somehow did not work. For both of your solution it gives Index was outside the bounds of the array error. @ minitech your loop somehow discards the first value of the array. – Sarp Kaya May 28 '12 at 23:46
  • 1
    `Index was outside the bounds of the array error` - Show the code you used to get this error, you shouldn't get this if using the LINQ solution. –  May 28 '12 at 23:59
  • @SarpKaya: (See Will Hughes' comment above.) – Ry- May 29 '12 at 00:01