Я пытаюсь добавить элементы в List<String>
, чтобы я мог их удалить по очереди из очереди. Вот код, который добавляет вещи в мой список:List.Add не увеличивается List.Count
foreach (String s in q)
{
if(s != null)
{
String str = s.Replace("!question", "");
questions.Add(str);
this.label1.Text = str;
}
}
д представляет собой массив, который я использовал для разделения, а также вопросы является List<String>
. Ярлык1 должен быть неактуальным (я использую Windows Form)
Вот код, который я использую, чтобы попробовать и удалить вторую строку из списка. Он выбрасывает «из диапазона» исключение:
questions.RemoveAt(1);
Console.WriteLine(questions.Count);
foreach(String s in questions)
{
if (s!= null)
this.label1.Text = s;
}
Когда он печатает на консоль, граф является один, но если я распечатать список, он дает мне правильный список:
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;
}
}
}
}
}
}
}
}
Как вы разбиваете строку? Я предполагаю, что это как-то связано с этим. Отправьте полный код, чтобы мы могли вам помочь. – entropic
Если 'Count' равно 1, то имеет смысл, что удаление элемента с индексом 1 (2-й элемент) потерпит неудачу. Нам нужно посмотреть, что такое начальное значение 'q'. –
Добавлен полный код и вход .txt – Maxw3ll