[英]List.Add not increasing List.Count
我試圖將元素添加到List<String>
以便可以一次從隊列中刪除它們。 這是將內容添加到我的列表中的代碼:
foreach (String s in q)
{
if(s != null)
{
String str = s.Replace("!question", "");
questions.Add(str);
this.label1.Text = str;
}
}
q是我用來拆分的數組,問題是List<String>
。 Label1應該無關(我使用的是Windows Form)
這是我用來嘗試從列表中刪除第二行的代碼。 它引發“超出范圍”異常:
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
....
我的假設是,我所有的字符串都將被添加到帶有返回字符的第一個索引下的列表中,但我不太確定它是如何工作的。 感謝大家!
編輯 : 輸入.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;
}
}
}
}
}
}
}
}
我假設您有兩個按鈕,第一個應該用文件的內容填充列表,而第二個“答案”按鈕應該打印計數並從列表中刪除一個項目。 (我看不到要打印列表內容的調用,並且不知道您在哪里進行此調用,對此我無能為力。)
我可以看到這里發生了幾個問題。 首先,您有兩個事件處理程序可以根據文件的內容填充列表,即
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;
似乎不顯示任何文本。 它被送入空字符串。
編輯:另外,那不是完整的代碼。 包括設計者代碼將很容易發現問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.