【问题标题】:Access object variables after object added to a queue?对象添加到队列后访问对象变量?
【发布时间】:2015-11-04 15:15:52
【问题描述】:

我正在尝试了解如何获取 Customer 对象的名称和食物,当它已经添加到队列中时?所以说我想在添加到队列后使用第一个客户对象的名称和食物元素打印一个字符串?队列窥视方法是占位符,因为我不确定在将对象添加到队列后如何访问对象的名称和食物。如果我打印 peek 方法,它只会给我内存位置,而不是对象食物或名称。

结果会是这样的:

“您要处理什么:比萨饼还是沙拉?

沙拉

詹姆斯的沙拉做好了!”

代码:

主类:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        File customerTxt = new File("customer.txt");
        Queue<Customer> pizza = new LinkedList<Customer>();
        Queue<Customer> salad = new LinkedList<Customer>();
        try {
            Scanner readCus = new Scanner(customerTxt);
            Scanner readFood = new Scanner(System.in);
            while (readCus.hasNextLine()) {
                String line = readCus.nextLine();
                String[] strArray = line.split(",");
                String customerName = strArray[0];
                String customerFood = strArray[1];
                Customer cus = new Customer(customerName, customerFood);
                if (customerFood.equalsIgnoreCase("salad")) {
                    salad.add(cus);
                }
                if (customerFood.equalsIgnoreCase("pizza")) {
                    pizza.add(cus);
                }
            }
            if (pizza.isEmpty() == false && salad.isEmpty() == false) {
                System.out.println("What kind of food would you like to make?");
                String foodChoice = readFood.nextLine();
                if (foodChoice.equalsIgnoreCase("salad")) {
                    System.out.println(salad.peek());
                }
                if (foodChoice.equalsIgnoreCase("pizza")) {
                    System.out.println(salad.peek());
                }
            }
            if (pizza.isEmpty() == true && salad.isEmpty() == false) {
                System.out.println("There are no Pizzas left to process. I will just finish the rest of the Salads");
                while (salad.isEmpty() == false) {
                    System.out.println(salad.peek());
                }
            }
            if (pizza.isEmpty() == false && salad.isEmpty() == true) {
                System.out.println("There are no Salads left to process. I will just finish the rest of the Pizzas");
                while (pizza.isEmpty() == false) {
                    System.out.println(pizza.peek());
                }
            }
        }

        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

客户类别:

public class Customer {

    public String name = "";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String food = "";

    public String getFood() {
        return food;
    }

    public void setFood(String food) {
        this.food = food;
    }

    public Customer(String customerName, String customerFood) {
        this.name = customerName;
        this.food = customerFood;
    }



    }

【问题讨论】:

  • 覆盖 Customer 类中的 toString() 以包含食物和名称值
  • 顺便说一句,当! &lt;boolean value&gt; 很好时,找到&lt;boolean value&gt; == false 非常难看。也许这只是我,但我认为您应该将您的 pizza.isEmpty() == false &amp;&amp; salad.isEmpty() == false 更改为 ! pizza.isEmpty() &amp;&amp; ! salad.isEmpty()!(pizza.isEmpty() || salad.isEmpty())

标签: java oop linked-list queue


【解决方案1】:

根据LinkedList.peek(),它确实返回了正确的对象。我相信您只是看到了对象的哈希值,因为您打印了 Customer 对象,该对象尚未重新定义 .toString() :您正在使用 Object.toString() 返回您所看到的哈希值。

如果您总是想将 Customer 表示为其名称和食物选择,请按照 Zack Macomber 的建议在 Customer 中重新定义 .toString(),或者改为选择 System.out.println(queue.peek().getName() + " choosed " + queue.peek().getFood()) 或类似名称。

【讨论】:

    猜你喜欢
    • 2015-02-14
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 2012-11-02
    相关资源
    最近更新 更多