正如duffymo建议,你应该考虑一个面向对象的方法。请考虑使用类似下面的例子:
import java.util.ArrayList;
public class Product {
private String name;
private double price;
private String company;
private int total;
private boolean available;
public Product(String name, double price, String company, int total,
boolean available) {
super();
this.name = name;
this.price = price;
this.company = company;
this.total = total;
this.available = available;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
@Override
public String toString() {
return "Product [name=" + name + ", price=" + price + ", company="
+ company + ", total=" + total + ", available=" + available
+ "]";
}
public static void main(String[] args) {
ArrayList<Product> products = new ArrayList<Product>();
Product product1 = new Product("PlayStation 4", 300, "Sony", 10, true);
Product product2 = new Product("XBOX One", 400, "Microsoft", 0, false);
Product product3 = new Product("WiiU", 250, "Nintendo", 5, true);
products.add(product1);
products.add(product2);
products.add(product3);
System.out.println("-- Products --");
for (Product product : products) {
System.out.println(product.toString());
}
}
}
这将产生以下的输出:
-- Products --
Product [name=PlayStation 4, price=300.0, company=Sony, total=10, available=true]
Product [name=XBOX One, price=400.0, company=Microsoft, total=0, available=false]
Product [name=WiiU, price=250.0, company=Nintendo, total=5, available=true]
正如你所看到的,你就可以轻松管理您的产品清单。
希望它有帮助。
克莱门西奥莫拉莱斯卢卡斯。
你为什么坚持使用数据结构?有很多其他方式来存储这种信息。 – unknown 2014-10-07 14:16:38
*“db中记录的位置”* - 没有这样的东西。如果您不使用ORDER BY,那么您可以按照任意顺序获取行。它通常看起来好像是按主键或插入时间排序的,但这可以随时更改(实际上,只需几次删除即可)。如果你使用'ORDER BY',比你不需要存储'Integer',因为你可以随时对行进行排序和编号。 – maaartinus 2014-10-07 16:04:13