正如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