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

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

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?

  1. public static int[] withoutDuplicates(int[] x) {
  2. int[] temp = new int[x.length];
  3. int count = 0;
  4. for (int i = x.length - 1; i >= 0; i--) {
  5. for (int j = i-1; j >= 0; j--) {
  6. if (temp[i] == x[j]) {
  7. temp[i] = 0; // Instead of null, assign 0 to indicate removal.
  8. count++;
  9. }
  10. }
  11. }
  12. int size = x.length - count;
  13. int[] a = new int[size];
  14. int pos = 0;
  15. for (int i = 0; i < x.length; i++) {
  16. if (temp[i] != 0) { // Check for 0 instead of null.
  17. a[pos] = temp[i];
  18. pos++;
  19. }
  20. }
  21. return a;
  22. }
英文:

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?

  1. public static int[] withoutDuplicates(int[] x) {
  2. int[] temp = new int[x.length];
  3. int count = 0;
  4. for (int i = x.length - 1; i &gt;= 0; i--) {
  5. for (int j = i-1; j &gt;= 0; j--) {
  6. if (temp[i] == x[j]) {
  7. temp[i] = null;
  8. count++;
  9. }
  10. }
  11. }
  12. int size = x.length - count;
  13. int[] a = new int[size];
  14. int pos = 0;
  15. for (int i = 0; i &lt; x.length; i++) {
  16. if (temp[i] != null) {
  17. a[pos] = temp[i];
  18. pos++;
  19. }
  20. }
  21. return a;
  22. }

答案1

得分: 1

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

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

  1. 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:

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

答案2

得分: 1

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

  1. public static int[] withoutDuplicates(int[] x) {
  2. boolean[] duplicated = new boolean[x.length];
  3. int count = 0;
  4. for (int i = x.length - 1; i > 0; i--) {
  5. for (int j = i-1; j >= 0; j--) {
  6. if (x[i] == x[j]) {
  7. duplicated[i] = true;
  8. count++;
  9. }
  10. }
  11. }
  12. int size = x.length - count;
  13. int[] a = new int[size];
  14. for (int i = 0, pos = 0; pos < size && i < x.length; i++) {
  15. if (!duplicated[i]) {
  16. a[pos++] = x[i];
  17. }
  18. }
  19. return a;
  20. }

测试:

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

输出:

  1. [1, 2, 3, 4, 5]
  2. [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:

  1. public static int[] withoutDuplicates(int[] x) {
  2. boolean[] duplicated = new boolean[x.length];
  3. int count = 0;
  4. for (int i = x.length - 1; i &gt; 0; i--) {
  5. for (int j = i-1; j &gt;= 0; j--) {
  6. if (x[i] == x[j]) {
  7. duplicated[i] = true;
  8. count++;
  9. }
  10. }
  11. }
  12. int size = x.length - count;
  13. int[] a = new int[size];
  14. for (int i = 0, pos = 0; pos &lt; size &amp;&amp; i &lt; x.length; i++) {
  15. if (!duplicated[i]) {
  16. a[pos++] = x[i];
  17. }
  18. }
  19. return a;
  20. }

Test:

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

output

  1. [1, 2, 3, 4, 5]
  2. [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:

确定