简体   繁体   English

来自不同类的对象的ArrayList

[英]ArrayList of Objects from a different class

New to Java. Java的新手。

I have a txt file: 我有一个txt文件:

  • hat, 20, 1 帽子,20,1
  • pants, 50, 45 裤子,50,45
  • shoes, 100, 10 鞋,100、10

In one class called Goods I have been able to read it, split it using delimiter, add it to an ArrayList. 在一个名为Goods的类中,我已经能够读取它,使用定界符将其拆分,然后将其添加到ArrayList中。

I have another class called GoodsList where I will be creating an ArrayList which should have the above as an object that I can use if the user requests it. 我还有一个名为GoodsList的类,我将在其中创建一个ArrayList,该数组应具有上述对象,如果用户要求,则可以使用该对象。 Thanks 谢谢

I think you are asking about java generics. 我想您是在问Java泛型。 If so, create your Goods class since you need to access it as an object via the ArrayList. 如果是这样,请创建您的Goods类,因为您需要通过ArrayList将其作为对象进行访问。

  public Class Goods implements Serializable {
  private String goodName;
  private double price;
  private int quantity;

  public String getGoodName() {
    return goodName;
  }

  public void setGoodName(String goodName) {
    this.goodName = goodName;
  }

  public double getPrice() {
    return price;
  }

  public void setPrice(double price) {
    this.price = price;
  }

  public int getQuantity() {
    return quantity;
  }

  public void setQuantity(int quantity) {
    this.quantity = quantity;
  }

}

and then Write your GoodsList class to create the list with Goods object you set: 然后编写您的GoodsList类以使用您设置的Goods对象创建列表:

public class GoodsList {

  public static void main(String args[]) {

    Goods g = new Goods();
    Goods g2 = new Goods();
    Goods g3 = new Goods();

    g.setGoodName("hat");
    g.setQuantity(50);
    g.setPrice(100.00);

    g2.setGoodName("pants");
    g2.setQuantity(50);
    g2.setPrice(100.00);

    g3.setGoodName("shoes");
    g3.setQuantity(50);
    g3.setPrice(100.00);

    List < Goods > goodsList = new ArrayList < Goods > ();
    goodsList.add(g);
    goodsList.add(g2);
    goodsList.add(g3);


    //printing goods:

    for (Goods g: goodsList) {
      System.out.println(g.getGoodName() + "," + g.getQuantity() + "," + g.getPrice());
    }

  }

}

is that what you're looking for? 那是您要找的东西吗?

Are you looking for something like: 您是否正在寻找类似的东西:

public class FileReaderExample
{


  public class Goods
  {

    private String goodName = null;
    private String price = null;
    private String quantity = null;

    public Goods(String goodName ,String price,String quantity)
    {
      this.goodName = goodName;
      this.price = price;
      this.quantity = quantity;
    }

    public String getGoodName()
    {
      return goodName;
    }

    public void setGoodName(String goodName)
    {
      this.goodName = goodName;
    }

    public String getPrice()
    {
      return price;
    }

    public void setPrice(String price)
    {
      this.price = price;
    }

    public String getQuantity()
    {
      return quantity;
    }

    public void setQuantity(String quantity)
    {
      this.quantity = quantity;
    }
  }

  private ArrayList<Goods> populateGoods()
  {
    ArrayList<Goods> goodsList = new ArrayList<Goods>();

    File file = new File("d:\\text.txt");
    try
    {
      BufferedReader br = new BufferedReader(new FileReader(file));
      String line;
      while ((line = br.readLine()) != null)
      {

          String[] itemsOnLine = line.trim().split(",");
          goodsList.add(new Goods(itemsOnLine[0],itemsOnLine[1],itemsOnLine[2]));
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return goodsList;
  }


  public static void main(String[] args)
  {
    FileReaderExample fileReaderExample = new FileReaderExample();
    ArrayList<Goods> goodsList  =  fileReaderExample.populateGoods();

    for (Goods goods : goodsList)
    {
      System.out.println(goods.getGoodName());
    }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM