求教一个把list转换成数组的问题!!!!

tikyo772 2011-09-22 04:18:09
public class ListToArrayTest {
public static void main(String args[]) {
ArrayList<String> list = new ArrayList<String>();
String group[];
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");

// 1
// Object[] temp;
// temp = list.toArray();
// System.out.println(temp.length);
// group=new String[temp.length];
// for(int i=0;i<temp.length;i++){
// group[i]=(String) temp[i];
// }
//
// 2
group =new String[list.size()];
group= (String[]) list.toArray();

//Object to String


System.out.println("group size is "+group.length);
}
}


报错的是:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
at ListToArrayTest.main(ListToArrayTest.java:23)


oboject的强转不是应该可以的吗?谢谢!

...全文
127 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaojinhua1988 2011-09-22
  • 打赏
  • 举报
回复
学习哈
风尘中国 2011-09-22
  • 打赏
  • 举报
回复
参照这样写才对
public class ListToArrayTest {
public static void main(String args[]) {
ArrayList<String> list = new ArrayList<String>();
String group[];
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");


group =new String[list.size()];
group= list.toArray(group);



System.out.println("group size is "+group.length);
for(String s:group){
System.out.println(s);
}
}
}
shiyiwan 2011-09-22
  • 打赏
  • 举报
回复
下面是这两种方法的区别,查源码和注释就明白了
 /**
* Returns an array containing all of the elements in this list
* in proper sequence (from first to last element).
*
* <p>The returned array will be "safe" in that no references to it are
* maintained by this list. (In other words, this method must allocate
* a new array). The caller is thus free to modify the returned array.
*
* <p>This method acts as bridge between array-based and collection-based
* APIs.
*
* @return an array containing all of the elements in this list in
* proper sequence
*/
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}

/**
* Returns an array containing all of the elements in this list in proper
* sequence (from first to last element); the runtime type of the returned
* array is that of the specified array. If the list fits in the
* specified array, it is returned therein. Otherwise, a new array is
* allocated with the runtime type of the specified array and the size of
* this list.
*
* <p>If the list fits in the specified array with room to spare
* (i.e., the array has more elements than the list), the element in
* the array immediately following the end of the collection is set to
* <tt>null</tt>. (This is useful in determining the length of the
* list <i>only</i> if the caller knows that the list does not contain
* any null elements.)
*
* @param a the array into which the elements of the list are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose.
* @return an array containing the elements of the list
* @throws ArrayStoreException if the runtime type of the specified array
* is not a supertype of the runtime type of every element in
* this list
* @throws NullPointerException if the specified array is null
*/
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
softroad 2011-09-22
  • 打赏
  • 举报
回复
list.toArray(group);
brightyq 2011-09-22
  • 打赏
  • 举报
回复
List list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");

int size = list.size();
String[] str = (String[])list.toArray(new String[size]);
huntor 2011-09-22
  • 打赏
  • 举报
回复
使用 public <T extends java/lang/Object> T[] toArray(T[]);
台城柳月 2011-09-22
  • 打赏
  • 举报
回复


public static void main(String args[]) {
ArrayList<String> list = new ArrayList<String>();
String group[];
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
String[] arr = new String[list.size()];
list.toArray(arr);


for (String s : arr)
{
System.out.println(s);
}
}

shiyiwan 2011-09-22
  • 打赏
  • 举报
回复
import java.util.ArrayList;

public class ListToArrayTest {
public static void main(String args[]) {
ArrayList<String> list = new ArrayList<String>();
String group[];
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");

// 1
// Object[] temp;
// temp = list.toArray();
// System.out.println(temp.length);
// group=new String[temp.length];
// for(int i=0;i<temp.length;i++){
// group[i]=(String) temp[i];
// }
//
// 2
group = new String[list.size()];
list.toArray(group);

// Object to String

System.out.println("group size is " + group.length);
for(String s: list)
System.out.println(s);
}
}

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧