import java.util.*;
public class TestList1{
public static void main(String[] args) {
List<String> allList = new ArrayList<String>();
allList.add("Hello");
allList.add(0,"World");
allList.add("Hello");
allList.add("World");
String str[] = allList.toArray(new String[]{});
//上面toArray();里的参数,new出来的数组没有指明大小,而且后面还有个大括号,这么写不太明白啊
for (int i = 0; i < str.length; i++) {
System.out.print(str[i] + "、");
}
}
}
解决方案 »
- eclipse下如何将程序打包成 可以双击执行的Jar文件
- 本人是初学java有几道题挺难的,请高手帮看一下!
- 急救~~~~~怎么用url类实现访问带session的两个页面?????????
- 新手的一个字符输出程序问题
- 今天开始自学java
- 有谁用过JCREATOR
- 简单的javaw,为何出错?
- java扫盲(1)正则表达式
- 请教??如果将.CLASS反编译为.JAVA文件??
- 请教各位大侠,在VisalAge for Java 中怎么把JRadioButton添加到ButtonGroup
- jdk写了一个helloworld,如何用JIT把它变成本地代码?
- DataOutputStream 与BufferedOutputStream
* 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;
}
//试下你就知道了
String[] qq =new String[]{};
System.out.println(qq.length);
String[] qq =new String[]{"67","ddd"};
System.out.println(qq.length);
//上面toArray();里的参数,new出来的数组没有指明大小,而且后面还有个大括号,这么写不太明白啊
这么写是指定数组的类型,看下jdk的API吧
这个才是正解!
还要注意,如果指定的数组的长度大于list的size。则后面剩余的部分全部为null。
2.#1的源代码给的很好,学习ing
你可以使用下面的代码来看看,下面的是不能转化的
import java.util.*;
public class Test{
public static void main(String[] args) {
List<Object> allList = new ArrayList<Object>();
allList.add("Hello");
allList.add(0,"World");
allList.add("Hello");
allList.add("World");
String str[] = (String[])allList.toArray();
for (int i = 0; i < str.length; i++) {
System.out.print(str[i] + "、");
}
}
}
a - 要在其中存储列表元素的数组(如果它足够大);否则,为此分配一个具有相同运行时类型的新数组。