• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

K&B SCJP Study Guide for Java 5 p 559 List question.

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
p559 code snippet




Can you help me out here? "List" is an interface. The Java 1.5 API states that "Arrays.asList()" returns a "List", but "List" is not an object (?) How can you get the size() of an interface reference, or get index 2 of an interface reference?

This whole thing looks kind of funky to me.

Thanks.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
the static method asList() return a reference of an object who pass the IS-A test of the interface, if you modify your code by adding the following line :

it will disply :

java.util.Arrays$ArrayList,


the ArrayList is a class that implement the interface List, and if we have a class that implement a given interface, an instance of this interface can hold a reference of the class who implements it, so the code is cool and the rest of your code lines will display the size of the ArrayList who it's 4, and the value of the given index, peace
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might find this interesting. When the .asList() static method is used, the initial array and the ArrayList are linked; changes made to one are reflected in changes to the other. I tried your suggestion, and I got the following result:

java.util.Arrays$ArrayList

This isn't the same thing as the following result, which is also demonstrated in my code snippet:

java.util.ArrayList

As you can see, getClass().getName returns a different result in the two cases.

C:\TEMP>java ListTest
java.util.Arrays$ArrayList
[one, two, three, four]
java.util.ArrayList
[one, two, three, four]



Thanks,
Harry



 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you can see, we return the return type only to the reference variable but internally the object used is different. Here we use the polymorphic type as its the list view and any type of list can be expected. But actually for arrays to list view, the internal list used is arraylist.
 
I think I'll just lie down here for a second. And ponder this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic