ArrayList输出错误

时间:2014-07-19 05:01:16

标签: java arraylist

我刚刚开始研究Vending Machine程序,但我在这里遇到了一个问题。输出不符合预期。这是我的代码。

自动售货机:

import java.util.*;

public class VendingMachine {


    static List<ShowMenu> menu = new ArrayList<ShowMenu>();

    public static void main(String [] args){


    System.out.println("Please choose from the menu below:");
    for(int i=0; i<9; i++){

        String[] item = {"Water", "Coke" , "Diet Coke", "Iced Tea", "Swiss Chocolate", "Candy", "Chips", "Bubble Gum", "Turkish Delight"};
        int[] count = {10,10,10,10,10,10,10,10,10};
        double[] price = {0.75,1.20,1.20,1.00,1.50,0.95,1.10,0.50,1.20};


        ShowMenu items = new ShowMenu(item, count, price);
        menu.add(items);

    }

    int k=1;

    for(ShowMenu item: menu){
        System.out.print(k +".");
        k = k + 1;
        System.out.println(item);
    }
   }
  }

ShowMenu:

    import java.util.*;

    public class ShowMenu {

    public String[] item;
    public int[] count;
    public double[] price;

    public ShowMenu(String[] items, int[] count, double[] price){

        this.item = items;
        this.count = count;
        this.price = price;

    }


    public String toString(){
    return " " + item + " " + "$" + price;
    }
  }

这是下面的输出。我期待以价格列出项目,例如 1.水0.75美元,但它显示另一件事。

Please choose from the menu below:
1. [Ljava.lang.String;@5f47ff11 $[D@6fbae5f5
2. [Ljava.lang.String;@574f7121 $[D@208c5a4f
3. [Ljava.lang.String;@5e7d093a $[D@62e8ef4c
4. [Ljava.lang.String;@2ab6994f $[D@3a0b2771
5. [Ljava.lang.String;@324a897c $[D@3b8845af
6. [Ljava.lang.String;@6f9702b1 $[D@2331ebb3
7. [Ljava.lang.String;@226946d2 $[D@2da896a4
8. [Ljava.lang.String;@173a0067 $[D@60f0761a
9. [Ljava.lang.String;@4ad9d6ae $[D@239d5fe6

任何人帮助.. !!

2 个答案:

答案 0 :(得分:1)

您正在对整个产品列表进行循环,而不是在每个产品上。这是你修改的代码:

import java.util.*;

public class VendingMachine
{

    static List<ShowMenu> menu = new ArrayList<ShowMenu>();

    public static void main(String[] args)
    {

        System.out.println("Please choose from the menu below:");
        for (int i = 0; i < 9; i++)
        {

            String[] item = {"Water", "Coke", "Diet Coke", "Iced Tea", "Swiss Chocolate", "Candy", "Chips",
                             "Bubble Gum", "Turkish Delight"};
            int[] count = {10, 10, 10, 10, 10, 10, 10, 10, 10};
            double[] price = {0.75, 1.20, 1.20, 1.00, 1.50, 0.95, 1.10, 0.50, 1.20};

            ShowMenu items = new ShowMenu(item[i], count[i], price[i]);
            menu.add(items);

        }

        int k = 1;

        for (ShowMenu item : menu)
        {
            System.out.print(k + ".");
            k = k + 1;
            System.out.println(item);
        }
    }
}




import java.util.*;

public class ShowMenu
{

    public String item;
    public int count;
    public double price;

    public ShowMenu(String items, int count, double price)
    {

        this.item = items;
        this.count = count;
        this.price = price;

    }

    public String toString()
    {
        return " " + item + " " + "$" + price;
    }
}

输出结果为:

Please choose from the menu below:
1. Water $0.75
2. Coke $1.2
3. Diet Coke $1.2
4. Iced Tea $1.0
5. Swiss Chocolate $1.5
6. Candy $0.95
7. Chips $1.1
8. Bubble Gum $0.5
9. Turkish Delight $1.2

答案 1 :(得分:0)

return " " + item + " " + "$" + price;

当您将项目作为字符串返回时,您将获得[LJava.lang...表示法。使用Arrays.toString(item)