1

I have read other similar questions on StackOverflow but do not understand the root cause, and can't figure out a fix on my own. Could you help me understand what the issue is?

Here is my code:

List<List<Integer>> res;

public List<List<Integer>> permute(int[] nums) {
    res = new ArrayList();
    List<Integer> n = new ArrayList(Arrays.asList(nums));
    permute(n, new ArrayList());
    return res;
}

private void permute(List<Integer> nums, List<Integer> cur) {
    if (nums.size() == 0) {
        res.add(cur);
        return;
    }
    
    for (int i = 0; i < nums.size(); ++i) {
        List<Integer> temp = new ArrayList<Integer>(cur);
        List<Integer> temp2 = new ArrayList<Integer>(nums);
        Integer c = (Integer)nums.get(i); (line 20)
        temp2.remove(i);
        
        permute(temp2, temp);
    }
}

but I get this error:

java.lang.ClassCastException: class [I cannot be cast to class java.lang.Integer ([I and java.lang.Integer are in module java.base of loader 'bootstrap')
  at line 20, Solution.permute
  at line 7, Solution.permute
  at line 54, __DriverSolution__.__helper__
  at line 84, __Driver__.main
  • What's inside `nums`? Where's the code that puts stuff in it? – Federico klez Culloca Jun 27 '22 at 14:13
  • 3
    Why are you casting this? – m.antkowicz Jun 27 '22 at 14:13
  • @m.antkowicz sorry, that was after playing around trying to get it to work, I forgot to remove it. Keeping it now for the sake of anyone reading this, so they don't get confused. – web1connoisseur Jun 27 '22 at 14:17
  • This code doesn't do anything. The for loop is removing values from a copy of the original List, not the List. You don't need the cast. Depending on the size of the Lists, you're gobbling up a ton of memory. – Ryan Jun 27 '22 at 14:17
  • @Ryan I'm just playing around with this, I understand it might not be optimal. Please help me understand the issue on line 20. – web1connoisseur Jun 27 '22 at 14:20
  • 1
    @web1connoisseur `List n = new ArrayList(Arrays.asList(nums));` didn't this throw a warning? – Federico klez Culloca Jun 27 '22 at 14:20
  • @FedericoklezCulloca it did not. However, I'm doing this in LeetCode's web IDE. – web1connoisseur Jun 27 '22 at 14:21
  • `([I and java.lang.Integer are ...`, this `I`, where it comes from ? Strange error message ... – PeterMmm Jun 27 '22 at 14:23
  • @web1connoisseur I see. By the way my point is, `n` is not what you think it is, since `new ArrayList(Arrays.asList(nums))` does *not* return a `List`. So when you cast the result of `get`ting something from it you're not `get`ing an `Integer` and the cast fails. – Federico klez Culloca Jun 27 '22 at 14:23
  • 2
    See [this other answer](https://stackoverflow.com/a/2607327/133203) as for what is happening here (and the other answers in that thread to see how to circumvent this). – Federico klez Culloca Jun 27 '22 at 14:25
  • Typecast error can be removed if you know how to circumvent the integer type. – Roman C Jun 27 '22 at 14:27
  • Regarding your question: "_this `I`, where it comes from?_" - the identifier is actually `[I`. For an explanation see [\[L array notation - where does it come from?](https://stackoverflow.com/q/5085889/12567365) In your case, it refers to an array of integers. @PeterMmm – andrewJames Jun 27 '22 at 14:32
  • `Integer c` is never being used ...so what's the point? – Martin Zeitler Jun 27 '22 at 14:36
  • @MartinZeitler this solution isn't finished past where the error occurs. I would have added c to temp before calling permute. – web1connoisseur Jun 27 '22 at 14:40
  • 1
    Does this answer your question? [Using Arrays.asList with int array](https://stackoverflow.com/questions/25483854/using-arrays-aslist-with-int-array) (not the best duplicate but it explains what the end result is and my ddg-fu is failing me) – Federico klez Culloca Jun 27 '22 at 14:46
  • 1
    As noted in [this comment](https://stackoverflow.com/questions/2607289/converting-array-to-list-in-java/30281879#comment123882111_30281879), these days you can use `List n = Arrays.stream(nums).boxed().toList();` to avoid this problem. See also Josh Bloch's _Effective Java_ (3rd Edition) item 32: "Combine generics and varargs judiciously". – andrewJames Jun 27 '22 at 20:14
  • 1
    That worked, thanks @andrewJames! If you want to post that as the answer, I'll accept it – web1connoisseur Jun 28 '22 at 13:29
  • I don't think I can write an answer for this, as it would be a duplicate of the question [Converting array to list in Java](https://stackoverflow.com/q/2607289/12567365). The explanation of the problem is referenced in [this answer about generics and varargs](https://stackoverflow.com/a/2607327/12567365) in that question. And one solution is referenced in [this other answer which uses the stream and `boxed()` solution](https://stackoverflow.com/a/30281879/12567365) in the same question. I would go vote for those, instead of voting for any answer here. – andrewJames Jun 28 '22 at 17:50
  • You could also use `Integer[]` instead of `int[]`. – andrewJames Jun 28 '22 at 17:50

0 Answers0