C# 列表。添加不增加的列表。计数
我正在尝试将元素添加到C# 列表。添加不增加的列表。计数,c#,list,C#,List,我正在尝试将元素添加到列表中,以便可以从队列中一次删除一个元素。以下是向我的列表中添加内容的代码: foreach (String s in q) { if(s != null) { String str = s.Replace("!question", ""); questions.Add(str); this.label1.Text = str; } } q是我用来拆分的数组,而questions是一个列表。标签1应该
列表中
,以便可以从队列中一次删除一个元素。以下是向我的列表中添加内容的代码:
foreach (String s in q)
{
if(s != null)
{
String str = s.Replace("!question", "");
questions.Add(str);
this.label1.Text = str;
}
}
q是我用来拆分的数组,而questions是一个列表
。标签1应该是不相关的(我使用的是Windows窗体)
下面是我用来尝试从列表中删除第二行的代码。它抛出“超出范围”异常:
questions.RemoveAt(1);
Console.WriteLine(questions.Count);
foreach(String s in questions)
{
if (s!= null)
this.label1.Text = s;
}
当它打印到控制台时,计数为1,但如果我打印列表,它会给出正确的列表:
test 1
test 2
test 3
....
我的假设是,我所有的字符串都被添加到第一个索引下的列表中,并带有返回字符,但我不太确定它是如何工作的。谢谢大家
编辑:Input.txt:
test 1 \t
test 2 \t
cellosan asks: !question are we getting secret stream? \t
cellosan asks: !question read chat pls \t
cellosan asks: !question sorry for bothering you too \t
编辑:完整代码
namespace Question_Queue
{
public partial class Form1 : Form
{
public String fullText;
public String[] questionList;
public List<String> questions;
public int i = 0;
public String[] q;
public Form1()
{
InitializeComponent();
questionList = new String[20];
q = new String[30];
questions = new List<String>(20);
}
private void Form1_Load(object sender, EventArgs e)
{
}
//This is the refresh button
private void button1_Click(object sender, EventArgs e)
{
using (FileStream fs = File.Open("C:\\Users\\Lilianne\\Desktop\\questions.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using(StreamReader questionDoc = new StreamReader(fs))
{
//Now we have the stuff in question doc. Let's make an array for all the questions
if (questionDoc.ReadLine() != null)
{
fullText = questionDoc.ReadToEnd();
questionList = fullText.Split('\t');
for (int j = 0; j < questionList.Length; j++)
{
questionList[j] = questionList[j].Replace("!question", "");
questionList[j] = questionList[j].Replace("\t", "");
this.label1.Text = questionList[j];
}
}
else
this.label1.Text = "No questions!";
questionDoc.Close();
}
}
}
private void Answered_Click(object sender, EventArgs e)
{
using (FileStream fs = File.Open("C:\\Users\\Lilianne\\Desktop\\questions.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using(StreamReader sr = new StreamReader(fs))
{
//First, remove the topmost question, which is the second line
Console.WriteLine(questions.Count);
//questions.RemoveAt(2);
foreach(String s in questions)
{
if (s!= null)
this.label1.Text = s;
}
}
}
}
private void ClearQueue_Click(object sender, EventArgs e)
{
File.WriteAllText("C:\\Users\\Lilianne\\Desktop\\questions.txt", "***** EMPTY LINE *****");
}
private void Load_Click(object sender, EventArgs e)
{
}
private void button1_Click_2(object sender, EventArgs e)
{
using (FileStream fs = File.Open("C:\\Users\\Lilianne\\Desktop\\questions.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs))
{
if (sr.ReadLine() != null)
{
fullText = sr.ReadToEnd();
q = fullText.Split('\t');
foreach (String s in q)
{
if(s != null)
{
String str = s.Replace("!question", "");
questions.Add(str);
this.label1.Text = str;
}
}
}
}
}
}
}
}
名称空间问题\u队列
{
公共部分类Form1:Form
{
公共字符串全文;
公共字符串[]问题列表;
公开问题清单;
公共整数i=0;
公共字符串[]q;
公共表格1()
{
初始化组件();
问题列表=新字符串[20];
q=新字符串[30];
问题=新名单(20);
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
//这是刷新按钮
私有无效按钮1\u单击(对象发送者,事件参数e)
{
使用(FileStream fs=File.Open(“C:\\Users\\Lilianne\\Desktop\\questions.txt”,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
{
使用(StreamReader questionDoc=new StreamReader(fs))
{
//现在我们有问题了,博士。让我们为所有的问题做一个数组
if(questionDoc.ReadLine()!=null)
{
全文=questionDoc.ReadToEnd();
questionList=全文.Split('\t');
for(int j=0;j
我假设您有两个按钮,第一个按钮应使用文件内容填充列表,而第二个“应答”按钮应打印计数并从列表中删除一项。(我没有看到打印列表内容的呼叫,而且,由于不知道您在哪里拨打此电话,我无法对此提供太多帮助。)
我可以看到这里发生了几个问题。首先,有两个事件处理程序可以根据文件内容填充列表,即
private void button1_Click(object sender, EventArgs e)
及
如果您在按下应答按钮之前调用button1\u Click,则这就是为什么会出现超出范围的异常。处理程序按钮1\u Click正在填充问题列表,而已回答的\u Click正在从尚未填充任何数据的问题中读取数据(或缺少)
简而言之,检查事件处理程序是否正确映射
我还要指出,呼吁
sr.ReadLine()
返回第一行的内容并将流移动到第二行。我不确定你是否打算跳过第一行直接转到第二行,只是打个电话
sr.ReadToEnd()
应该足够了
此外,由于文件末尾包含一个制表符(\t),并且您正在拆分\t字符上的字符串,因此数组中的最后一条记录将始终为空字符串。这就是为什么
this.label1.Text = s;
不会显示任何文本。它正在被输入空字符串
编辑:而且,这不是完整的c
this.label1.Text = s;