Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

List backed by Integer array versus int array differencr

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have the below piece of code for swapping.


Now when i use List backed by Integer array like below



It works fine and gives output as [2,1]

but i use List backed by int array like below



throws ArrayIndexOutOfBounds exception. Little help please
 
Marshal
Posts: 69426
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you posted the offending code exactly as it was run? Because what you wrote won't throw any exceptions. What you wanted to write, viz swap(Arrays.asList(ar1), 1, 0); will however throw an exception. If you look through the Arrays class, you will find the asList() method, but you won't find any overloadings for it. It takes T... as a parameter and ints cannot be a T. If you pass an Integer[], the method can take Integer as its T, but if you pass an int[] or any other array of primitives, it can only take int[] or similar as its type. So you create a List<int[]]> with one element.
 
Right! We're on it! Let's get to work tiny ad!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic