如何显示此数组的输出?

时间:2019-04-08 17:46:41

标签: java arrays

我的类型类数组有问题。当我运行程序时,输出的是数组的地址,而不是我初始化的地址?

代码:

    Items dairy[] = new Items[4];

    dairy[0] = new Items("Butter",14);
    dairy[1] = new Items ("Cream cheese",10.5);
    dairy[2] = new Items ("Sliced cheese",7);
    dairy[3] = new Items("Eggs",14);

还有更多,但我给出了示例,因为所有问题都相同。

整个班级:

    Scanner input=new Scanner(System.in);
    int selection;

    Items dairy[] = new Items[4];
    Items homehold[] = new Items[4];
    Items produce[] = new Items[4];

    dairy[0] = new Items("Butter",14);
    System.out.println(dairy[0]);
    dairy[1] = new Items ("Cream cheese",10.5);
    dairy[2] = new Items ("Sliced cheese",7);
    dairy[3] = new Items("Eggs",14);

    homehold[0] = new Items( "Toilet paper",24);
    homehold[1] = new Items("Paper towles",12);
    homehold[2]= new Items("Laundry soap",32);
    homehold[3]= new Items("Dishwasher soap",9);

    produce[0] = new Items( "Apple",5);
    produce[1]= new Items("Avocado",12);
    produce[2]= new Items("Banana",7);
    produce[3] = new Items("Orange",6.5);

    System.out.println("Press 1:" + dairy[0]+"\n"+ "Press 2:" + dairy[1]+"\n"+ "Press 3:" + dairy[2]+"\n"+ "Press 4:" + dairy[3]+);

    System.out.println("Press 5:" + homehold[0]+"\n"+"Press 6:" + homehold[1]+"\n"+
    "Press 7:" + homehold[2]+"\n"+"Press 8:" + homehold[3]);

    System.out.println("Press 9:" + produce[0]+ "\n"+"Press 10:" + produce[1]+ "\n"+
    "Press 11:" + produce[2]+ "\n"+"Press 12:" + produce[3]);

    selection=input.nextInt();

0 个答案:

没有答案