2012-02-28 6 views
0

私はここで関連する質問を見つけようとしました。私はそれが一般的な質問だと思いますが、私はまだインターネット上で見つけることができない不運です。ベクトルは1つの位置に複数のデータを格納できますか?

ポイントには、id、lat、lonの3つの部分が含まれています。私はそれらを格納するために3つの分離ベクトルを使用しましたが、それらはお互いに関連しています。新しい点が見つかると、異なるベクトルに3回追加する必要があります。

3つのデータを3つのベクトルに分割するのではなく、1つのデータに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

これはのように聞こえます宿題。ヒントを使用するオブジェクト。 –

+1

Vector >しかし理由がないかもしれませんpre_mature配列タイプ – mKorbel

+1

彼らはまだVectorを教えていますか? – aviad

答えて

0

を複数の「データ」をVectorの位置ですが、複数の「オブジェクト」を格納することはできません。したがって、明らかな解決策は、複数の「データ」を保持できるよりも1つの「オブジェクト」を使用することです。あなたは、1つのベクトルの位置に位置(緯度/経度)を格納する場所

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

をmodelatesして、Vectorにそれを格納するクラスを作成し、同じようにしたい場合:

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の使用は、特にもはや推奨されないと思うので、私はListな代わりArrayListとして使用します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); 
関連する問題