对于a来说
Scanner,读取数据是一项很好的工作。就无法像
ArrayList您一样使用集合,您将不得不自己动态地重新分配数组。
请尝试以下操作:
public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("Stock.txt")); input.useDelimiter("-|n"); Product[] products = new Product[0]; while(input.hasNext()) { int id = input.nextInt(); String department = input.next(); String name = input.next(); double price = Double.valueOf(input.next().substring(1)); int stock = input.nextInt(); Product newProduct = new Product(name, price, department, id, stock); products = addProduct(products, newProduct); } for (Product product : products) { System.out.println(product); }}private static Product[] addProduct(Product[] products, Product productToAdd) { Product[] newProducts = new Product[products.length + 1]; System.arraycopy(products, 0, newProducts, 0, products.length); newProducts[newProducts.length - 1] = productToAdd; return newProducts;}public static class Product { protected String name; protected double price; protected String department; protected int id; protected int stock; private static NumberFormat formatter = new DecimalFormat("#0.00"); public Product(String n, double p, String d, int i, int s) { name = n; price = p; department = d; id = i; stock = s; } @Override public String toString() { return String.format("ID: %drnDepartment: %srnName: %srnPrice: %srnStock: %drn", id, department, name, formatter.format(price), stock); }}结果:
ID: 0Department: BakeryName: Chocolate CakePrice: 12.50Stock: 250ID: 1Department: MeatName: Premium SteakPrice: 2.60Stock: 120ID: 2Department: SeafoodName: TunaPrice: 1.20Stock: 14



