Java:如何创建包含数组的对象数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1989364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: How to create an array of objects which include arrays?
提问by Niko Gamulin
I would like to create a list objects called Product (Listproducts) where Product includes the following members:
我想创建一个名为 Product (Listproducts) 的列表对象,其中 Product 包括以下成员:
public class Product {
private Double price;
private String title;
private String description;
private List<Point>points;
...
}
Point is another object which includes latitude and longitude:
Point 是另一个包含纬度和经度的对象:
public class Point{
Double lat;
Double lng;
...
}
In the application I first have to create the list List products from XML file and then display the products on the map. One product can be located at several locations. Does anyone know what is the most appropriate way to create the structure (a list or linked list) which is the easiest to iterate through and enables to link the lists?
在应用程序中,我首先必须从 XML 文件创建列表 List products,然后在地图上显示产品。一种产品可以位于多个位置。有谁知道创建最容易迭代并能够链接列表的结构(列表或链接列表)的最合适方法是什么?
The logic should work the following way: 1. Read each point from the list 2. Check which product belongs to the point 3. Display the product informations on the map
该逻辑应按以下方式工作: 1. 从列表中读取每个点 2. 检查哪个产品属于该点 3. 在地图上显示产品信息
Thank you!
谢谢!
回答by digitalsanctum
Why not create a Map where the keys is the product and the value is the list of Points.
为什么不创建一个 Map,其中键是产品,值是点列表。
Map<Product, List<Point>> productPoints = new HashMap<Product, List<Point>>();
for (Product prod: productPoints) {
for (Point point : productPoints.get(prod)) {
// construct the point and add the product info to the point
}
}
This also allows you to easily filter the points on the map by product if necessary.
这还允许您在必要时按产品轻松过滤地图上的点。
Or use the List interface as suggested above so you can later change the implementation with minimal code changes.
或者使用上面建议的 List 接口,以便您以后可以通过最少的代码更改来更改实现。
List<Product> products = new ArrayList<Product>();
for (Product prod : products) {
for (Point point : prod.getPoints()) {
// construct the point and add the product info
}
}
回答by Bozho
An ArrayListof Productshould suffice.
一个ArrayList的Product就足够了。
But your algorithm better be Product-centric, not Point-centric.
但是你的算法最好以-为Product中心,而不是-为Point中心。
- iterate over all products
- iterate over all the points in the current product
- add the product info to the point
- 迭代所有产品
- 迭代当前产品中的所有点
- 将产品信息添加到点
回答by trashgod
Any class that implements Iterable, which returns an Iteratorfrom the Collections Framework, can be conveniently iterated using the for-eachsyntax, introduced in Java 1.5. Your top-level collection would be
任何实现从集合框架Iterable返回 的类都可以使用Java 1.5 中引入的语法方便地迭代。您的顶级收藏将是Iteratorfor-each
List<Product> products = new ArrayList<Product>();
An you could iterate it like so:
你可以像这样迭代它:
for (Product p : products) { ... }
By using the interface type, List, you can later change to another Listimplementation as required.
通过使用接口类型 ,List您可以稍后List根据需要更改为另一个实现。
回答by duffymo
Either of these is perfectly okay:
其中任何一个都没有问题:
List<Product> products = new ArrayList<Product>();
or
或者
int numProducts = DEFAULT_VALUE; // Have to know this in advance.
Product [] products = new Product[numProducts];
What you're really asking here is "How do I bind XML to Java objects?"
您在这里真正要问的是“我如何将 XML 绑定到 Java 对象?”
Have a look at the javax.xml.bindMarshaller and Unmarshaller interfaces.
查看javax.xml.bindMarshaller 和 Unmarshaller 接口。
回答by ZoFreX
If I understand you correctly, you need to be able to find all Points given a Product, and vice-versa? I think you should add a List to Point in that case. I would use a LinkedList if you are just going to be iterating through them.
如果我理解正确,您需要能够找到给定产品的所有 Points,反之亦然?我认为在这种情况下您应该向 Point 添加一个 List 。如果您只是要遍历它们,我会使用 LinkedList。

