0

I have an ArrayList that I need converted back into an int[]. I have been trying to use the T[] toArray(T[] a) method inherited from the List interface. I've looked up several examples, and it seems I'm doing it right. Can someone tell me why I'm getting this error?

Line of code in question

keyCodes = projectedKeyCodes.toArray(new Integer[0]);

in

  public KeyBindingsAdapter(JComponent c, int delay, int... keyCodes)
  {
     boolean allKeysSelected;
     ArrayList<Integer> projectedKeyCodes = new ArrayList<Integer>();
     for(int k: keyCodes)
     {
        if(k==ALPHA_KEYS)for(int p = 65; p < 91; p++)projectedKeyCodes.add(new Integer(p));
        else if(k==NUMERIC_KEYS)for(int p = 48; p < 58; p++)projectedKeyCodes.add(new Integer(p));
        else if(k==DIRECTIONAL_KEYS)for(int p = 224; p < 228; p++)projectedKeyCodes.add(new Integer(p));
        else if(k==NUMPAD_KEYS)for(int p = 96; p < 112; p++)projectedKeyCodes.add(new Integer(p));
        else if(k==FUNCTION_KEYS)for(int p = 112; p < 124; p++)projectedKeyCodes.add(new Integer(p));
        else if(k==ALL_KEYS)allKeysSelected = true;
     }
     if(allKeysSelected)
     {
        keyCodes = new int[]{10,8,9,3,12,16,17,18,19,20,27,32,33,34,35,36,37,38,39,40,44,45,46,47,48,49,50,51,52,53,54,55,56,57,59,61,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,96,97,98,99,100,101,102,103,104,105,106,107,108,108,109,110,111,127,144,145,112,113,114,115,116,117,118,119,120,121,122,123,154,155,156,157,192,222,224,225,226,227,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,150,151,152,153,160,161,162,512,513,514,515,516,517,518,519,520,521,522,523,524,525,24,28,29,30,31,21,25,240,241,242,243,244,245,256,257,258,259,260,261,262,263,0};
     }
     else
     {
        keyCodes = projectedKeyCodes.toArray(new Integer[0]);
     }
     ...
     ...
  }

Error

KeyBindingsAdapter.java:41: error: incompatible types: inference variable T has incompatible upper bounds int,Object
         keyCodes = projectedKeyCodes.toArray(new Integer[0]);
                                             ^
  where T is a type-variable:
    T extends Object declared in method <T>toArray(T[])

Ideas?

user3376587
  • 124
  • 2
  • 12
  • post full code please – Michele Lacorte Sep 12 '15 at 20:44
  • 1
    That's not possible. If you're using Java 8, you could do `projectedKeyCodes.stream().mapToInt(i -> i).toArray()` but I'd say that is a bit of a hack. Go with a `new int[projectedKeyCodes.size()]` and an ordinary for loop. – aioobe Sep 12 '15 at 20:50
  • You can't implicitly cast from an `Integer[]` to an `int[]`. You have to copy from one type to another converting each element. – Peter Lawrey Sep 12 '15 at 21:20
  • Another Java 8 way to convert a `List` to an `int[]` is `IntStream.range(0, list.size()).map(list::get).toArray();` but I agree with aioobe that `for` loop is probably best. – Paul Boddington Sep 12 '15 at 21:38
  • aioobe... where in the world did you pull out that beautiful solution?? I don't understand i -> i, but it worked. The loop would've required me to rewrite some of my code and I just didn't want to do that. – user3376587 Sep 12 '15 at 22:52

0 Answers0