我想要添加一个基本类型到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 错误

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【shallendor】截止到2008-06-24 20:00:27的历史汇总数据(不包括此帖):
    发帖数:10                 发帖分:270                
    结贴数:5                  结贴分:160                
    未结数:5                  未结分:110                
    结贴率:50.00 %            结分率:59.26 %            
    楼主加油
      

  2.   


    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); 
      } 

      

  3.   

    List.add(boxedInt); 这样的语句要写在方法或构造方法中的
      

  4.   

    import java.util.ArrayList;public class NewCollection {
       
        ArrayList List=new ArrayList();
        Integer boxedInt=new Integer();
        
       
        public static void main(String args[]) {
    List.add(boxedInt);
        System.exit(0);
      }
      

  5.   

    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); 
      } 
      

  6.   

    我想你这段代码存在两个问题吧,
    第一个:
        Integer类没有构造器Integer(),只有Integer(String)和Integer(int)第二个:
        List.add(boxedInt); 这句代码必须放在语句块中;语句块包括6楼和3楼的两种写法
      

  7.   

    import java.util.ArrayList; public class csdntest3 {    
      public static void main(String args[]) { 
        ArrayList List=new ArrayList();
        Integer boxedInt=new Integer(1);
        List.add(boxedInt);
        System.exit(0); 
      } 
    }
    我晕啊,不写在main里面....
      

  8.   

    import java.util.ArrayList; public class NewCollection { 
       ArrayList List=new ArrayList(); 
       public  void add(String args[]) {  
    //      这里你可以定义你要加的基本数据类型对象
            Integer boxedInt=new Integer(1);          List.add(boxedInt); 
      } 
      

  9.   

    import java.util.ArrayList; public class NewC { 
        
        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 的定义啦!
      

  10.   

    D:\test\newcollection.java:7:需要 <标识符> 请你把文件名改大写。我想要添加一个基本类型到ArrayList列表中 什么叫基本类型?
      参考: 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);
        }
    }
      

  11.   

    不过,在编译的时候,会有如下警告:注意:NewCollection.java 使用了未经检查或不安全的操作。
    注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。这个不用解释吧。
      

  12.   

    package com.suypower.listDemo;import java.util.ArrayList;public class ArrayListDemo {
    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);
    }
    }
      

  13.   

    你引用列表的对象变量名起的很危险,List 是 java.util 包中的一个接口的名字,而你用的 ArrayList 就实现了这个接口,起名要注意。import java.util.*;public class NewCollection {    public static void main(String[] args) {  //[] 放 String 后在 Java 中更提倡
            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 方法返回后程序就退出了。
        }}
      

  14.   

    真是不好意思,麻烦啦.刚初识JAVA就遇上这么多的问题。
    根据大家的指点,我重写了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 错误
      

  15.   

        该警告是因为在List的对象声明时没有为其里面装的对象指定类型,
         改为:List<Integer> newList = new ArrayList<Integer>();
        学习到后面这些东西自然会知道的,初学时可以忽略这个问题,不要太在乎警告。
      

  16.   


    补充一个,楼主的类名,应该List,最好是小写开头list,或是xxxList^_^