2012-02-28 82 views
0

我试图在这里找到相关的问题。我认为这是一个常见的问题,但不幸的是我仍然无法在互联网上找到。Vector可以在一个位置存储多个数据吗?

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

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

非常感谢!

这里是我的代码:

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++;  
} 
+0

这听起来像家庭作业。提示 - 使用对象。 – 2012-02-28 09:07:01

+1

矢量<矢量>但也许有没有理由使用pre_mature阵列类型 – mKorbel 2012-02-28 09:09:10

+1

他们仍然教Vector? – aviad 2012-02-28 09:10:54

回答

0

您可以存储多个“数据” Vector位置,但不能存储多个“对象”。所以显而易见的解决方案是使用一个“对象”,而不是可以容纳多个“数据”。如果您想要的位置(纬度/经度)存储到一个单一的向量位置,创建modelates位置

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; 
    } 
} 

,然后将其存储到载体,像一类:

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

您需要创建一个可以存储一个点的三个部分,然后创建一个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的特别鼓励了,所以我会用一个ListArrayList代替:List<Point> = new ArrayList<Point>我只是看着这件事,因为我不是100%肯定,这里是一个非常有用的文章,解释何时使用它们:Vector or List

2

您可以创建自己的类的载体,像Vector<YourClass>这个类包含所有3变量是它的字段。

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

我们通常创建单独的类和那些与载体的存储实例:

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); 
相关问题