我想要添加一个基本类型到ArrayList列表中。
我是这样子写的:
import java.util.ArrayList;public class NewCollection {
ArrayList List=new ArrayList();
Integer boxedInt=new Integer();
List.add(boxedInt);
public static void main(String args[]) { System.exit(0);
}
}
编译了好几次,JAVAC老是报错: D:\test\newcollection.java:7:需要<标识符>
List.add(boxedInt);
^
1 错误
我是这样子写的:
import java.util.ArrayList;public class NewCollection {
ArrayList List=new ArrayList();
Integer boxedInt=new Integer();
List.add(boxedInt);
public static void main(String args[]) { System.exit(0);
}
}
编译了好几次,JAVAC老是报错: D:\test\newcollection.java:7:需要<标识符>
List.add(boxedInt);
^
1 错误
楼主【shallendor】截止到2008-06-24 20:00:27的历史汇总数据(不包括此帖):
发帖数:10 发帖分:270
结贴数:5 结贴分:160
未结数:5 未结分:110
结贴率:50.00 % 结分率:59.26 %
楼主加油
import java.util.ArrayList; public class NewCollection {
ArrayList List=new ArrayList();
//没有new Integer()构造方法哦
Integer boxedInt=new Integer(1);
//代码要写在方法体内
//List.add(boxedInt);
public static void main(String args[]) { System.exit(0);
}
}
ArrayList List=new ArrayList();
Integer boxedInt=new Integer();
public static void main(String args[]) {
List.add(boxedInt);
System.exit(0);
}
}
ArrayList List=new ArrayList();
{
// 如果你非得写在这里,前后加上大括号
Integer boxedInt=new Integer();
List.add(boxedInt);
}
public static void main(String args[]) { System.exit(0);
}
}
第一个:
Integer类没有构造器Integer(),只有Integer(String)和Integer(int)第二个:
List.add(boxedInt); 这句代码必须放在语句块中;语句块包括6楼和3楼的两种写法
public static void main(String args[]) {
ArrayList List=new ArrayList();
Integer boxedInt=new Integer(1);
List.add(boxedInt);
System.exit(0);
}
}
我晕啊,不写在main里面....
ArrayList List=new ArrayList();
public void add(String args[]) {
// 这里你可以定义你要加的基本数据类型对象
Integer boxedInt=new Integer(1); List.add(boxedInt);
}
}
ArrayList List=new ArrayList();
{
Integer boxedInt=new Integer(1);
List.add(boxedInt);
}
public static void main(String args[]) {
ArrayList List=new ArrayList();
{
Integer boxedInt=new Integer(1);
List.add(boxedInt);
}
for(int i=0;i<List.size();i++)
System.out.println("values is="+List.get(i));
System.exit(0);
}
}
这样的结果为:values is=1
看一下啦,如果还是认为不正确的话,可以去看一下!Integer 的定义啦!
参考: boolean\byte\short\int\long\float\double\char (8个)import java.util.ArrayList;public class NewCollection { public static void myMethod() {
ArrayList list=new ArrayList();
int boxedInt = 1;
list.add(boxedInt);
System.out.println(list.get(0));
} public static void main(String args[]) {
NewCollection.myMethod();
System.exit(0);
}
}或者import java.util.ArrayList;public class NewCollection {
public static void main(String args[]) {
ArrayList list=new ArrayList();
int boxedInt = 1;
list.add(boxedInt);
System.out.println(list.get(0));
System.exit(0);
}
}
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。这个不用解释吧。
public static void main(String[] args)
{
ArrayList<Double> arrayList =new ArrayList<Double>();
double d=Math.random()*100;
Double i=Double.valueOf(d);
arrayList.add(i);
for(int f=0;f<arrayList.size();f++)
{
System.out.println(arrayList.get(f));
}
System.exit(0);
}
}
List<Integer> list = new ArrayList<Integer>();
list.add(6); //等价于 list.add(Integer.valueOf(6));
System.out.println(list.get(0)); //等价于 System.out.println(list.get(0).toString());
//System.exit(0); //这句没必要,如果没自己开非守护线程,当 main 方法返回后程序就退出了。
}}
根据大家的指点,我重写了NewCollection.java:
import java.util.ArrayList; public class NewCollection {
ArrayList newList=new ArrayList(); {
Integer boxedInt=new Integer();
newList.add(boxedInt);
}
public static void main(String args[]) {
}
}
我改成这样子了,但还会有错误+警告: D:\test\newcollection.java:6:找不到符号
符号:构造函数Integer()
位置:类java.lang.Integer
Integer boxedInt=new Integer();
^
注意:NewCollection.java 使用了未经检查或不安全的操作。
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。 1 错误
我再换另外一个方法:
import java.util.ArrayList; public class NewCollection {
public static void main(String args[]) {
ArrayList newList=new ArrayList();
Integer boxedInt=new Integer(1);
newList.add(boxedInt);
}
} 好开心哦,少了错误。但遗憾的是还是有警告!
注意:NewCollection.java 使用了未经检查或不安全的操作。
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。
1 错误
改为:List<Integer> newList = new ArrayList<Integer>();
学习到后面这些东西自然会知道的,初学时可以忽略这个问题,不要太在乎警告。
补充一个,楼主的类名,应该List,最好是小写开头list,或是xxxList^_^