【问题标题】:Explicitly setting NULL value to an array element将 NULL 值显式设置为数组元素
【发布时间】:2013-08-18 07:35:07
【问题描述】:

有人能告诉我为什么将NULL 值显式设置为数组元素会出现编译错误吗?

int[] a = new int[5];

a[0] = 1;
a[2] = 'a';
a[3] = null; //Compiler complains here


for (int i : a) System.out.println(i);

我假设是因为它是一个 int 数组,并且允许的文字值是 0 而不是 NULL。我说的对吗?

【问题讨论】:

  • 这是因为 int 是一个原语。应该使用“整数”代替 int
  • 提示:如果你写了int x = null;,你期望会发生什么?如果您了解为什么不允许这样做,那么数组大小写完全相同。数组只是你可以通过索引访问的大量变量。

标签: java


【解决方案1】:

正确。 int 是原始类型,这意味着它包含一个显式值(从 -2^31 到 2^31-1 的数字),而不是引用,因此它不能是 null。如果您确实需要 null 值,请使用 Integer

【讨论】:

    【解决方案2】:

    你的数组

    int[] a
    

    是原始类型int。基元不能具有null 值,而是具有默认值0。只有Java 中的对象才能具有空值作为value。原语包括:byte,short,char,int,long,float,double.

    【讨论】:

      【解决方案3】:

      我假设是因为它是一个 int 数组,并且允许的字面值是 0 而不是 NULL。我说的对吗?

      是的。

      如果您希望能够使用null,请将其设为Integer[]Integers 是对象,可以设置为 null,与原语(intchar 等)不同。

      【讨论】:

        【解决方案4】:

        intprimitive 类型,它不能是 null - 它必须有一个值。

        唯一的选择是将其设置为您可以对待的值,例如“null”,例如-1

        【讨论】:

        • 或者改用Integer
        【解决方案5】:
             int[] a = new int[5];
        

        声明的数组是 int 类型,它是原始类型,如 byte、short、long、float、double、char 和 boolean。基元不能有空值,而是有下面给出的默认值

            byte = 0; 
            short = 0;
            int = 0;
            long = 0l;
            float = 0.0f
            double = 0.0d
            boolean = false;
        

        原始类型的范围公式如下所述。基元只能接受该范围内的值。

            -2^(N - 1) to 2^(N - 1)-1 
            where N stands for no. of bits that each primitive type takes
        

        只有 java 中的对象才能有 null 作为值。如果无论如何你想要这样,最好使用像

        这样的包装类
            Byte, Short, Integer, Long, Character, Boolean, etc.
        

        【讨论】:

          【解决方案6】:

          将数组声明为: 整数[] a = 新整数[5];

          其余的应该按原样工作。

          【讨论】:

            【解决方案7】:

            这是一个int 数组。 int 的默认值不为空,默认值为0,如果这是Integer 数组,则可以设置null

            【讨论】:

              【解决方案8】:

              整数数组元素是值类型(Int 类型),因此它们将分配给它们的值存储在内存位置中。如果你想伪造一个空值,你可以尝试为元素分配一个 -1 值。

              试试这样的...

              public static void main(String[]args){
                      int a[][]={{4,3,2,1},{0,-1,-1,-1},{0,-1,-1,-1}};
                      printStatus(a);
                      procesar(a);
                      printStatus(a);
                  }
              
                  public static void procesar (int a[][])
              {
              int temp, tope0, tope1, tope2;
              tope0 = ((a[0].length)-1);
              tope1 = 0;
              tope2 = 0;
              
              while(a[0][tope0] >= 0){
              
                  if(a[2][tope2]==0){
                      System.out.println(a[2][tope2]);
                      temp=a[0][tope0];
                      a[2][tope2] = temp;
                      a[0][tope0] = 0;    
                      tope0= tope0 -1;    
                      tope2 = tope2 +1 ;
                      System.out.println(a[0][tope0]+ " "+a[1][tope1] +" "+ a[2][tope2 -1] );
                      printStatus(a);
                  }
                  System.out.println(a[0][tope0]+ " "+ a[2][(tope2-1)] );
                  if((a[2][(tope2 -1)]> a[0][tope0])){
                      temp=a[0][tope0];
                      a[2][tope2] = temp;
                      a[0][tope0] = 0;    
                      tope0= tope0 -1;    
                      tope2 = tope2 +1 ;
                      System.out.println(a[0][tope0]+ " "+a[1][tope1] +" "+ a[2][tope2 -1] );
                      printStatus(a);
                  }
              
                  if(a[1][tope1]==0){
                      System.out.println(a[1][tope1]);
                      temp = a[0][tope0];
                      a[1][tope1]= temp;
                      a[0][tope0]= 0;
                      tope0= tope0 -1;
                      tope1 = tope1 +1;
                      System.out.println(a[0][tope0]+ " "+a[1][tope1 - 1] +" "+ a[2][tope2 -1] );
                      printStatus(a);
                  }
                  System.out.println(a[0][tope0]+ " "+ a[1][(tope1-1)] );
                  if(a[1][(tope1-1)]> a[0][tope0]){
                      temp = a[0][tope0];
                      a[1][tope1]= temp;
                      a[0][tope0]= 0;
                      tope0= tope0 -1;
                      tope1 = tope1 +1;
                      System.out.println(a[0][tope0]+ " "+a[1][tope1 - 1] +" "+ a[2][tope2 -1] );
                      printStatus(a);
                  }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2012-11-30
                • 2020-07-16
                • 1970-01-01
                • 1970-01-01
                • 2017-01-30
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多