Class toString() 方法 returns 一个格式化的对象数组
Class toString() method that returns a formatted array of objects
我的情况是我必须为 class 创建一个 toString() 方法,它基本上只是从另一个 class 中获取一堆对象并将所有对象放入数组列表中。构成 arraylist 的 class 需要此 toString() 方法来 return 格式化字符串中的所有 arraylist 对象,这些对象将准备好在控制台中显示。创建放入数组列表的对象的 class 有自己的 toString() 方法,return 是所有数据的格式良好的字符串,也许我可以调用此 toString() 方法另一个 class 的 toString() 方法中的每个对象?这是代码:
Class 创建基础对象:
public class Batter {
private String firstName;
private String lastName;
private int atBats;
private int hits;
private int doubles;
private int triples;
private int homeRuns;
public Batter(String fName, String lName){
firstName = fName;
lastName = lName;
}
public void setBats(int batCount){
atBats = batCount;
}
public void setHits(int hitCount){
hits = hitCount;
}
public void setDoubles(int doubleCount){
doubles = doubleCount;
}
public void setTriples(int tripleCount){
triples = tripleCount;
}
public void setHR(int homeRunCount){
homeRuns = homeRunCount;
}
public double getAve(int hits, int bats){
double average = hits / bats;
return average;
}
public String getName(){
String fullName = this.lastName + "," + this.firstName;
return fullName;
}
public boolean equals(){
return true;
}
public String toString(){
return "Player: "+getName()+"\nAt Bats: "+this.atBats+"\nHits: "+this.hits+"\nDoubles: "+this.doubles+"\nTriples: "+this.triples+"\nHome Runs: "+this.homeRuns;
}
}
Class 将上述 class 生成的对象放入数组列表中 - 这是我不确定要做什么的地方。我需要编写的代码将在这个 class 的 toString() 方法中。同样,它的 objective 是 return 一个格式良好的字符串中的所有对象的数据,也许以某种方式使用在另一个 class:
中创建的那些对象的 toString() 方法
public class BatterDB
{
private ArrayList<Batter> theBatters = new ArrayList<Batter>(); // ArrayList of Batters
private int num; // int to store logical size of DB
// Initialize this BatterDB
public BatterDB()
{
num = 0;
}
public void addBatter(Batter b)
{
theBatters.add(b);
num++;
}
public Batter removeBatter(Batter b)
{
return theBatters.remove(0);
}
// Return logical size of the DB
public int numBatters()
{
return theBatters.size();
}
public Batter findBatter(String fName, String lName)
{
return theBatters.get(0); //change
}
public void sortName()
{
}
public void sortAve()
{
}
public String toString()
{
//Where I'm lost
}
public String toStringFile()
{
return "toStringFile()";
}
}
您要做的是将 List theBatters
变成 String
。在 BatterDB
的 toString
方法中,执行此操作:
return theBatters.stream() // get all of them in a stream
.map(b -> b.toString()) // turn them into strings
.collect(Collectors.joining(", ")) // join them with commas
// (also can be anything, like "\n" for new lines)
我的情况是我必须为 class 创建一个 toString() 方法,它基本上只是从另一个 class 中获取一堆对象并将所有对象放入数组列表中。构成 arraylist 的 class 需要此 toString() 方法来 return 格式化字符串中的所有 arraylist 对象,这些对象将准备好在控制台中显示。创建放入数组列表的对象的 class 有自己的 toString() 方法,return 是所有数据的格式良好的字符串,也许我可以调用此 toString() 方法另一个 class 的 toString() 方法中的每个对象?这是代码:
Class 创建基础对象:
public class Batter {
private String firstName;
private String lastName;
private int atBats;
private int hits;
private int doubles;
private int triples;
private int homeRuns;
public Batter(String fName, String lName){
firstName = fName;
lastName = lName;
}
public void setBats(int batCount){
atBats = batCount;
}
public void setHits(int hitCount){
hits = hitCount;
}
public void setDoubles(int doubleCount){
doubles = doubleCount;
}
public void setTriples(int tripleCount){
triples = tripleCount;
}
public void setHR(int homeRunCount){
homeRuns = homeRunCount;
}
public double getAve(int hits, int bats){
double average = hits / bats;
return average;
}
public String getName(){
String fullName = this.lastName + "," + this.firstName;
return fullName;
}
public boolean equals(){
return true;
}
public String toString(){
return "Player: "+getName()+"\nAt Bats: "+this.atBats+"\nHits: "+this.hits+"\nDoubles: "+this.doubles+"\nTriples: "+this.triples+"\nHome Runs: "+this.homeRuns;
}
}
Class 将上述 class 生成的对象放入数组列表中 - 这是我不确定要做什么的地方。我需要编写的代码将在这个 class 的 toString() 方法中。同样,它的 objective 是 return 一个格式良好的字符串中的所有对象的数据,也许以某种方式使用在另一个 class:
中创建的那些对象的 toString() 方法public class BatterDB
{
private ArrayList<Batter> theBatters = new ArrayList<Batter>(); // ArrayList of Batters
private int num; // int to store logical size of DB
// Initialize this BatterDB
public BatterDB()
{
num = 0;
}
public void addBatter(Batter b)
{
theBatters.add(b);
num++;
}
public Batter removeBatter(Batter b)
{
return theBatters.remove(0);
}
// Return logical size of the DB
public int numBatters()
{
return theBatters.size();
}
public Batter findBatter(String fName, String lName)
{
return theBatters.get(0); //change
}
public void sortName()
{
}
public void sortAve()
{
}
public String toString()
{
//Where I'm lost
}
public String toStringFile()
{
return "toStringFile()";
}
}
您要做的是将 List theBatters
变成 String
。在 BatterDB
的 toString
方法中,执行此操作:
return theBatters.stream() // get all of them in a stream
.map(b -> b.toString()) // turn them into strings
.collect(Collectors.joining(", ")) // join them with commas
// (also can be anything, like "\n" for new lines)