简体   繁体   English

无法将整数对象转换为原始类型

[英]Trouble casting integer object to primitive type

I'm trying to do this challenge on codewars : https://www.codewars.com/kata/554ca54ffa7d91b236000023/train/java I changed the original array into an arraylist, but then I have to use the values in arrayist to do some comparisons with primitive types. 我正在尝试在codewars上进行此挑战: https ://www.codewars.com/kata/554ca54ffa7d91b236000023/train/java我将原始数组更改为arraylist,但是随后我必须使用arrayist中的值来做一些操作与原始类型的比较。

I tried to cast the Integer objects using (int) but there's still casting errors. 我尝试使用(int)强制转换Integer对象,但仍然存在强制错误。 When I tried to do (int)(arrList.get(j)).equals(current), it tells me boolean can't be converted to int. 当我尝试执行(int)(arrList.get(j))。equals(current)时,它告诉我布尔值无法转换为int。

import java.util.*;
public class EnoughIsEnough {

    public static int[] deleteNth(int[] elements, int maxOccurrences) {
      ArrayList arrList = new ArrayList<>(Arrays.asList(elements));
    for (int i = 0; i < arrList.size(); i++) {
      int current = (int)arrList.get(i);
      int occurrences = 1;
      for (int j = i + 1; j < arrList.size(); j++) {
        if (arrList.get(j).equals(current) && occurrences >= maxOccurrences) {
          arrList.remove(j);
        } else if (arrList.get(j).equals(current)) {
          occurrences++;
        }
      }
    }
    int arr[] = new int[arrList.size()];
    for (int i = 0; i < arrList.size(); i++) {
      arr[i] = (int) arrList.get(i);
    }
    return arr;
    }

}

It compiled but the test shows : class [I cannot be cast to class java.lang.Integer ([I and java.lang.Integer are in module java.base of loader 'bootstrap') 它已编译,但测试显示:类[无法将其强制转换为类java.lang.Integer([和java.lang.Integer在加载程序'bootstrap'的模块java.base中)

Arrays.asList(elements) does not do what you think it does, it returns a list of containing the int[] array, not the elements of the array. Arrays.asList(elements)并没有您认为的那样,它返回一个包含int []数组而不是数组元素的列表。 You can not create a list of primitives. 您无法创建基元列表。 If you want to use List you must first convert the int to Integer . 如果要使用List,则必须首先将int转换为Integer

You can get a List of Integer with 您可以使用获取Integer列表

List<Integer> arrList = Arrays.stream(elements).boxed().collect(Collectors.toList());

however, you still have a bug in your program where you will skip numbers. 但是,您的程序中仍然存在一个错误,您将跳过数字。

for (int j = i + 1; j < arrList.size(); j++) {
  if (arrList.get(j).equals(current) && occurrences >= maxOccurrences) {
    arrList.remove(j); // This shortens the list causing us to skip the next element
    j--; // One hackish way is to go back one step

  } else if (arrList.get(j).equals(current)) {
    occurrences++;
  }
}

One solution is to loop backwards instead 一种解决方案是改为向后循环

for (int j = arrList.size() - 1; j > i; j--) {
  if (arrList.get(j).equals(current) && occurrences >= maxOccurrences) {
    arrList.remove(j);
  } else if (arrList.get(j).equals(current)) {
    occurrences++;
  }
}

You can replace 您可以更换

ArrayList arrList = new ArrayList<>(Arrays.asList(elements));

with

List<Integer> arrList = Arrays.stream(elements).boxed().collect(Collectors.toList());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM