• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

type casting

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can an Arraylist be typecast to a vector?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. But they both extend AbstractList, so you can typecast to that.
 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
No. But they both extend AbstractList, so you can typecast to that.


True, but note that AbstractList's public methods are precisely
the same as List's, so I would more likely write:

On the other hand, I think the OP wanted to *cast* ("convert*) an existing ArrayList
to a Vector: you can't do that, but you can create a new Vector that
holds copies of the references found in the ArrayList:

Note that v != arrayList, although v.equals(arrayList).

Of course, in the best of all possible worlds, just being of type List should suffice!
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would mention two principles here that are useful:

1. Vector is old and kind of discouraged. Avoid its use unless there is legacy code which you must add your code to.

2. Use Collections 2 and always strive to use references of the base-interface type. E.g.,

List list = // new ArrayList();
public List getItems(){...}

This way, the implementation can be changed when needed.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A lot of classes in javax.swing expect a Vector in the constructor (or an array) but not an arraylist.
That's often a reason for using Vector.
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic