我需要将所有教授从文本文件添加到链接列表。这就是文本文件的样子:
Math;John Torpel;10
Physics;Tom Smith;40
...
所以我写了阅读方法:
public void ReadData(Container kon)
{
using (StreamReader reader = new StreamReader("C:\\Users\\Sparta\\Documents\\Visual Studio 2015\\WebSites\\WebSite4\\u22b.txt"))
{
string line = null;
while ((line = reader.ReadLine()) != null)
{
LinkedList list = new LinkedList();
string[] values = line.Split(';');
ProjectWork temp = new ProjectWork(values[0], values[1], int.Parse(values[2]));
list.AddToEnd(values[1]);
}
}
}
和AddToEnd方法:
public void AddToEnd(ProjectWork data)
{
if(head == null)
{
head = new Node(data);
}
else
{
head.AddToEnd(data);
}
}
我知道我做错了什么。如果我想将教授名称(值[1])添加到链表?
,我需要更改什么编辑:LinkedList类:
public class LinkedList
{
private Node head;
public LinkedList()
{
head = null;
}
public void AddToEnd(ProjectWork data)
{
if(head == null)
{
head = new Node(data);
}
else
{
head.AddToEnd(data);
}
}
进行此操作
答案 0 :(得分:2)
有两个问题。
应该改为:
using (StreamReader reader = new StreamReader("C:\\Users\\Sparta\\Documents\\Visual Studio 2015\\WebSites\\WebSite4\\u22b.txt"))
{
string line = null;
LinkedList list = new LinkedList();
while ((line = reader.ReadLine()) != null)
{
string[] values = line.Split(';');
ProjectWork temp = new ProjectWork(values[0], values[1], int.Parse(values[2]));
list.AddToEnd(temp);
}
}
答案 1 :(得分:1)
public void ReadData(Container kon)
{
using (StreamReader reader = new StreamReader("C:\\Users\\Sparta\\Documents\\Visual Studio 2015\\WebSites\\WebSite4\\u22b.txt"))
{
LinkedList list = new LinkedList();
string line = null;
while ((line = reader.ReadLine()) != null)
{
string[] values = line.Split(';');
ProjectWork temp = new ProjectWork(values[0], values[1], int.Parse(values[2]));
list.AddToEnd(temp);
}
}
}
class ProjectWork
{
public string Lesson { get; set; }
public string FullName { get; set; }
public int Credits { get; set; }
public ProjectWork(string _lesson, string _fullname, int _credits)
{
Lesson = _lesson;
FullName = _fullname;
Credits = _credits;
}
}
class LinkedList
{
public void AddToEnd(ProjectWork data)
{
var myFullName = data.FullName;
var Lesson = data.Lesson;
var Credits = data.Credits;
//
}
}