This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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: 64496
225
  • 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.
 
Always! Wait. Never. Shut up. Look at this tiny ad.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!