Java中定义数组,如果定义时不知道长度,该怎么办? 好像java中定义数组,一定要先指明数组长度,但有时定义时,还不知道数组长度的,那怎么办呢?谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 int arr[];String[] str;......都可以··· 楼上的这个只能算“声明”,不能算“定义”,因为这样并没有创建真正的数组对象。我印象里 Java 的数组是不能动态改长度的。如果要使用“动态数组”,那就用 List 好啦,比如 ArrayList。 用Vector Vector temp = new Vector();temp.addElement(....);添加元素(变量,值等)temp.size();取得大小;String xx = temp.toString();转换为字符串;String[] yy = new String[temp.size()];yy = xx.split(",");转换为数组; 例如:String test=null;String str="a,b,c,d";If(1=1){ test=str.split(",");} 一般情况下:俺是喜欢这样List list = new ArrayList(0);for (...){ list.add(...);}..[] = list.toArray(...); 用集合一般使用ArrayList 如果牵扯到多线程的话用Vector 还是用List如果你实在要数组,那就从集合转:String[] str=(String[])list.toArray(new String[0]); 请问JTextField跟KeyListener相关问题. Exception in thread "main" java.lang.NullPointerException at Server.main(Server.java:22) java处理visio的问题 谁能推荐几本关于算法的书 有2个简单问题,请大家帮忙~~~ Jtalbe疑惑 JAVA程序间隔自动运行的问题 JDK问题:编译.java文件时为何总报错,不能读文件? 编译成功后的CODE为什么回有这样的提示! java写一个字符串排序 求:java3d电子版教程 MLet的问题
String[] str;
......
都可以···
Vector temp = new Vector();
temp.addElement(....);添加元素(变量,值等)
temp.size();取得大小;String xx = temp.toString();转换为字符串;
String[] yy = new String[temp.size()];
yy = xx.split(",");转换为数组;
String test=null;
String str="a,b,c,d";
If(1=1)
{
test=str.split(",");
}
俺是喜欢这样List list = new ArrayList(0);for (...)
{
list.add(...);
}..[] = list.toArray(...);
一般使用ArrayList 如果牵扯到多线程的话用Vector
如果你实在要数组,那就从集合转:
String[] str=(String[])list.toArray(new String[0]);