w***y 发帖数: 6251 | 1 假如已经有了一个Arraylist p, 里面的元素其实是一些string
我想转成一个String[]
String[] y = new String[p.size()];
for ( i=0;i< p.size(); i++){
y[i] = (String)p.get(i);
}
可是执行到y[i] = (String)p.get(i);总是有 java.lang.ClassCastException
怎么回事呢?
我还试了String[] predators = (String[])al.toArray( new String[ al.size() ] );
这样的, 但是error是 java.lang.ArrayStoreException:((
thx |
s*i 发帖数: 5025 | 2 (String)(p.get(i))?
);
【在 w***y 的大作中提到】 : 假如已经有了一个Arraylist p, 里面的元素其实是一些string : 我想转成一个String[] : String[] y = new String[p.size()]; : for ( i=0;i< p.size(); i++){ : y[i] = (String)p.get(i); : } : 可是执行到y[i] = (String)p.get(i);总是有 java.lang.ClassCastException : 怎么回事呢? : 我还试了String[] predators = (String[])al.toArray( new String[ al.size() ] ); : 这样的, 但是error是 java.lang.ArrayStoreException:((
|
w***y 发帖数: 6251 | 3 I tried that kind of thing, but it didn't help:(
【在 s*i 的大作中提到】 : (String)(p.get(i))? : : );
|
Q**g 发帖数: 183 | 4 你是不是放错东西到p里头了?下面这段是work的
ArrayList p = new ArrayList();
for (int i=0;i<10;i++)
p.add(""+i);
String[] s = new String[p.size()];
for (int i=0;i< p.size(); i++){
s[i] = (String)p.get(i);
}
for (int i=0;i
System.out.println(s[i]);
【在 w***y 的大作中提到】 : 假如已经有了一个Arraylist p, 里面的元素其实是一些string : 我想转成一个String[] : String[] y = new String[p.size()]; : for ( i=0;i< p.size(); i++){ : y[i] = (String)p.get(i); : } : 可是执行到y[i] = (String)p.get(i);总是有 java.lang.ClassCastException : 怎么回事呢? : 我还试了String[] predators = (String[])al.toArray( new String[ al.size() ] ); : 这样的, 但是error是 java.lang.ArrayStoreException:((
|
xt 发帖数: 17532 | 5
唯一的解释是里面那些东西不是String或不全是
【在 w***y 的大作中提到】 : 假如已经有了一个Arraylist p, 里面的元素其实是一些string : 我想转成一个String[] : String[] y = new String[p.size()]; : for ( i=0;i< p.size(); i++){ : y[i] = (String)p.get(i); : } : 可是执行到y[i] = (String)p.get(i);总是有 java.lang.ClassCastException : 怎么回事呢? : 我还试了String[] predators = (String[])al.toArray( new String[ al.size() ] ); : 这样的, 但是error是 java.lang.ArrayStoreException:((
|
xt 发帖数: 17532 | 6
Then try p.get(i).toString(). You are guaranteed. :-)
【在 w***y 的大作中提到】 : I tried that kind of thing, but it didn't help:(
|
w***y 发帖数: 6251 | 7 got it, thx a lot:)
【在 xt 的大作中提到】 : : Then try p.get(i).toString(). You are guaranteed. :-)
|
xt 发帖数: 17532 | 8
You are welcome.
Remember this does not solve your problem, if you intend to use that
ArrayList to store String but you get sth else in it.
【在 w***y 的大作中提到】 : got it, thx a lot:)
|
c********m 发帖数: 16 | 9 p.get(i).toString() should solve the problem.
);
【在 w***y 的大作中提到】 : 假如已经有了一个Arraylist p, 里面的元素其实是一些string : 我想转成一个String[] : String[] y = new String[p.size()]; : for ( i=0;i< p.size(); i++){ : y[i] = (String)p.get(i); : } : 可是执行到y[i] = (String)p.get(i);总是有 java.lang.ClassCastException : 怎么回事呢? : 我还试了String[] predators = (String[])al.toArray( new String[ al.size() ] ); : 这样的, 但是error是 java.lang.ArrayStoreException:((
|
m**c 发帖数: 90 | 10
Try to print out "p.get(i).getClass().getName()" to debug. You don't want to
find a solution without knowing why :-)
【在 xt 的大作中提到】 : : You are welcome. : Remember this does not solve your problem, if you intend to use that : ArrayList to store String but you get sth else in it.
|
|
|
xt 发帖数: 17532 | 11
to
hm... we call that a surprise feature.
【在 m**c 的大作中提到】 : : Try to print out "p.get(i).getClass().getName()" to debug. You don't want to : find a solution without knowing why :-)
|
c**g 发帖数: 274 | 12 String[] predators = al.toArray(new String[0]);
【在 w***y 的大作中提到】 : 假如已经有了一个Arraylist p, 里面的元素其实是一些string : 我想转成一个String[] : String[] y = new String[p.size()]; : for ( i=0;i< p.size(); i++){ : y[i] = (String)p.get(i); : } : 可是执行到y[i] = (String)p.get(i);总是有 java.lang.ClassCastException : 怎么回事呢? : 我还试了String[] predators = (String[])al.toArray( new String[ al.size() ] ); : 这样的, 但是error是 java.lang.ArrayStoreException:((
|
m******t 发帖数: 2416 | 13
This has got to be the worst solution I have ever seen _you_ giving on this
board, man. 8-)
【在 xt 的大作中提到】 : : to : hm... we call that a surprise feature.
|
xt 发帖数: 17532 | 14
Come on, woomy was so desparate so I gave out this horrible stuff to let it
at least run. :-)
Then he can find out what is going wrong by looking at the strings.
【在 m******t 的大作中提到】 : : This has got to be the worst solution I have ever seen _you_ giving on this : board, man. 8-)
|
m******t 发帖数: 2416 | 15
this
Right, that's the part I rarely see people do once they got around a problem.
8-)
【在 xt 的大作中提到】 : : Come on, woomy was so desparate so I gave out this horrible stuff to let it : at least run. :-) : Then he can find out what is going wrong by looking at the strings.
|