如何从csv添加和打印对象到arrayList

时间:2018-05-09 13:15:38

标签: java csv object arraylist

我尝试将从CSV文件中读取的产品添加到ArrayList,并为每个产品创建一个对象。但是当我尝试使用for循环打印数组主义时,我不断获得最后一个元素,在这种情况下我得到三次电视

我的csv文件:

汽车,20000

表,150

电视,250

我的产品类:

entitlement. **Xcode 8** does not automatically copy the aps-environment entitlement from provisioning profiles at build time. This behavior is intentional. To use this entitlement, either enable

我的主要:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class product {
    private static String Name;
    private static double Price;
    public static List<product> List_product = new ArrayList<>();

    public static void readPrice() throws IOException {

        FileReader info = new FileReader("info.csv");
        BufferedReader reader = new BufferedReader(info);
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            String[] attributes = line.split(",");      
            double price = Double.parseDouble(attributes[1]);
            List_product.add(new product(attributes[0], price));
        }
        reader.close(); 
    }

    public product(String Name, double Price) {
        product.Name = Name;
        product.Price = Price;
    }

    public String getName() {
        return Name;
    }

    public double getPrice() {
        return Price;
    }

    public static List<product> getList() { 
        return List_product;
    }
}

2 个答案:

答案 0 :(得分:0)

    Try This,
    Main.java
        try {
        Product.readPrice();
        for (Product product : Product.getList()) {
        System.out.println(product.getName()+"--"+product.getPrice());
        }
        }catch (IOException e) {
        }

       public class Product {    
       private String name;    
       private double price;    
       public static List<Product> listProduct = new ArrayList<>();    
       public static void readPrice() throws IOException {    
       FileReader info = new FileReader("info.csv");    
       BufferedReader reader = new BufferedReader(info);    
       while (true) {    
       String line = reader.readLine();    
       if (line == null) {    
       break;    
       }    
       String[] attributes = line.split(",");    
       double price = Double.parseDouble(attributes[1]);        
       listProduct.add(new Product(attributes[0], price));    
       }    
       reader.close();    
       }    
       public Product(String name, double price) {    
       this.name = name;    
       this.price = price;    
       }    
       public String getName() {    
       return name;    
       }    
       public double getPrice() {    
       return price;    
       }    
       public static List<Product> getList() {    
       return listProduct;    
       }    
       }    

答案 1 :(得分:0)

整个问题是您使用静态变量:

private static String Name;
private static double Price;

它们属于类,而不属于您创建的对象。当您创建最后一个对象时 - 它会覆盖其他对象。

因此,正确完成任务的方法是更改​​产品类(请使用大写,因为它是命名类的代码约定)

class Product {
    private String name;
    private double price;
    public static List<Product> List_product = new ArrayList<>();

    public Product(String name, double price) {
       this.name = name;
       this.price = price;
    }
    //...other code is fine

}