-1

我在 Java 中有称为数据的向量的字符串向量。我可以访问这个向量元素:

(Vector) data.get(i)).get(j); 

如何获得内部向量 (i) 的计数和每个内部向量 (j) 中的元素计数。

4

2 回答 2

2

Use Vector.size() to get collection size. While this would solve your immediate problem:

int innerCount = ((Vector)data).size();
for (Object v: data) {
   int elementCount = ((Vector)v).size();
   ...
}

would suggest to use generics here to avoid casting:

int innerCount = data.size();
for (Vector v: data) {
   int elementCount = v.size();
   ...
}

Also ArrayLists are typically used now since the introduction of the Collections API in Java 2. See ArrayLists vs Vectors

于 2012-10-31T14:47:49.143 回答
1

内部向量的数量:

((Vector)data).size();

每个内部向量的元素数:

for(Vector v : (Vector)data) {
    int nrElemsInCurrentInner = v.size();
}
于 2012-10-31T14:41:16.330 回答