Return arraylist 中的某些值
Return certain values from the arraylist
尝试 return 仅 getUFFleet()
中的引用(IW1
、SS2
)但是它 returning [的每个值=17=]。方法 getUFFleet()
和 setupForces
来自同一个 class 而 toString
来自另一个 class FYI.
public String getUFFleet()
{
System.out.println(ForceDetails.toString());
return "No forces in UFF";
}
private void setupForces()
{
ForceDetails.add(new starShip("IW1","Twisters",200,200,10,0,0,"No","Wing" +"\n"));
ForceDetails.add(new starShip("SS2","Enterprise",300,200,0,10,20,"No","Starship"));
}
public String toString()
{
String s;
s = "\nForce reference: " + FleetRef + "\nName: " + FullName +
"\nActivation Fee: " + ActivationFee +"\nStrikers: "
+ Strikers + "\nLaser Canons: " + LaserCanon + "\nPhotonTorpedoes: "
+ PhotonTorpedoes + "\nStregth: "+ BattleStrength
+"\nCloaking: " + Cloaking + "\nForce Type: " + ForceType +"\n";
return s;
}
你可以尝试循环:
public String getUFFleet(){
ForceDetails.forEach((starShip data) -> {
System.out.println(data.getFleetRef());
}
);
return "No forces in UFF";
}
有关 forEach 循环的额外信息请参阅以下 link:
Foreach loop in java for a custom object list
尝试 return 仅 getUFFleet()
中的引用(IW1
、SS2
)但是它 returning [的每个值=17=]。方法 getUFFleet()
和 setupForces
来自同一个 class 而 toString
来自另一个 class FYI.
public String getUFFleet()
{
System.out.println(ForceDetails.toString());
return "No forces in UFF";
}
private void setupForces()
{
ForceDetails.add(new starShip("IW1","Twisters",200,200,10,0,0,"No","Wing" +"\n"));
ForceDetails.add(new starShip("SS2","Enterprise",300,200,0,10,20,"No","Starship"));
}
public String toString()
{
String s;
s = "\nForce reference: " + FleetRef + "\nName: " + FullName +
"\nActivation Fee: " + ActivationFee +"\nStrikers: "
+ Strikers + "\nLaser Canons: " + LaserCanon + "\nPhotonTorpedoes: "
+ PhotonTorpedoes + "\nStregth: "+ BattleStrength
+"\nCloaking: " + Cloaking + "\nForce Type: " + ForceType +"\n";
return s;
}
你可以尝试循环:
public String getUFFleet(){
ForceDetails.forEach((starShip data) -> {
System.out.println(data.getFleetRef());
}
);
return "No forces in UFF";
}
有关 forEach 循环的额外信息请参阅以下 link: Foreach loop in java for a custom object list