将对象的实例分配给数组
Assigning instances of objects to an array
我正在使用 bufferedFileReader
和 lineScanner
通读 csv
文件,以逗号分隔并将行中的第一个标记分配给 class 的对象Team
。这之后的每个标记都分配给一个变量 Team
.
我这部分工作正常。下一部分是将这些对象放入数组中,我不知道该怎么做。我假设我需要在 while 循环(可能是 for 循环)的底部放置更多代码,但我不确定。
class 的代码是:
public class Pool
{
/* instance variables */
private String poolName; // the name of the pool
private Team[] teams; // the teams in the pool
private final static int NOOFTEAMS = 5; // number of teams in each pool
/**
* Constructor for objects of class Pool
*/
public Pool(String aName)
{
super();
this.poolName = aName;
this.teams = new Team[NOOFTEAMS];
}
/**
* Prompts the user for the name of the text file that
* contains the results of the teams in this pool. The
* method uses this file to set the results of the teams.
*/
public void loadTeams()
{
String fileName;
OUDialog.alert("Select input file for " + this.getPoolName());
fileName = OUFileChooser.getFilename();
File aFile = new File(fileName);
BufferedReader bufferedFileReader = null;
try
{
Scanner lineScanner;
bufferedFileReader = new BufferedReader(new FileReader(aFile));
String correctPool = bufferedFileReader.readLine();
if (!poolName.equals(correctPool))
{
OUDialog.alert("Wrong File Selected");
}
else
{
String currentLine = bufferedFileReader.readLine();
while (currentLine != null)
{
lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
Team aTeam = new Team(lineScanner.next());
aTeam.setWon(lineScanner.nextInt());
aTeam.setDrawn(lineScanner.nextInt());
aTeam.setLost(lineScanner.nextInt());
aTeam.setFourOrMoreTries(lineScanner.nextInt());
aTeam.setSevenPointsOrLess(lineScanner.nextInt());
currentLine = bufferedFileReader.readLine();
aTeam.setTotalPoints(aTeam.calculateTotalPoints());
//somewhere here I need to add the aTeam object to the array
}
}
将此添加到您的属性中:
private List<Team> myTeam=new ArrayList<Team>();
然后在循环的末尾添加这一行:
myTeam.add(aTeam);
如果绝对必须是 array
而不是 ArrayList
那么在循环之后执行此操作:
Team[] myArray=new Team[myTeam.size()];
myTeam.toArray(myArray);
public class Pool
{
private int teamCounter;
...
public Pool(String aName)
{
super();
this.poolName = aName;
this.teams = new Team[NOOFTEAMS];
teamCounter=0;
}
...
public void loadTeams()
{
...
//somewhere here I need to add the aTeam object to the array
this.teams[teamCounter++]=aTeam;
}
}
我正在使用 bufferedFileReader
和 lineScanner
通读 csv
文件,以逗号分隔并将行中的第一个标记分配给 class 的对象Team
。这之后的每个标记都分配给一个变量 Team
.
我这部分工作正常。下一部分是将这些对象放入数组中,我不知道该怎么做。我假设我需要在 while 循环(可能是 for 循环)的底部放置更多代码,但我不确定。
class 的代码是:
public class Pool
{
/* instance variables */
private String poolName; // the name of the pool
private Team[] teams; // the teams in the pool
private final static int NOOFTEAMS = 5; // number of teams in each pool
/**
* Constructor for objects of class Pool
*/
public Pool(String aName)
{
super();
this.poolName = aName;
this.teams = new Team[NOOFTEAMS];
}
/**
* Prompts the user for the name of the text file that
* contains the results of the teams in this pool. The
* method uses this file to set the results of the teams.
*/
public void loadTeams()
{
String fileName;
OUDialog.alert("Select input file for " + this.getPoolName());
fileName = OUFileChooser.getFilename();
File aFile = new File(fileName);
BufferedReader bufferedFileReader = null;
try
{
Scanner lineScanner;
bufferedFileReader = new BufferedReader(new FileReader(aFile));
String correctPool = bufferedFileReader.readLine();
if (!poolName.equals(correctPool))
{
OUDialog.alert("Wrong File Selected");
}
else
{
String currentLine = bufferedFileReader.readLine();
while (currentLine != null)
{
lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
Team aTeam = new Team(lineScanner.next());
aTeam.setWon(lineScanner.nextInt());
aTeam.setDrawn(lineScanner.nextInt());
aTeam.setLost(lineScanner.nextInt());
aTeam.setFourOrMoreTries(lineScanner.nextInt());
aTeam.setSevenPointsOrLess(lineScanner.nextInt());
currentLine = bufferedFileReader.readLine();
aTeam.setTotalPoints(aTeam.calculateTotalPoints());
//somewhere here I need to add the aTeam object to the array
}
}
将此添加到您的属性中:
private List<Team> myTeam=new ArrayList<Team>();
然后在循环的末尾添加这一行:
myTeam.add(aTeam);
如果绝对必须是 array
而不是 ArrayList
那么在循环之后执行此操作:
Team[] myArray=new Team[myTeam.size()];
myTeam.toArray(myArray);
public class Pool
{
private int teamCounter;
...
public Pool(String aName)
{
super();
this.poolName = aName;
this.teams = new Team[NOOFTEAMS];
teamCounter=0;
}
...
public void loadTeams()
{
...
//somewhere here I need to add the aTeam object to the array
this.teams[teamCounter++]=aTeam;
}
}