已为我提供了一个对象以及使用该对象的几种方法。我很难打印要分配给变量的字符串。目前,我不确定是否分配所有新值,并且无法打印任何值。以前的迭代仅打印参考。
问题:我要分配新的字符串值吗?如何使用给定的方法打印字符串?
这是我提供的代码
str.sortWith{ case (c1,c2) => c1.compareTo(c2) < 0}
//" &ADLOOOPSSTacdeeeghiiinoooppprrrsv"
这是我当前的代码
public class Team implements Comparable<Team> {
public String toString(String team, int wins) {
return team + ": " + wins;
}
// Data fields
private String name;
private int winCount;
Team() {
name = "Sooners";
winCount = 1;
}
Team(String inputName) {
name = inputName;
winCount = 1;
}
Team(String inputName, int inputWinCount) {
name = inputName;
winCount = inputWinCount;
}
// ----------------------------------------------------
// Getters and Setters
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the winCount
*/
public int getWinCount() {
return winCount;
}
/**
* @param winCount
* the winCount to set
*/
public void setWinCount(int winCount) {
this.winCount = winCount;
}
/**
* Increments the winCount variable by one for this Team
*/
public void incrementWinCount() {
winCount++;
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object.
*
* This method allows you to use the contains method in ArrayList to see
* if any element in an array list has the same name as a specific Team.
*
* @param o
* the other Team being compared to.
*/
@Override
public boolean equals(Object o) {
return name.equals(((Team) o).name);
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object
*
* @param otherTeam
* one team
*/
public boolean sameName(Team otherTeam) {
return name.equals(otherTeam.name);
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object
*
* @param team1
* one team
* @param team2
* the other team
*/
public static boolean sameName(Team team1, Team team2) {
return team1.name.equals(team2.name);
}
/**
* This method allows you to sort an ArrayList of Team items using
* Collections.sort
*
* @param o
* the other Team being compared to.
* @return -1 if this Team item should come first, +1 if this Team item
* should come after the other, and 0 if this Team item is
* equivalent to the other.
*/
@Override
public int compareTo(Team o) {
if (this.winCount < o.winCount) {
return -1;
} else if (this.winCount > o.winCount) {
return 1;
}
return 0;
}
}
答案 0 :(得分:0)
他们给你的toString方法的原始代码是什么?是否添加了参数?更好的方法是让对象在方法内部使用其数据字段。在您的for循环中传递成员变量是不必要的,并且只是错误的代码。相反,您想要执行以下操作:
JSON.parse()
在循环中,如果要打印结果,只需执行以下操作:
public String toString() {
return name + ": " + wins;
}