【问题标题】:C#: Remove Many Elements from Array [closed]C#:从数组中删除许多元素 [关闭]
【发布时间】:2021-10-28 10:26:18
【问题描述】:

请我有一个填充了元素数量的数组,如果选中特定的 Radiobutton,我想要从该数组中删除特定元素....谢谢

【问题讨论】:

  • LINQ 除了他们
  • 请分享一些您已经尝试编写的代码。
  • @Qwertyluk ,我不知道这个过程的代码
  • 你能输入代码吗>>>>> @CaiusJard

标签: c# arrays arraylist


【解决方案1】:

我不确定我是否完全理解你的意思,如果你的意思是在检查单选按钮后删除特定元素信息,我写了一个类似的例子。

如果我误解了你的意思,请指出并告诉我。

xaml的代码如下:

<Grid>
        <TextBlock x:Name="textbox_1" HorizontalAlignment="Left" Margin="158,59,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="92" Width="447" Background="#FFC9AD62"/>
        <Button x:Name="button_1" Content="Original array" HorizontalAlignment="Left" Margin="158,173,0,0" VerticalAlignment="Top" Width="75" Click="Button_1_Click"/>
        <RadioButton x:Name="RadioButton_1" Content="Delete less than 10" HorizontalAlignment="Left" Margin="273,176,0,0" VerticalAlignment="Top" Checked="RadioButton_1_Checked"/>
        <RadioButton x:Name="RarioButton_2" Content="Delete more than 80" HorizontalAlignment="Left" Margin="414,176,0,0" VerticalAlignment="Top" Checked="RarioButton_2_Checked"/>
    </Grid>

xaml.cs的代码如下:

using System;
using System.Linq;
using System.Windows;

namespace demo830 {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }

        private void Button_1_Click(object sender, RoutedEventArgs e) {
            //Generate 100 random integers between 1 and 99 and fill the array
            //Array type is String array, size is 100
            string[] a = new string[100];
            Random r = new Random();
            for (int i = 0; i < a.Length; i++) {
                a[i] = r.Next(1, 100).ToString();
            }
            //Invoke display function
            ViewArry(a);
        }

        //Display function
        private void ViewArry(string[] b) {
            this.textbox_1.Text="";//Initialize text before calling the output function
            foreach (var i in b) {
                this.textbox_1.Text = this.textbox_1.Text + i + " " ; 
            }
        }

        private void RadioButton_1_Checked(object sender, RoutedEventArgs e) {
            string[] tmp = textbox_1.Text.Split(' ');//Here is "" as the demarcation
            //Delete the element at the specified position of tmp
            string[] c = tmp.Where(s => tmp.ToList().IndexOf(s) != tmp.Length-1).ToArray();

            //Delete values less than 10
            for (var i = c.Length - 1; i > -1; i--) {
                if (c[i] != null) { 
                var item = Convert.ToInt32(c[i]);//Convert string to int
                    if (item < 10) {
                        //Delete the element at the specified position of c
                        string[] d = c.Where(s => c.ToList().IndexOf(s) != i).ToArray();
                    c = d;
                    }      
                }    
            }
            ViewArry(c);
        }

        private void RarioButton_2_Checked(object sender, RoutedEventArgs e) {
            string[] tmp = textbox_1.Text.Split(' ');//Here is "" as the demarcation
            //Delete more than one blank data
            string[] c = tmp.Where(s => tmp.ToList().IndexOf(s) != tmp.Length - 1).ToArray();

            //Delete values greater than 80
            for (var i = c.Length - 1; i > -1; i--) {
                if (c[i] != null) {
                    var item = Convert.ToInt32(c[i]);
                    if (item > 80) {
                        string[] d = c.Where(s => c.ToList().IndexOf(s) != i).ToArray();
                        c = d;
                    }
                }
            }
            //invoke the output function
            ViewArry(c);
        }
    }
}

我的demo运行效果如下:

【讨论】:

    【解决方案2】:

    请找到下面提到的代码

            List<string> lstString = new List<string>
            {
                new string("ABC"),
                new string("BCD"),
                new string("CDE"),
                new string("DEF"),
            };
    
            List<string> lstString1 = new List<string>
            {
                new string("AAA"),
                new string("BCD"),
                new string("ACD"),
                new string("DEF"),
            };
    
            lstString.RemoveAll(x=> lstString1.Contains(x));
    

    【讨论】:

    • OP 询问数组
    猜你喜欢
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 2012-04-27
    相关资源
    最近更新 更多