设置整型数组(int[])的元素为null时出现错误。

huangapple go评论62阅读模式
英文:

Error when setting entry of int[] array to null

问题

I had the following idea to take an array and remove any duplicates. However, I am getting the error "error: incompatible types: cannot be converted to int" referring to the part of the code where I set temp[i] = null. Is it possible to do this? How can I fix this problem?

public static int[] withoutDuplicates(int[] x) {
    int[] temp = new int[x.length];
    int count = 0;
    for (int i = x.length - 1; i >= 0; i--) {
        for (int j = i-1; j >= 0; j--) {
            if (temp[i] == x[j]) {
                temp[i] = 0; // Instead of null, assign 0 to indicate removal.
                count++;
            }
        }
    }
    int size = x.length - count;
    int[] a = new int[size];
    int pos = 0;
    for (int i = 0; i < x.length; i++) {
        if (temp[i] != 0) { // Check for 0 instead of null.
            a[pos] = temp[i];
            pos++;
        }
    }
    return a;
}
英文:

I had the following idea to take an array and remove any duplicates. However, I am getting the error "error: incompatible types: <null> cannot be converted to int" referring to the part of the code where I set temp[i] = null. Is it possible to do this? How can I fix this problem?

public static int[] withoutDuplicates(int[] x) {
    int[] temp = new int[x.length];
    int count = 0;
    for (int i = x.length - 1; i &gt;= 0; i--) {
      for (int j = i-1; j &gt;= 0; j--) {
        if (temp[i] == x[j]) {
          temp[i] = null;
          count++;
        }
      }
    }
    int size = x.length - count;
    int[] a = new int[size];
    int pos = 0;
    for (int i = 0; i &lt; x.length; i++) {
      if (temp[i] != null) {
        a[pos] = temp[i];
        pos++;
      }
    }
    return a;
  }

答案1

得分: 1

你不能将 null 赋值给原始类型 int,但可以将 null 赋值给包装对象 Integer

要移除整型数组的重复项,你可以使用类似以下的方法:

myIntArray = Arrays.stream(myIntArray).distinct().toArray();
英文:

You can't assign a null to a primitive type int but you can assign a null to the wrapper object Integer.

To remove the duplicates of an int array you could use something like this:

myIntArray = Arrays.stream(myIntArray).distinct().toArray();

答案2

得分: 1

流式解决方案简洁,但根据您的任务/要求,您可以仅使用临时布尔数组来标记重复位置:

public static int[] withoutDuplicates(int[] x) {
    boolean[] duplicated = new boolean[x.length];
    int count = 0;
    for (int i = x.length - 1; i > 0; i--) {
        for (int j = i-1; j >= 0; j--) {
            if (x[i] == x[j]) {
                duplicated[i] = true;
                count++;
             }
        }
    }
    int size = x.length - count;
    int[] a = new int[size];
    for (int i = 0, pos = 0; pos < size && i < x.length; i++) {
        if (!duplicated[i]) {
            a[pos++] = x[i];
        }
    }
    return a;
}

测试:

int[] arr1 = {1, 2, 3, 4, 3, 5, 2};
int[] arr2 = {1, 2, 3, 4, 0, 5, 6};
        
System.out.println(Arrays.toString(withoutDuplicates(arr1)));
System.out.println(Arrays.toString(withoutDuplicates(arr2)));

输出:

[1, 2, 3, 4, 5]
[1, 2, 3, 4, 0, 5, 6]
英文:

Stream-based solution is concise but for your task/requirements you could be using just a temporary boolean array to mark positions of duplicates:

public static int[] withoutDuplicates(int[] x) {
    boolean[] duplicated = new boolean[x.length];
    int count = 0;
    for (int i = x.length - 1; i &gt; 0; i--) {
        for (int j = i-1; j &gt;= 0; j--) {
            if (x[i] == x[j]) {
                duplicated[i] = true;
                count++;
             }
        }
    }
    int size = x.length - count;
    int[] a = new int[size];
    for (int i = 0, pos = 0; pos &lt; size &amp;&amp; i &lt; x.length; i++) {
        if (!duplicated[i]) {
            a[pos++] = x[i];
        }
    }
    return a;
}

Test:

int[] arr1 = {1, 2, 3, 4, 3, 5, 2};
int[] arr2 = {1, 2, 3, 4, 0, 5, 6};
        
System.out.println(Arrays.toString(withoutDuplicates(arr1)));
System.out.println(Arrays.toString(withoutDuplicates(arr2)));

output

[1, 2, 3, 4, 5]
[1, 2, 3, 4, 0, 5, 6]

huangapple
  • 本文由 发表于 2020年10月10日 04:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64286578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定