我需要阅读一系列棒球运动员名字和各种统计数据,这些数据是用''分隔的。 这是2行,所以我的示例代码将有意义
Abreu, J;CWS;1B;30;101;0.29
Altuve, J;HOU;2B;15;66;0.313
目前我正在将文件读入一个数组,其中每一行都在数组中自己的索引中,代码如下:
public static void fillArray()
{
string[] linesInFile = File.ReadAllLines(GlobalVariables.filePath);
//EACH LINE IS A STRING AT THIS POINT
foreach (string s in linesInFile)
{
string[] items = s.Split(';');
for (int i = 0; i < items.Length; i++)
{
Console.WriteLine(items[i]);
//Console.WriteLine(items[1]);
}
}
}
唯一的问题是我是C#的新手并且不知道如何从不同的方法访问这个填充的数组,因此我可以对其进行排序并显示各种信息等。
我已经搜索了我在谷歌上可以想到的所有排列,但我无法找到任何相关的答案。
感谢您的帮助。
答案 0 :(得分:1)
如何从不同的方法访问此填充数组
您需要将数组作为参数传递给另一个方法,否则您需要在方法之外声明变量以扩大其范围。
答案 1 :(得分:1)
一种方法是在方法fillArray
List<string[]> itemsList = new List<string[]>(); //declare this outside of the method
public static void fillArray()
{
string[] linesInFile = File.ReadAllLines(GlobalVariables.filePath);
//EACH LINE IS A STRING AT THIS POINT
foreach (string s in linesInFile)
{
string[] items = s.Split(';');
itemsList.Add(items); //add the list in the itemsList
for (int i = 0; i < items.Length; i++)
{
Console.WriteLine(items[i]);
//Console.WriteLine(items[1]);
}
}
}
然后只需在其他方法中使用它:
public void otherMethod(){
string[] itemsAtZero = itemsList[0]; //your first string[] items
string[] itemsAtOne = itemsList[1]; //your second string[] items
//and so on...
}
答案 2 :(得分:1)
只需更改fillArray
方法的签名即可返回array
。给你样本代码,你需要一个二维数组,因为你有多行,所以jagged array是一个很好的选择,如果玩家&#39;统计数据不固定(某些玩家可能拥有更少或更多的统计数据)。或者,您可以返回一个列表(List<List<string>>
)而不是数组,它们比数组更灵活。因此,返回锯齿状数组的代码将如下所示:
public static string[][] fillArray()
{
string[] linesInFile = File.ReadAllLines(GlobalVariables.filePath);
string[][] result = new string[linesInFile.Length][];
int index = 0;
//EACH LINE IS A STRING AT THIS POINT
foreach (string s in linesInFile)
{
string[] items = s.Split(';');
result[index++] = items;
}
return result;
}
请注意,返回数组而不是全局数组并在方法中更改它更为可取/可读。
答案 3 :(得分:1)
我会这样做:
Player[] players =
(
from line in File.ReadAllLines(GlobalVariables.filePath)
let parts = line.Split(';')
select new Player(
parts[0], parts[1], parts[2],
int.Parse(parts[3]), int.Parse(parts[4]),
double.Parse(parts[5]))
).ToArray();
你会得到这个结果:
你也需要这个Player
课程:
public sealed class Player : IEquatable<Player>
{
private readonly string _Name;
private readonly string _Team;
private readonly string _Position;
private readonly int _HomeRuns;
private readonly int _Rbi;
private readonly double _BattingAverage;
public string Name { get { return _Name; } }
public string Team { get { return _Team; } }
public string Position { get { return _Position; } }
public int HomeRuns { get { return _HomeRuns; } }
public int Rbi { get { return _Rbi; } }
public double BattingAverage { get { return _BattingAverage; } }
public Player(string Name, string Team, string Position, int HomeRuns, int Rbi, double BattingAverage)
{
_Name = Name;
_Team = Team;
_Position = Position;
_HomeRuns = HomeRuns;
_Rbi = Rbi;
_BattingAverage = BattingAverage;
}
public override bool Equals(object obj)
{
if (obj is Player)
return Equals((Player)obj);
return false;
}
public bool Equals(Player obj)
{
if (obj == null) return false;
if (!EqualityComparer<string>.Default.Equals(_Name, obj._Name)) return false;
if (!EqualityComparer<string>.Default.Equals(_Team, obj._Team)) return false;
if (!EqualityComparer<string>.Default.Equals(_Position, obj._Position)) return false;
if (!EqualityComparer<int>.Default.Equals(_HomeRuns, obj._HomeRuns)) return false;
if (!EqualityComparer<int>.Default.Equals(_Rbi, obj._Rbi)) return false;
if (!EqualityComparer<double>.Default.Equals(_BattingAverage, obj._BattingAverage)) return false;
return true;
}
public override int GetHashCode()
{
int hash = 0;
hash ^= EqualityComparer<string>.Default.GetHashCode(_Name);
hash ^= EqualityComparer<string>.Default.GetHashCode(_Team);
hash ^= EqualityComparer<string>.Default.GetHashCode(_Position);
hash ^= EqualityComparer<int>.Default.GetHashCode(_HomeRuns);
hash ^= EqualityComparer<int>.Default.GetHashCode(_Rbi);
hash ^= EqualityComparer<double>.Default.GetHashCode(_BattingAverage);
return hash;
}
public override string ToString()
{
return String.Format("{{ Name = {0}, Team = {1}, Position = {2}, HomeRuns = {3}, Rbi = {4}, BattingAverage = {5} }}", _Name, _Team, _Position, _HomeRuns, _Rbi, _BattingAverage);
}
}
答案 4 :(得分:1)
您必须返回数组读取:
public static String[][] fillArray() {
return File
.ReadLines(GlobalVariables.filePath)
.Select(line => line.Split(';'))
.ToArray(); // usually not necessary, but question requies array
}
所以你可以说,从其他方法打印出数组:
Console.Write(String.Join(Ebvironment.NewLine, fillArray()
.OrderBy(items => items[1]) // Let's sort the array by second column
.ThenBy(items => items[0]) // ..and by 1st column on tie
.Select(items => String.Join(","))));
更好的方法是返回一个特殊设计的课程Player
而不是String[]
,如下所示:
public static Player[] fillArray() {
return File
.ReadLines(GlobalVariables.filePath)
.Select(line => line.Split(';'))
.Select(items => new Player(items[0], items[1], items[2]) {
HomeRuns = Int.Parse(items[3], CultureInfo.InvariantCulture),
RBI = Double.Parse(items[4], CultureInfo.InvariantCulture),
Batting = Double.Parse(items[5], CultureInfo.InvariantCulture),
})
.ToArray(); // usually not necessary, but question requies array
}
,样本报告将
Console.Write(String.Join(Ebvironment.NewLine, fillArray()
.OrderBy(player => player.Team)
.ThenBy(player => player.Name));
答案 5 :(得分:0)
做这样的事情......你可以在网上找到很多好的教程,帮助你在C#上建立自己的基础
"^[\\d\\%]+$"