2014-10-07 78 views
0

我對如何選擇我的數據結構感到困惑。可以說,我有以下數據 產品,價格,公司,總可用..我從數據庫中獲得。現在我想表達一下,比如讓我說excel或csv的順序與我從db公司獲得的明智順序相同。 所以我選擇了下面的數據結構。設計數據結構/ Java數據結構

Map<String, TreeMap<Integer, TreeMap<String, String>>> . 

第一串表示公司 整數表示分貝記錄的位置,這樣我可以在相同的順序顯示。 TreeMap包含其他值。

我可以爲這個需求選擇更好的數據結構嗎?

+0

你爲什麼堅持使用數據結構?有很多其他方式來存儲這種信息。 – unknown 2014-10-07 14:16:38

+1

*「db中記錄的位置」* - 沒有這樣的東西。如果您不使用ORDER BY,那麼您可以按照任意順序獲取行。它通常看起來好像是按主鍵或插入時間排序的,但這可以隨時更改(實際上,只需幾次刪除即可)。如果你使用'ORDER BY',比你不需要存儲'Integer',因爲你可以隨時對行進行排序和編號。 – maaartinus 2014-10-07 16:04:13

回答

3

是的,絕對。

更好的解決方案將是面向對象:

public class Product { 
    private String name; 
    private String company; 
    private Money total; 
    private boolean available; 
    // Add necessary methods. 
} 

該數據結構將是一個List<Product>

你的方式太原始了。

1

傳統的數據結構遵循結構化編程範例。面向對象程序設計從根本上解決了結構化程序設計問題,但增加了行爲局部性的概念。簡而言之,數據不僅僅是集中的,而且數據的行爲(方法)是集中的。

這使得數據隱藏(用於維護有用的,因爲正確的數據格式隨着時間推移而改變),並打開大門,其他更高級的行爲(因爲該行爲是本地化polymorphisim是可能的)。然而,對純粹的遊戲數據結構方法來說,這並沒有太大的作用。我們最接近老派的數據結構是代表它們的對象。

當選擇一個數據結構,如果你真的沒有什麼是很重要的一個想法,你真的沒有這將讓你在另一個選擇一個數據結構的標準。當然,你可以只始終使用HashMapHashSet,這將是罰款大量的時間;但是,這些選擇可能是最糟糕的選擇。總之,您需要知道訪問模式才能做出正確的選擇。

0

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

正如你所看到的,你就可以輕鬆管理您的產品清單。

希望它有幫助。

克萊門西奧莫拉萊斯盧卡斯。