首页 > 解决方案 > C#:从数组中删除许多元素

问题描述

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

标签: c#arraysarraylist

解决方案


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

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

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运行效果如下:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述


推荐阅读