2014-10-07 66 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] 

正如你所看到的,你就可以轻松管理您的产品清单。

希望它有帮助。

克莱门西奥莫拉莱斯卢卡斯。