Java:如何创建包含数组的对象数组?

时间:2022-10-10 12:04:11

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文件创建列表列表产品,然后在地图上显示产品。一个产品可以位于多个位置。谁知道创建结构(列表或链表)最合适的方法是什么,它是最容易迭代的,并且能够链接列表?

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!

谢谢你!

5 个解决方案

#1


3  

Why not create a Map where the keys is the product and the value is the list of Points.

为什么不创建一个映射,其中键是产品,值是点的列表。

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<Product> products = new ArrayList<Product>();
for (Product prod : products) {
  for (Point point : prod.getPoints()) {
     // construct the point and add the product info
  }
}

#2


2  

An ArrayList of Product should suffice.

一个产品的ArrayList就足够了。

But your algorithm better be Product-centric, not Point-centric.

但是你的算法最好以产品为中心,而不是以点为中心。

  1. iterate over all products
  2. 遍历所有产品
  3. iterate over all the points in the current product
  4. 迭代当前产品中的所有点。
  5. add the product info to the point
  6. 将产品信息添加到要点中

#3


1  

Any class that implements Iterable, which returns an Iterator from the Collections Framework, can be conveniently iterated using the for-each syntax, introduced in Java 1.5. Your top-level collection would be

实现Iterable(从集合框架返回迭代器)的任何类都可以使用Java 1.5中引入的for-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 List implementation as required.

通过使用接口类型List,以后可以根据需要更改为另一个List实现。

#4


0  

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.bind Marshaller and Unmarshaller interfaces.

看看javax.xml。绑定编组器和解组器接口。

#5


0  

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.

如果我理解正确,你需要在一个产品中找到所有的点,反之亦然。我认为在那种情况下你应该添加一个列表。我将使用LinkedList,如果你只是要遍历它们。

#1


3  

Why not create a Map where the keys is the product and the value is the list of Points.

为什么不创建一个映射,其中键是产品,值是点的列表。

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<Product> products = new ArrayList<Product>();
for (Product prod : products) {
  for (Point point : prod.getPoints()) {
     // construct the point and add the product info
  }
}

#2


2  

An ArrayList of Product should suffice.

一个产品的ArrayList就足够了。

But your algorithm better be Product-centric, not Point-centric.

但是你的算法最好以产品为中心,而不是以点为中心。

  1. iterate over all products
  2. 遍历所有产品
  3. iterate over all the points in the current product
  4. 迭代当前产品中的所有点。
  5. add the product info to the point
  6. 将产品信息添加到要点中

#3


1  

Any class that implements Iterable, which returns an Iterator from the Collections Framework, can be conveniently iterated using the for-each syntax, introduced in Java 1.5. Your top-level collection would be

实现Iterable(从集合框架返回迭代器)的任何类都可以使用Java 1.5中引入的for-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 List implementation as required.

通过使用接口类型List,以后可以根据需要更改为另一个List实现。

#4


0  

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.bind Marshaller and Unmarshaller interfaces.

看看javax.xml。绑定编组器和解组器接口。

#5


0  

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.

如果我理解正确,你需要在一个产品中找到所有的点,反之亦然。我认为在那种情况下你应该添加一个列表。我将使用LinkedList,如果你只是要遍历它们。