c# - C#:从数组中删除许多元素
问题描述
请我有一个填充了元素数量的数组,如果选中特定的 Radiobutton,我想要从该数组中删除特定元素....谢谢
解决方案
我不确定我是否完全理解你的意思,如果你的意思是在检查单选按钮后删除特定的元素信息,我写了一个类似的例子。
如果我误解了你的意思,请指出并告诉我。
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运行效果如下:
推荐阅读
- python - 有没有办法在一行上打印用户定义的对象列表(每个对象都是一个多行字符串)?
- django - 尝试在 Django 中搜索 ManyToMany 字段时出现“相关字段查找无效:包含”
- php - PHP if 语句和重定向
- html - 我的项目仅适用于实时服务器扩展
- python - 使用 python 3.7.3 发送带附件的电子邮件
- visual-c++ - 试图用 std::unique_ptr 调用替换删除指针调用
- paypal - 当国家/地区未知时,PayPal稍后付款消息?
- sql - 如何将值插入到具有复合主键的表中?
- android - 如何在 Jetpack Compose List / Column / Row 中禁用同时点击多个项目(开箱即用的去抖动?)
- r - 仅转置某些列 - 数据格式化