当前位置: 代码迷 >> J2EE >> 求教一个把list转换成数组的有关问题!
  详细解决方案

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

热度:80   发布时间:2016-04-22 02:58:19.0
求教一个把list转换成数组的问题!!!!
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的强转不是应该可以的吗?谢谢!



------解决方案--------------------
Java code
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);    }}
------解决方案--------------------
Java code
    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);        }      }
------解决方案--------------------
使用 public <T extends java/lang/Object> T[] toArray(T[]);

------解决方案--------------------
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]);
------解决方案--------------------
list.toArray(group);
------解决方案--------------------
下面是这两种方法的区别,查源码和注释就明白了
Java code
 /**     * 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;    }
  相关解决方案