• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList and List as parameters  RSS feed

 
Vinicius Souza
Ranch Hand
Posts: 70
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends! I hava a method with this structure:


But when I call this method with:

I'm getting a runtime convertion erro as follows:
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

What is wrong?
Thanks!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you noticed, this statement is risky:


It assumes that n is an ArrayList when it is not. It can be any type of List.

If you need an ArrayList, you can create a copy using:


Or you could just loop through the List and not create an intermediate variable.
 
Vinicius Souza
Ranch Hand
Posts: 70
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still not working using both forms!
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not working... in what way?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you casting to ArrayList at all? You don't need the list to be a java.util.ArrayList in that method. Just use the List interface and don't cast. Also, use generics; do not use the raw type List.

 
Vinicius Souza
Ranch Hand
Posts: 70
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Why are you casting to ArrayList at all? You don't need the list to be a java.util.ArrayList in that method. Just use the List interface and don't cast. Also, use generics; do not use the raw type List.


Thank you it works! The problem was my missunderstanding about interfaces, now I understood how it works.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point is that the object that the variable 'nomes' refers to is some object that implements interface List<String>, and the method should not care which actual implementation is behind it - whether it's an ArrayList, LinkedList or whatever other implementation of class List. The method only needs to call methods on the object that are defined on the interface List.

This demonstrates one of the most important principles in object oriented programming: loose coupling. Different parts (classes, methods) of the program should know as little as possible about other parts of the program - for example your method only needs to know that 'nomes' is a List<String> and nothing more. By not having more knowledge than necessary about other parts of the program, your methods become more flexible - you can use them with any kind of List and not just with ArrayList.

Another important principle that is relevant here is "program to an interface, not an implementation", which has the same advantage as what I mentioned above - by making it work via the interface List, the method can work on any kind of List, and not just one particular implementation such as ArrayList.
 
Vinicius Souza
Ranch Hand
Posts: 70
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation friend, as I use more Java, I will use this interfaces and understand more deeply these techniques. Thank you for your explanation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!