打印出数组列表

时间:2013-11-23 01:38:41

标签: java arrays eclipse

我搞砸了试图打印出这个数组列表我做错了什么我已经尝试了多次但是当它打印时它输出null。

 import java.util.ArrayList;
 import java.util.Arrays;
 import java.text.NumberFormat;
 import java.util.Scanner;

 //Class header

 public class ShoppingCart {

     // Start of main method

     public static <Item> void main(String[] args) {

         // Declare and instantiate a variable that is an ArrayList that can hold
         // Product objects

         ArrayList<Product> item = new ArrayList<Product>();

         // Declare necessary local variables here

         String Name = null;
         double Price = 0;
         int Quantity = 0;
         String Seller = null;

         Scanner scan = new Scanner(System.in);

         String Shop = "yes";

         // Write a print statement that welcome's the customer to our shop

         /**
          *
          * create a do while that will be keep looping as long as user wants to
          * continue shopping
          */

         Product im = new Product(Name, Price, Quantity, Seller);

         // do while loop start

         do {

             // Ask user to enter product name and store it in appropriate local
             // variable

             System.out.print("Please Enter the Product Name: ");

             Name = scan.next();

             // Ask user to enter product price and store it in appropriate local
             // variable

             System.out.print("Please Enter the Price of the Product: ");

             Price = scan.nextDouble();

             // Ask user to enter quantity and store it in appropriate local
             // variable

             System.out.print("Please enter the Quantity: ");

             Quantity = scan.nextInt();

             // Ask user to enter product manufacturer name and store it in
             // appropriate local variable

             System.out.print("Please Enter the Manufacturer: ");

             Seller = scan.next();

             System.out.print("Would you like to continue shopping?");

             Shop = scan.next();

             // create a new Product object using above inputed values

             Product item2 = new Product(Name, Price, Quantity, Seller);

             // add above created Product to the ArrayList cart if Product has
             // available stock

             // if stock not available inform user requested product is out of
             // stock

             // Ask user whether wants to continue shopping

             // set the do while loop to continue to loop if Yes option is
             // selected

         } while (Shop.equals("Yes"));

 System.out.println("Product                     Unit Price         Quantity         SubTotal");
 System.out.println(Name + "                " +     (Price) + "     " + Quantity + "         " + (Price * Quantity));
 System.out.println("00"); System.out.println(item);

 // do while loop end
             // header for shopping cart contents

 // print details of each product added to the ArrayList

 // calculate total price of the shopping cart

 // print the total price of the shopping cart


     }// end of main method

      }// end of Shop class

3 个答案:

答案 0 :(得分:0)

首先,你为什么要这样做

public static <Item> void main(String[] args) {

应该

public static void main(String[] args) {

要打印ArrayLIst,只需执行此操作

System.out.println("Name\tPrice\tQuantity\tSeller\tTotal")
for (Product p : item){
    double total = p.price * p.quantity;
    System.out.println(p.name + "\t" + p.price + "\t" p.quantity + "\t" + p.seller + "\t" + total);
}

但首先你需要在列表中添加一些东西

Product item2 = new Product(Name, Price, Quantity, Seller);
item.add(item2);

或者更清晰的想法是覆盖the toString类中的Product方法

public class Product {
    String name;
    String Seller;
    double price;
    int quantity;

    public Product(String name, double price, int quantity, String seller){
        this.name = name;
        this.seller = seller;
        this.price = price;
        this.seller = seller;
    }

    public String toString(){
        double total = p.price * p.quantity;
        return name + "\t" + price + "\t" quantity + "\t" + seller + "\t" + total;
    }
}

要从列表中打印,只需执行此操作。

for (Product p : item){
    System.out.println(p);
}

答案 1 :(得分:0)

因为你从不将item2添加到arraylist项目 只需添加

item.add(item2)

答案 2 :(得分:0)

您的计划中存在相当多的错误。

首先,当您尝试打印arraylist时没有打印任何内容的原因是因为您从未向数组列表添加任何元素,为此您必须使用以下方法。

arrayListName.add(elementToAdd);

其次你的主要方法不正确,而不是

public static <Item> void main(String[] args) {

应该是

public static void main (String[] args) {