V
vhqd
hello, my trouble is exampled in the code. Please take a look and give
me some threads. thanks.
TestArray.java
//-----------------------------------------------------
import java.util.Vector;
public class TestArray {
public static void main(String[] agrs) {
// I only want to get an Array from the Vector v
Vector v = SomeClass.getVector();
// I can make sure the object type is correct
// I have checked every object in v is instanceof SomeClass
// [method1] and [method2] both throw
ClassCastException
SomeClass[] sc1 = (SomeClass[]) v.toArray(); // [method 1]
SomeClass[] sc2 = (SomeClass[]) toArray2(v); // [method 2]
// I have to code like this, and no exception any more.
Why?
SomeClass[] sc3 = new EmMessage[v.size()]; // [method 3]
for (int i = 0; i < v.size(); i++) {
sc4 = (SomeClass) v.elementAt(i);
}
}
public static Object[] toArray2(Vector vector) {
if (vector == null) {
return new Object[0];
}
Object[] obs = new Object[vector.size()];
for (int i = 0; i < obs.length; i++) {
obs = vector.elementAt(i);
}
return obs;
}
}
me some threads. thanks.
TestArray.java
//-----------------------------------------------------
import java.util.Vector;
public class TestArray {
public static void main(String[] agrs) {
// I only want to get an Array from the Vector v
Vector v = SomeClass.getVector();
// I can make sure the object type is correct
// I have checked every object in v is instanceof SomeClass
// [method1] and [method2] both throw
ClassCastException
SomeClass[] sc1 = (SomeClass[]) v.toArray(); // [method 1]
SomeClass[] sc2 = (SomeClass[]) toArray2(v); // [method 2]
// I have to code like this, and no exception any more.
Why?
SomeClass[] sc3 = new EmMessage[v.size()]; // [method 3]
for (int i = 0; i < v.size(); i++) {
sc4 = (SomeClass) v.elementAt(i);
}
}
public static Object[] toArray2(Vector vector) {
if (vector == null) {
return new Object[0];
}
Object[] obs = new Object[vector.size()];
for (int i = 0; i < obs.length; i++) {
obs = vector.elementAt(i);
}
return obs;
}
}