• 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 65033
247
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!