1

I have the below piece of code for swapping.

public static <E> void swap(List<E> list, int i, int j){
        E temp = list.get(i);
        list.set(i, list.get(j));
        list.set(j, temp);
}

Now when I use List backed by Integer array like below

Integer[] ar = new Integer[]{1,2};      
swap(Arrays.asList(ar),1,0);

It works fine and gives output as [2,1] But I use List backed by int array like below

int[] ar = new int[]{1,2};
swap(Arrays.asList(ar),1,0);

It hrows ArrayIndexOutOfBounds exception. I don't understand why this is happening. List should treat int element as object only. Little help please.

5
  • 3
    Adding some garbage text to meet the minimum requirements for posting a question is ridiculous. Commented Apr 11, 2019 at 13:10
  • Thanks Aniket. Love your reply :) Commented Apr 11, 2019 at 13:16
  • @AniketSahrawat I'm actually surprised the garbage text was even needed. Certainly, I have seen shorter questions today, so it can't be a length requirement alone. Commented Apr 11, 2019 at 13:18
  • 1
    @TimBiegeleisen I think it's code vs text ratio which is considered. Never really read about it though. Commented Apr 11, 2019 at 13:19
  • If it's the ratio of text to code, @UtpalKumar should have been fine if he hadn't included most of his questions inside of the code block. Commented Apr 11, 2019 at 13:22

1 Answer 1

2

Here is what is happening. Your current code is actually creating a List<Object>, which happens to contain just a single int[], not a list of actual integers:

int[] ar = new int[]{1,2};
List<Object> list = Arrays.asList(ar);

The reason for the ArrayIndexOutOfBounds exception is that the list you pass in to the swap method has only one entry, at index zero.

In any case, it is not possible to use Arrays.asList to directly convert an array of primitives to a list of some boxed type. The first version of your code is correct, and is what you should be using:

Integer[] ar = new Integer[] {1, 2};
swap(Arrays.asList(ar), 1, 0);
Sign up to request clarification or add additional context in comments.

3 Comments

In both cases i.e primitive and Wrapper object array, i am passing only Array object reference to swap method, where it is viewed as List and being referred by a list object. I still don't get how i am passing a single int[]. Is it something to do with internal structure of both the arrays.
That's the thing, viewed as List doesn't mean what you think it does. The way you are calling Arrays.asList without specifying a type means that it defaults to List<Object>, with one entry for the primitive array. It doesn't "know" that you want a list of integers.
I guess you are right, when i do this "int[] ar1 = new int[]{1,2}; System.out.println(Arrays.asList(ar1).size());" it returns 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.