在数组中打印对象的单个元素。 Java
Printing single element of object in array. Java
正在尝试将对象存储在数组中并稍后打印出来。而是继续获取内存中的实际位置。我这辈子都记不起如何打印内容而不是位置。
case VIEW_RECIPE:
System.out.println("Please enter the recipe ID:");
searchIndex = input.nextInt();
System.out.println(recipeArray[searchIndex-1]);
break;
case CREATE_RECIPE:
Recipe recipe = new Recipe();
recipeArray[recipe.getRecipeId() -1] = recipe; //-1 to store in element 0;
break;
所以假设在 recipeArray[] 的索引 0 处有一个食谱,我该如何打印它。这段代码只给了我内存位置,循环遍历是不切实际的。
尝试了代码
System.out.println(Arrays.toString(recipeArray[searchIndex-1]));
但是它说toString应该是deepToString,或者数组应该很长。
编辑:在被要求提供菜谱后class这里是
package potluck;
import java.util.*;
public class Recipe {
private int recipeId = 0;
private Scanner input = new Scanner(System.in);
private String attribution;
private String dateAdded;
private String category;
private String listOfIngredients;
private String tags;
private String steps;
private String comments;
public Recipe(){ //constructor
recipeId += 1;
setAttribution();
setDateAdded();
setCategory();
setListOfIngredients();
setTags();
setSteps();
System.out.println("Thank you for the recipe, it's Id is "+recipeId);
System.out.println("Use this to find it later.");
System.out.println();
}
其他都是 getter 和 setter
您应该覆盖食谱 class 中的 toString()
方法。例如:
class Recipe {
private int id;
private String name;
public String toString() {
return "Recipe#" + id + ": " + name;
}
}
我想你已经写了一个 class 食谱。如果是这样,覆盖它的 toString()
方法和 return 你想要的。这将在您使用 System.out.println()
时打印。 println(Object o)
方法简单地调用给定对象的 toString()
方法并将 returned String
写入控制台。
正在尝试将对象存储在数组中并稍后打印出来。而是继续获取内存中的实际位置。我这辈子都记不起如何打印内容而不是位置。
case VIEW_RECIPE:
System.out.println("Please enter the recipe ID:");
searchIndex = input.nextInt();
System.out.println(recipeArray[searchIndex-1]);
break;
case CREATE_RECIPE:
Recipe recipe = new Recipe();
recipeArray[recipe.getRecipeId() -1] = recipe; //-1 to store in element 0;
break;
所以假设在 recipeArray[] 的索引 0 处有一个食谱,我该如何打印它。这段代码只给了我内存位置,循环遍历是不切实际的。
尝试了代码
System.out.println(Arrays.toString(recipeArray[searchIndex-1]));
但是它说toString应该是deepToString,或者数组应该很长。
编辑:在被要求提供菜谱后class这里是
package potluck;
import java.util.*;
public class Recipe {
private int recipeId = 0;
private Scanner input = new Scanner(System.in);
private String attribution;
private String dateAdded;
private String category;
private String listOfIngredients;
private String tags;
private String steps;
private String comments;
public Recipe(){ //constructor
recipeId += 1;
setAttribution();
setDateAdded();
setCategory();
setListOfIngredients();
setTags();
setSteps();
System.out.println("Thank you for the recipe, it's Id is "+recipeId);
System.out.println("Use this to find it later.");
System.out.println();
}
其他都是 getter 和 setter
您应该覆盖食谱 class 中的 toString()
方法。例如:
class Recipe {
private int id;
private String name;
public String toString() {
return "Recipe#" + id + ": " + name;
}
}
我想你已经写了一个 class 食谱。如果是这样,覆盖它的 toString()
方法和 return 你想要的。这将在您使用 System.out.println()
时打印。 println(Object o)
方法简单地调用给定对象的 toString()
方法并将 returned String
写入控制台。