列表.添加不增加的List.Count
本文关键字:List Count 增加 添加 列表 | 更新日期: 2023-09-27 18:15:58
我正试图将元素添加到List<String>
,以便我可以一次从队列中删除它们。下面是添加东西到我的列表的代码:
foreach (String s in q)
{
if(s != null)
{
String str = s.Replace("!question", "");
questions.Add(str);
this.label1.Text = str;
}
}
q是我用来分割的数组,questions是一个List<String>
。Label1应该是不相关的(我使用Windows窗体)
这是我用来尝试从列表中删除第二行的代码。它抛出一个'out of range'异常:
questions.RemoveAt(1);
Console.WriteLine(questions.Count);
foreach(String s in questions)
{
if (s!= null)
this.label1.Text = s;
}
当它打印到控制台时,Count是1,但如果我打印列表,它会给我正确的列表:
test 1
test 2
test 3
....
我的假设是,我所有的字符串被添加到列表下的第一个索引与返回字符,但我不太确定它是如何工作的。谢谢大家!
EDIT: 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
EDIT: 完整代码
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;
}
}
}
}
}
}
}
}
我假设您有两个按钮,第一个按钮应该用文件的内容填充列表,而第二个"Answer"按钮应该打印计数并从列表中删除一项。(我没有看到打印列表内容的调用,而且,由于不知道您在哪里进行调用,我无法对此提供太多帮助。)
我可以看到这里发生了几个问题。首先,您有两个事件处理程序,它们可以根据文件的内容填充列表,即
private void button1_Click(object sender, EventArgs e)
和
private void button1_Click_2(object sender, EventArgs e)
如果您在按下回答按钮之前调用button1_Click,那么这就是为什么您会得到超出范围的异常。处理程序button1_Click正在填充questionList,而Answered_Click正在从尚未填充任何数据的问题中读取数据(或缺少数据)。
简而言之,检查事件处理程序是否正确映射。
我还想指出对
的调用sr.ReadLine()
返回第一行的内容,并将流移动到第二行。我不确定您是否打算跳过第一行,直接进入第二行,而是简单地调用
sr.ReadToEnd()
应该足够了。
此外,由于文件在文件末尾包含一个制表符('t),并且您在't字符上分割字符串,因此数组中的最后一条记录将始终是空字符串。这就是为什么
this.label1.Text = s;
似乎不显示任何文本。输入的是空字符串
编辑:另外,这不是完整的代码。包含设计器代码将非常方便地识别问题。