File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList

 
suavedeep kaur
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to create an ArrayList and then display it using an Array but theres an exception at runtime



please explain me whats the problem ???
[Dave - formatted and added code tags]
[ July 26, 2008: Message edited by: David O'Meara ]
 
Agarwal Priyanka
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the better way to examine any exception is using the try-catch block. you must try that to find the exception first.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Providing the exceprion that gets thrown would also be helpful.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the output:

Now this makes sense. Your ArrayList contains Strings. Then, when using the toArray method, you try to put all those elements into an Integer[]. Of course Integer[] cannot store String, that's where the ArrayStoreException comes from. As specified in the Javadoc of java.util.Collection:


Change Integer[] to String[] and it will work.


Mind you, I do think this is a mistake in the API. The method signature is "<T> T[] toArray(T[] a)" - it should have been "<T super E> T[] toArray(T[] a)" to enforce that T is E or a superclass of E (or an interface that is implemented by E). In your example, with E being String, that would limit T to one of the following:
- String
- Object
- Serializable
- CharSequence
- Comparable<String>
[ July 26, 2008: Message edited by: Rob Prime ]
 
suavedeep kaur
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot for the help Rob
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic