【问题标题】:How to convert null to int without casting to integer?如何在不转换为整数的情况下将 null 转换为 int?
【发布时间】:2014-01-07 00:22:43
【问题描述】:

好的,所以我尝试使用 3 个单独的条件来获得奇数、偶数和负数

%2=0、%2!=0 和

但是,如果说该数字不属于我放入 element at [i] = null; 的条件,我会收到一条错误消息,说无法从 int 转换为 null 添加强制转换整数

- Type mismatch: cannot convert from 
     null to int

然后,如果我继续转换整数,我会收到此错误

Exception in thread "main" java.lang.NullPointerException
    at Server.getEven(Server.java:10)
    at Main.main(Main.java:10)

现在我们还没有在我的计算机课上学过投射,我的老师不会接受我的投射作业,即使它不起作用。

我想知道我是否可以通过将我的 3 个数组存储在服务器中并在客户端中只输入单个数组来完成这个项目

即我的偶数数组、奇数数组、负数数组,全部存储并打印在服务器中,而仅在客户端中拥有“用户输入的数组”是我的代码

客户

import java.util.Scanner;
public class Main {
    public static void main (String[] args){
        Scanner input = new Scanner(System.in);
        int [] array = new int[10];
        System.out.print("Insert the 10 values of your array.");
        for(int i=0; i<array.length; i++){
            array[i] = input.nextInt();
        }
        int[] even = Server.getEven(array);
        int[] odd = Server.getOdd(array);
        int[] neg = Server.getNeg(array);
        System.out.println("The even numbers in the array are...");
        System.out.println(even);
        System.out.println("The odd numbers in the array are...");
        System.out.println(odd);
        System.out.println("The negative numbers in the array are...");
        System.out.println(neg);

        input.close();
    }
}

服务器

public class Server {
    public static int[] getEven(int[] array){
        int[] even = new int[array.length];
        for(int i=0; i<array.length; i++){
            if(array[i]%2 ==0){
                even[i] = array[i];
            }
            else
            { even[i] = null;// <-- here it asks me to do (Integer) null;
            }
        }
        return even;
    }

    public static int[] getOdd(int[] array){
        int[] odd = new int[array.length];
        for(int i=0; i<array.length; i++){
            if(array[i]%2 !=0){
                odd[i] = array[i];
            }
            else
            { odd[i] = null; // <-- here it asks me to do (Integer) null;
            }
        }
        return odd;
    }

    public static int[] getNeg(int[] array){
        int[] neg = new int[array.length];
        for(int i=0; i<array.length; i++){
            if(array[i]<0){
                neg[i] = array[i];
            }
            else
            { neg[i] = null; // <-- here it asks me to do (Integer) null;
            }
        }
        return neg;
    }

}

【问题讨论】:

  • 好的,所以我已经尝试过你建议做 Integer[] 但现在它给了我这个......数组中的偶数是...... [Ljava.lang.Integer;@3fe329eb数组中的奇数是... [Ljava.lang.Integer;@5ad52411 数组中的负数是... [Ljava.lang.Integer;@5f3306ad
  • 这是因为正在使用对象 toString 方法打印数组。而不是 System.out.println(even); 使用 for 循环并打印每个元素。
  • 尝试使用System.out.println(Arrays.toString(yourArray))进行打印。
  • @JavaDevil 不错的一个:) +1。更多信息:stackoverflow.com/questions/5415604/tostring-java-of-arrays
  • 您真的需要阵列中的孔吗?如果我得到{3, 2, 8, 1, 6, 5, 9} 作为输入,我希望getEven 返回{2, 8, 6},而不是{&lt;hole&gt;, 2, 8, &lt;hole&gt;, 6, &lt;hole&gt;, &lt;hole&gt;}。如果您不需要这些漏洞,那么这将影响我们建议的解决方案。

标签: java arrays casting null


【解决方案1】:

不能null 放入int[]。您必须要么使用Integer[],要么使用像-1 这样的显着“标志值”。

【讨论】:

    【解决方案2】:

    这里的基本问题是,您预先声明了三个数组,却不知道它们需要保存多少个值。当然,您知道它们将保存的最大值数 (10),但您不知道最小值。这会影响您对Arrays.toString 的调用。

    使用List 将是首选方法,但如果您还没有在课堂上介绍强制转换,那么我猜列表也尚未涵盖。为什么不让您的服务器返回 String 呢? (为了简单起见,我故意在这里省略了 StringBuilders,因为我怀疑它们已经被覆盖了)。

    主要:

    import java.util.Scanner;
    public class Main {
        public static void main (String[] args){
            Scanner input = new Scanner(System.in);
            int [] array = new int[10];
            System.out.print("Insert the 10 values of your array.");
            for(int i=0; i<array.length; i++){
                array[i] = input.nextInt();
            }
    
            System.out.println("The even numbers in the array are...");
            System.out.println(Server.getEven(array));
            System.out.println("The odd numbers in the array are...");
            System.out.println(Server.getOdd(array));
            System.out.println("The negative numbers in the array are...");
            System.out.println(Server.getNeg(array));
    
            input.close();
        }
    }
    

    服务器

    public class Server {
        public static String getEven(int[] array){
            String result = "";
            for(int i=0; i<array.length; i++){
                if(array[i]%2 ==0){
                    if(result.length() != 0)
                    {
                        result = result + ", ";
                    }
                    result = result + array[i];
                }
            }
            return result;
        }
    
        public static String getOdd(int[] array){
            String result = "";
            for(int i=0; i<array.length; i++){
                if(array[i]%2 !=0){
                    if(result.length() != 0)
                    {
                        result = result + ", ";
                    }
                    result = result + array[i];
                }
            }
            return result;
        }
    
        public static String getNeg(int[] array){
            String result = "";
            for(int i=0; i<array.length; i++){
                if(array[i]<0){
                    if(result.length() != 0)
                    {
                        result = result + ", ";
                    }
                    result = result + array[i];
                }
            }
            return result;
        }
    }
    

    【讨论】:

    • 非常感谢,这是一个非常容易解决的问题,我不必更改太多代码,并且根据我所学到的最新知识,这非常有效,因为你是对的我对选角和列表不太熟悉。
    【解决方案3】:

    evenodd 声明为Integer[] 而不是int[]。对于您的情况,这是最简单的解决方法。

    【讨论】:

      【解决方案4】:

      int 只会将数字作为值 使用 Integer 代替它将保存数字或 null

      【讨论】:

        【解决方案5】:

        intprimitive 类型,只能带数字。

        您可以使用Integer[],它是一个包含原始数字的包装对象。 从那以后你使用一个对象数组,你可以有一个空条目(空引用)。

        例子:

        Integer[] even;
        

        您还可以指定一个特殊值来表示null。示例:Integer.MIN_VALUE

        【讨论】:

          【解决方案6】:

          要编写一个接受数组并返回包含其元素子集的数组的方法,您需要做一些不同的事情。如果您可以使用ArrayList,那将是最好的,因为它可以让您在事先不知道大小时创建一个数组。如果不能,则必须通过两次。首先弄清楚结果中需要多少元素。然后,不是分配even[i],其中even 是您要返回的数组,而是分配even[index],其中index 是与循环索引不同 的索引,仅当您将元素分配给它时才会增加 1。

          public static int[] getEven(int[] array){
              // Count the event elements in array, and assign `count` to the count
              // (I'll let you fill in that part)
          
              int[] even = new int[count];
              int index = 0;
              for(int i=0; i<array.length; i++){
                  if(array[i]%2 ==0){
                      even[index++] = array[i];
                  }
              }
              return even;
          }
          

          【讨论】:

          • 好的,我尝试了你的想法,我收到了这个错误The even numbers in the array are... [I@efb78af这是我写的public static int[] getEven(int[] array){ int count =0; for(int i=0; i&lt;array.length; i++){ if(array[i]%2 ==0){ count++; } else { count = count*1; } } int[] even = new int[count]; int index = 0; for(int i=0; i&lt;array.length; i++){ if(array[i]%2 ==0){ even[index++] = array[i]; } } return even; }
          • @AdamCalcanes 你仍然不能在数组上使用System.out.println。看起来其中一个答案留在了里面,但这是一个错误。请参阅上面的 Java Devil 评论。附: count = count * 1的目的是什么?
          • 我使用 count = count*1 这样如果条件没有发生,那么它只会返回相同的 count 值,即如果数字不是偶数并且计数器没有增加那么它将保持 0 因为 0*1=0 或者如果它是“3”并且它没有增加 3*1=3 我想我可以留下一个空白的 else{} 语句,但我只是急于回答你,哈哈如果我不能使用System.out.println 我怎么能显示它?
          • 您可以使用for 循环显示数组的值。 for(int i = 0; i &lt; array.length; i++) { System.out.print(array[i] + " ")}
          猜你喜欢
          • 1970-01-01
          • 2022-01-20
          • 2016-10-30
          • 1970-01-01
          • 1970-01-01
          • 2013-05-23
          • 2020-07-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多