• Post Reply Bookmark Topic Watch Topic
  • New Topic

Vector Questions  RSS feed

 
JR Pearson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couple of items on my mind today guys.

Here's the code I'm working with:



Now, two questions. First, when I compile this, it gives me a warning about "Unchecked call to add(E) as a member of the raw data type java.util.Vector".

Is that something I need to worry about? It runs just fine, just with the warning. Is there a "proper" way to add these elements that wouldn't give me that warning?

Second question:

The book I'm going through said I cannot multiply two Double variables together; that I had to first extract the primitive values out first then construct a new Double based on the product. That's the "part 2" portion of code. But the first 'println' prints the same result as the second...no errors. Is the book wrong...new feature in 1.5...am I just missing the book's point?

Thanks a bunch everyone!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First question only for now...

Your book is a bit behind the times. The warning comes because of a new language feature in Java 5 called Generics. You can specify the type that a collection is allowed to hold which gives you run-time checking that you're putting the right type in and eliminates the need to cast when you get things back out. I was going to make an example out of your code, but I can't see where you put anything in the Vector. Maybe just didn't get that far yet?

Here's what the syntax looks like. Google about for more on Java Generics.

One other point is that Vector is a legacy class and there are better choices nowadays. See the Collections Framework tutorial for more details.

Leet me know if that helps!
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both of your questions involve features that are new in Java 1.5.

The Vector class has been retrofitted to be generic. So if you don't specify the type of the element to be stored in the Vector, you will get a warning, but the code will still work.

In 1.5, a Double will be autounboxed into a double when it is involved in a numerical computation.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a side note: you should prefer ArrayList to Vector. ArrayList was introduced as part of the Collections framework to replace Vector. The methods of ArrayList are not synchronized and it has a different growth rate. It's not a big deal but it's going to be faster in most usages.
 
JR Pearson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone. Not a whole lot of that made sense, aside from the "your book is outdated" part, which I was kind of assuming already.

The class I'm taking is self-guided, and uses a combination of three different books. I've pretty much shelved the one I was using for the "second" book, which explains things much more clearly (not to mention it's been updated for version 5).

I'm going to try my best to avoid the other book unless absolutely necessary. I'm going to come back to this question when I get back into this sort of thing. Right now I'm re-teaching myself the basics, 'cause this book explains things so much more clearly. I'm realizing how much the other (bad) book left out.
 
john cahil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all ,

"area = length * width;"

Is it a valid code

I am getting a compilation Error on the same

Please help
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rahul bhatt:
H
I am getting a compilation Error on the same


As the replies above have already implied, this works in Java 5, but not in any earlier version, due to a new Java 5 feature called "autoboxing".
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!