繁体   English   中英

Vector可以在一个位置存储多个数据吗?

[英]Can a Vector store more than one data in one position?

我试图在这里找到相关的问题。 我认为这是一个常见的问题但不幸的是我仍然无法在互联网上找到。

一个点包含3个部分,id,lat和lon。 我使用了3个分离的向量来存储它们,但它们彼此相关。 当找到新点时,它必须在不同的向量中添加3次...

我想将3个数据添加到一个向量而不是3个分离的向量中。 Vector可以做到吗? 或任何其他简单的方法达到我的目标?

非常感谢 !

这是我的代码:

public class Try01 {
  static Vector<String> id = new Vector<String>();
  static Vector<Double> lat = new Vector<Double>();
  static Vector<Double> lon = new Vector<Double>();

public static void main( String[] args ) throws Exception {
// create an input source for target document and parse it

  int counter=0;

  DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
  Document doc = docBuilder.parse (new File("data.xml"));

  // get all tags in the document with the name link
  NodeList links = doc.getElementsByTagName( "node" );

  for( int i = 0; i < links.getLength(); i++ ) {      
  Element link = (Element) links.item( i );
  //add part
  id.add(link.getAttribute( "id" ));
  lat.add( Double.parseDouble(link.getAttribute( "lat" )) );
  lon.add( Double.parseDouble(link.getAttribute( "lon" )) );

  //checking point: show the vector
  System.out.println( counter + " ) Vector = " + id.get(counter) + " and " + lat.get(counter) + " with " + lon.get(counter));

  counter++;     
}

你可以创建一个你自己的类的Vector<YourClass> ,比如Vector<YourClass> ,这个类包含所有3个变量都是它的字段。

public class YourClass {
  String id;
  double lat;
  double lon;
}

您需要创建一个可以存储点的三个部分的类,然后创建一个Vector<Point>来保存这些对象的集合。

就像是:

public class Point {
  private String id;
  private Double latitude;
  private Double longitude;

  //constructor, getters and setters, and rest of the class omitted
}

另外,我认为不再使用Vector s,所以我会使用List这样的ArrayListList<Point> = new ArrayList<Point> 我只是看了这个,因为我不是100%肯定,这是一篇有用的文章,解释了何时使用每个: 矢量或列表

我们通常创建单独的类并将它们的实例存储到向量:

public class Coordinate {
   public String id;
   public double lat;
   public double lon;
}

然后:

static Vector<Coordinate> coordinates = new Vector<Coordinate>();

并在方法中:

Element link = (Element) links.item( i );
//add part
Coordinate coord = new Coordinate();
coord.id = link.getAttribute( "id" );
coord.lat = Double.parseDouble(link.getAttribute( "lat" ));
coord.lon = Double.parseDouble(link.getAttribute( "lon" ));
coordinates.add(coord);

您可以将多个“数据”存储到Vector位置,但不能存储多个“对象”。 因此,显而易见的解决方案是使用一个“对象”而不是可以容纳多个“数据”。 如果要将位置(纬度/经度)存储到一个Vector位置,请创建一个用于调节位置的类

class Location
{
    private String id = null;
    private double latitude = 0.0;
    private double longitude = 0.0;

    public Location(String id, double latitude, double longitude)
    {
        this.id = id;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getId()
    {
        return id;
    }

    public double getLongitude()
    {
        return longitude;
    }

    public void setLongitude(double longitude)
    {
        this.longitude = longitude
    }

    public double getLatitude()
    {
        return longitude;
    }

    public void setLatitude(double latitude)
    {
        this.latitude = latitude;
    }
}

然后将其存储到Vector中,如:

double lat, lon;
Vector<Location> vlocations = new Vector<Location>();
vlocations.add(new Location(lat, lon));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM