Redian新闻
>
我想把一个Arraylist转成String[]
avatar
我想把一个Arraylist转成String[]# Java - 爪哇娇娃
w*y
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
avatar
s*i
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:((

avatar
w*y
3
I tried that kind of thing, but it didn't help:(

【在 s*i 的大作中提到】
: (String)(p.get(i))?
:
: );

avatar
Q*g
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;iSystem.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:((

avatar
xt
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:((

avatar
xt
6

Then try p.get(i).toString(). You are guaranteed. :-)

【在 w***y 的大作中提到】
: I tried that kind of thing, but it didn't help:(
avatar
w*y
7
got it, thx a lot:)

【在 xt 的大作中提到】
:
: Then try p.get(i).toString(). You are guaranteed. :-)

avatar
xt
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:)
avatar
c*m
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:((

avatar
m*c
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.

avatar
xt
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 :-)

avatar
c*g
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:((

avatar
m*t
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.

avatar
xt
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-)

avatar
m*t
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。