Java新手。来自不同类的对象ArrayList
我有一个txt文件:
- 帽子,20,1个
- 裤,50%,45轮
- 鞋,100,10
在一类被称为商品我有能够读取它,使用分隔符分割它,将它添加到一个ArrayList。
我有另一个名为GoodsList的类,我将创建一个ArrayList,它应该具有上述的对象,如果用户请求它可以使用它。谢谢
Java新手。来自不同类的对象ArrayList
我有一个txt文件:
在一类被称为商品我有能够读取它,使用分隔符分割它,将它添加到一个ArrayList。
我有另一个名为GoodsList的类,我将创建一个ArrayList,它应该具有上述的对象,如果用户请求它可以使用它。谢谢
我想你是在问关于Java泛型。 如果是这样,请创建您的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;
}
}
,然后写您的商品详细资料类创建列表与商品对象设置:
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());
}
}
}
的是,你在找什么?
这绝对是指向正确的方向。谢谢Nomesh。我会尝试这个,并在此添加其他相关问题。 – Tarinkot
太棒了,请接受它作为答案,如果你能做到的话! –
你是否在寻找类似:
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());
}
}
}
而你的问题是什么? –
您是否问过'Arraylist goodsList = new Arraylist ()'这样的泛型,然后在* hat,20,1 *上使用'String.split()'来创建一个新的'Goods'实例并添加它的名单? –
首先使用bufferReade通过使用readLine()方法读取文本文件中的行。之后,通过使用','作为分隔符来分割行。使用Arrays.asList(string array)方法将该字符串[]数组添加到arraylist。 –