Java 如何使字符串变量具有多个实例? 公共字符串foodstats(){ 字符串foodstats=“”; 对于(int i=0;i

Java 如何使字符串变量具有多个实例? 公共字符串foodstats(){ 字符串foodstats=“”; 对于(int i=0;i,java,Java,因此返回:Name:水营养:30耐力:15 我知道为什么它会这样做,当for循环第二次运行时,它会替换第一个项目的统计信息,并只返回被替换的统计信息 有办法解决这个问题吗?我需要根据数组列表大小返回所有项目的统计信息。我认为您需要的是StringBuilder,在这种情况下比+=串联更有效: public String foodstats (){ String foodstats = ""; for ( int i = 0; i < consumables.size();

因此返回:
Name:水营养:30耐力:15

我知道为什么它会这样做,当for循环第二次运行时,它会替换第一个项目的统计信息,并只返回被替换的统计信息


有办法解决这个问题吗?我需要根据数组列表大小返回所有项目的统计信息。

我认为您需要的是
StringBuilder
,在这种情况下比
+=
串联更有效:

public String foodstats (){
    String foodstats = "";
    for ( int i = 0; i < consumables.size(); i++){
         foodstats =  "Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n" ;
   }
    return foodstats;
}
公共字符串foodstats(){
StringBuilder foodstats=新的StringBuilder();
对于(int i=0;i
我认为您需要的是一个
StringBuilder
,在这种情况下比
+=
串联更有效:

public String foodstats (){
    String foodstats = "";
    for ( int i = 0; i < consumables.size(); i++){
         foodstats =  "Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n" ;
   }
    return foodstats;
}
公共字符串foodstats(){
StringBuilder foodstats=新的StringBuilder();
对于(int i=0;i
我认为您需要的是一个
StringBuilder
,在这种情况下比
+=
串联更有效:

public String foodstats (){
    String foodstats = "";
    for ( int i = 0; i < consumables.size(); i++){
         foodstats =  "Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n" ;
   }
    return foodstats;
}
公共字符串foodstats(){
StringBuilder foodstats=新的StringBuilder();
对于(int i=0;i
我认为您需要的是一个
StringBuilder
,在这种情况下比
+=
串联更有效:

public String foodstats (){
    String foodstats = "";
    for ( int i = 0; i < consumables.size(); i++){
         foodstats =  "Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n" ;
   }
    return foodstats;
}
公共字符串foodstats(){
StringBuilder foodstats=新的StringBuilder();
对于(int i=0;i
制作一个
StringBuilder
并在循环中使用
附加(…)
。完成后,对结果调用
toString()
,如下所示:

public String foodstats (){
    StringBuilder foodstats = new StringBuilder();
    for ( int i = 0; i < consumables.size(); i++){
         foodstats.append("Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n");
   }
    return foodstats.toString();
}
StringBuilder res=new StringBuilder();
对于(int i=0;i

注意:您也可以使用
foodstats+=…
语法,但这会比较差,因为循环的每次迭代都会创建一个一次性临时对象。

创建一个
StringBuilder
并在循环中使用
附加(…)
。完成后,对结果调用
toString()
,如下所示:

public String foodstats (){
    StringBuilder foodstats = new StringBuilder();
    for ( int i = 0; i < consumables.size(); i++){
         foodstats.append("Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n");
   }
    return foodstats.toString();
}
StringBuilder res = new StringBuilder();
for ( int i = 0; i < consumables.size(); i++){
     res.append("Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n");
}
return res.toString();
StringBuilder res=new StringBuilder();
对于(int i=0;i

注意:您也可以使用
foodstats+=…
语法,但这会比较差,因为循环的每次迭代都会创建一个一次性临时对象。

创建一个
StringBuilder
并在循环中使用
附加(…)
。完成后,对结果调用
toString()
,如下所示:

public String foodstats (){
    StringBuilder foodstats = new StringBuilder();
    for ( int i = 0; i < consumables.size(); i++){
         foodstats.append("Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n");
   }
    return foodstats.toString();
}
StringBuilder res = new StringBuilder();
for ( int i = 0; i < consumables.size(); i++){
     res.append("Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n");
}
return res.toString();
StringBuilder res=new StringBuilder();
对于(int i=0;i

注意:您也可以使用
foodstats+=…
语法,但这会比较差,因为循环的每次迭代都会创建一个一次性临时对象。

创建一个
StringBuilder
并在循环中使用
附加(…)
。完成后,对结果调用
toString()
,如下所示:

public String foodstats (){
    StringBuilder foodstats = new StringBuilder();
    for ( int i = 0; i < consumables.size(); i++){
         foodstats.append("Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n");
   }
    return foodstats.toString();
}
StringBuilder res = new StringBuilder();
for ( int i = 0; i < consumables.size(); i++){
     res.append("Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n");
}
return res.toString();
StringBuilder res=new StringBuilder();
对于(int i=0;i
注意:您也可以使用
foodstats+=…
语法,但这样做会比较差,因为循环的每次迭代都会创建一个一次性临时对象。

公共字符串foodstats()
StringBuilder res = new StringBuilder();
for ( int i = 0; i < consumables.size(); i++){
     res.append("Name: " + consumables.get(i).getName() + "\tNutrition: " + consumables.get(i).getNValue() + "\tStamina: " + consumables.get(i).getStamina() + "\n");
}
return res.toString();
{ StringBuilder foodstats=新的StringBuilder(); 对于(int i=0;i
公共字符串foodstats()
{
StringBuilder foodstats=新的StringBuilder();
对于(int i=0;i