Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

List backed by Integer array versus int array differencr  RSS feed

 
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: 63842
209
  • 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.
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!