Home > Enterprise >  Return certain values from the arraylist
Return certain values from the arraylist

Time:12-15

Trying to return only the references(IW1, SS2) in the getUFFleet() however it is returning every single value of the toString(). MethodsgetUFFleet() and setupForces are from the same class while the toString from another 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;
    }

CodePudding user response:

You can try to loop trough:

public String getUFFleet(){
    ForceDetails.forEach((starShip data) -> {
        System.out.println(data.getFleetRef());
      }
    );

    return "No forces in UFF";
}

as extra information about forEach loops see following link: Foreach loop in java for a custom object list

  • Related