• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Creating a vector

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

I am trying to create a vector:

Vector myVector = new Vector();

When I try an add an element to the end:

myVector.addElement(3);

and I compile I get the following message:

Note: C:\Vector.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

So, when I do that I get:

Vector.java:94: warning: [unchecked] unchecked call to addElement(E) as a member of the raw type java.util.Vector myVector.addElement(currentID);
^

What am i doing wrong? I cant seem to find any help anywhere

Thanks
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a warning one gets when one uses Collection Framework classes
which have been generified in Java 5.
either you can ignore the warning or use Java 1.4 compiler.
or learn about generics and use them.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Replacing



with



should do the trick.
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 is consider as a primitive datatype. To add 3 to that vector yo have to convert that 3 to Integer Object.
Try this before adding to vector
Integer x=new Integer(3);
then add x
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, use ArrayList instead of Vector.

Vector is a legacy collection class, from Java version 1.0. In version 1.2, new collection classes were added to Java. ArrayList is the replacement for Vector. Although the old collection classes are still in the Java API, there's no real reason anymore to use them in your programs.

See: The Collections Framework.

Originally posted by Aby Ouseph:
3 is consider as a primitive datatype. To add 3 to that vector yo have to convert that 3 to Integer Object.
Try this before adding to vector
Integer x=new Integer(3);
then add x


There's no need for that if you're using Java 5 or 6, because of autoboxing (a new feature in Java 5). This will work without errors in Java 5 or 6:

[ May 31, 2007: Message edited by: Jesper Young ]
 
Saket Barve
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


To add 3 to that vector yo have to convert that 3 to Integer Object.



Not required for JDK 1.5+ what with the Autoboxing feature.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ArrayList is the replacement for Vector. Although the old collection classes are still in the Java API, there's no real reason anymore to use them in your programs.



I thought that Vector was still useful because it is thread-safe and that ArrayList should be used when thread-safety isn't an issue to get higher performance.

Kaydell
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kaydell Leavitt:
I thought that Vector was still useful because it is thread-safe and that ArrayList should be used when thread-safety isn't an issue to get higher performance.

Kaydell


Thread safety is an issue only when you're writing a multi-threaded program and there are multiple threads that are modifying the same collection at the same time.

In the rare case that you really need a synchronized (thread-safe) collection, you could wrap your ArrayList in a thread-safe list like this:
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Saket Barve:
Replacing
with
should do the trick.



Then what if I want to add various types of objects (say, Strings, Integers, Panels) to the same vector?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prosenjit Banerjee:
Then what if I want to add various types of objects (say, Strings, Integers, Panels) to the same vector?


Instead of Integer use a common superclass between the objects you want to add. Since String, Integer and Panel have no other common superclass than Object, use:

Ofcourse, since Object is the superclass of all classes, this won't provide much type-safety...

Another example: Suppose you could add different kinds of numbers (integers, longs, doubles, ...). Then you could use Number as the parametric type, since Number is a common superclass of Integer, Long, Double etc.
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is the code above the equivalent to:



Kaydell
[ June 21, 2007: Message edited by: Kaydell Leavitt ]
 
And then the entire population worshiped me like unto a god. Well, me and 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