我尝试在这里找到相关问题。我认为这是一个常见的问题,但不幸的是我仍然无法在互联网上找到。
一个点包含 3 个部分,id、lat 和 lon。我使用了 3 个独立的 vector 来存储它们,但它们彼此相关。当找到一个新点时,它必须在不同的 vector 中添加 3 次...
我想将 3 个数据添加到一个 vector 中,而不是 3 个独立的 vector 。 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,例如 Vector<YourClass>
这个类保存了所有 3 个变量,即它的字段。
public class YourClass {
String id;
double lat;
double lon;
}
关于java - Vector 可以在一个位置存储多个数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9479007/