Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

What's wrong with this generic type?!

Thread view: 
Konrad Viltersten - 05 Jun 2005 14:09 GMT
Please regard the following code snip:

Vector[] vec = new Vector[number];
for (int i = 0; i < number; i++) {
  vec[i] = new Vector<Integer> ();
  vec[i].add (new Integer (0)); }

Despite the fact that i used "<Integer>" i still get a warning
telling me that the type is unchecked. Since i got that to
work otherwise, i guess there's something more to in when
it comes to arrays but i'm affraid that's something i need a
little help with.

Signature

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
           as a substitute for coffee

Ambition - a poor excuse for not having
                enough sense to be lazy

---------------------------------------------------

Boudewijn Dijkstra - 05 Jun 2005 17:03 GMT
> Please regard the following code snip:
>
[quoted text clipped - 8 lines]
> it comes to arrays but i'm affraid that's something i need a
> little help with.

One moment you say the type is Vector, the next you say it is Vector<Integer>.
Change the first line to:

Vector<Integer>[] vec = new Vector<Integer>[number];
Konrad Viltersten - 05 Jun 2005 19:33 GMT
>> Vector[] vec = new Vector[number];
>> for (int i = 0; i < number; i++) {
[quoted text clipped - 11 lines]
>
>  Vector<Integer>[] vec = new Vector<Integer>[number];

I already tried that approach  but since i got another
error i didn't mention it. Sorry. Anyhow, i get following
message from the compiler:
"generic array creation"
which, of course, doesn't imply your suggestion is wrong
in any way, but rather that i have an other stupid thing in
my code and don't even know about it, hehe.

Any more info will be warmly appreciated.

Signature

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
           as a substitute for coffee

Ambition - a poor excuse for not having
                enough sense to be lazy

---------------------------------------------------

Boudewijn Dijkstra - 05 Jun 2005 22:10 GMT
>>> Vector[] vec = new Vector[number];
>>> for (int i = 0; i < number; i++) {
[quoted text clipped - 21 lines]
>
> Any more info will be warmly appreciated.

I seem to have given an erroreous correction.  The Generics tutorial
<http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf>, paragraph 7.3, says:

"The component type of an array object may not be a type variable or a
parameterized type, unless it is an (unbounded) wildcard type."

It seems to me that declaring the array as a Vector<?>[] would be the best
general solution.
Konrad Viltersten - 06 Jun 2005 00:57 GMT
>>> One moment you say the type is Vector, the next you say it is
>>> Vector<Integer>. Change the first line to:
[quoted text clipped - 9 lines]
> It seems to me that declaring the array as a Vector<?>[]
> would be the best general solution.

Hmmm... Now i'm confused. Why would it be unallowed to
use the syntax you suggested? I found your explanation
rather logical. What danger arises if that method is used?

Also - i'll try the wildcard approach this evening and we'll
see if that will halp me. Man, i'm really fed up rewriting
the old code of mine just because i updated JVM to 1.5.
Perhaps i'm extra lazy these days, hehe. Thanks!

Signature

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
           as a substitute for coffee

Ambition - a poor excuse for not having
                enough sense to be lazy

---------------------------------------------------

Boudewijn Dijkstra - 06 Jun 2005 19:49 GMT
>>>> One moment you say the type is Vector, the next you say it is
>>>> Vector<Integer>. Change the first line to:
[quoted text clipped - 13 lines]
> use the syntax you suggested? I found your explanation
> rather logical. What danger arises if that method is used?

The example in the beforementioned paragraph explains more about the problems.
The solution that I (and the paragraph) gave, allowes non-Vector<Integer>
elements to be added to the array (if the compiler ever accepts it
(workaround: omit the "<?>")).  Which is not worse than without generics.
Except for the compiler warning.
Konrad Viltersten - 06 Jun 2005 23:06 GMT
>> Hmmm... Now i'm confused. Why would it be unallowed to
>> use the syntax you suggested? I found your explanation
[quoted text clipped - 5 lines]
> array. Which is not worse than without generics. Except for
> the compiler warning.

I'm not sure if i got it right. Do you mean that the warning is to
be regarded as something to live with? I would strongly prefer
a code that doesn't generate any errors nor warnings. Is it
achievable in this case?

Signature

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
           as a substitute for coffee

Ambition - a poor excuse for not having
                enough sense to be lazy

---------------------------------------------------

Stefan Schulz - 07 Jun 2005 19:12 GMT
>>> Hmmm... Now i'm confused. Why would it be unallowed to
>>> use the syntax you suggested? I found your explanation
[quoted text clipped - 10 lines]
> a code that doesn't generate any errors nor warnings. Is it
> achievable in this case?

Only to a point. You will need one unchecked conversion... if only
@SurpressWarnings would work. We really need a way to shut the compiler
about unchecked warnings where they are well considered and unavoidable.

Signature

In pioneer days they used oxen for heavy pulling, and when one ox
couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
be trying for bigger computers, but for more systems of computers.
          --- Rear Admiral Grace Murray Hopper

Konrad Viltersten - 07 Jun 2005 23:30 GMT
>> I'm not sure if i got it right. Do you mean that the warning is to
>> be regarded as something to live with? I would strongly prefer
[quoted text clipped - 5 lines]
> compiler about unchecked warnings where they are well considered
> and unavoidable.

Thanks!

Signature

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
           as a substitute for coffee

Ambition - a poor excuse for not having
                enough sense to be lazy

---------------------------------------------------

Konrad Viltersten - 06 Jun 2005 13:45 GMT
> "The component type of an array object may not be a type
> variable or a parameterized type, unless it is an (unbounded)
> wildcard type."
>
> It seems to me that declaring the array as a Vector<?>[]
> would be the best general solution.

I have tried that as well and there seems to be something
strange as i can't compile the following code.

import java.util.*;
public class Test01 {
public static void main (String[] arg)
{
 Vector<?>[] vecs = new Vector<?>[1];
 vecs[0] = new Vector<Integer> ();
 vecs[0].add (new Integer (13));
}
}

I have experimented with different generics (none, "<?>",
"Integer") and in various constelations and i don't get it
to work - somebody willing to give it a try?

Of what i understand, the line
   Vector<?>[] vecs = new Vector<?>[1];
should not need any generics since i'm reserving a part of
memory for an object of type Vector and whether i'll be
storing Integers or JButtons in there is none of compilers
concern (it can't know how many such elements i'm going
to put in anyway).

So, simply, i'm reserving one memory section for an array
of type Vector. Why on Earth do i need to explain the
generics here?!

I'd like to go
   Vector[] vecs = new Vector[1];
or maybe
   Vector[] vecs = new Vector<Integer>[1];
but the compiler whines then... What to do?

Signature

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
           as a substitute for coffee

Ambition - a poor excuse for not having
                enough sense to be lazy

---------------------------------------------------

tzvika.barenholz@gmail.com - 06 Jun 2005 14:27 GMT
This compile and runs just fine:

import java.util.*;
public class Test01 {
public static void main (String[] arg)
{
 Vector[] vecs = new Vector[1];
 vecs[0] = new Vector<Integer> ();
 vecs[0].add(new Integer (13));
}

}

on my 1.5-02

of course you cannot instantiate a new array of generics. that's one of
the limitations. but still i don't get why the simple good old version
doesn't work for you.
Konrad Viltersten - 06 Jun 2005 16:42 GMT
> This compile and runs just fine:
> import java.util.*;
[quoted text clipped - 11 lines]
> that's one of the limitations. but still i don't get why the
> simple good old version doesn't work for you.

That's what i figure but since i'm a little bit rusty, i
accepted the hints i got.

Now, you say you don't get why the simple good old version
doesn't work for me. Well, it's because:
"warning: [unchecked] unchecked call to add(E) as a member
of the raw type java.util.Vector".
The compiler points at the paranthesis at this spot:
"vecs[0].add    #THE SPOT#   (new Integer (13));

Any ideas? Please note that it's not an error, just a warning
so if you have set your editor not to show warnings, maybe
that's the reason you don't get any crap in the window...
My version is 1.5.0, by the way.

Signature

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
           as a substitute for coffee

Ambition - a poor excuse for not having
                enough sense to be lazy

---------------------------------------------------

hiwa - 06 Jun 2005 03:23 GMT
> Please regard the following code snip:
>
[quoted text clipped - 8 lines]
> it comes to arrays but i'm affraid that's something i need a
> little help with.

I believe this is a compiler bug.
Change the body of the for loop to:

Vector<Integer> tempvec = new Vector<Integer>();
tempvec.add(new Integer(0));
vec[i] = tempvec;

then, it compiles fine. But there's no substantial difference from
your original code.

Vector<Integer> vec[i] = new Vector<Integer> ();
also emits weird error.

> Sleep - thing used by ineffective people
>             as a substitute for coffee
[quoted text clipped - 3 lines]
>
> ---------------------------------------------------
Konrad Viltersten - 06 Jun 2005 08:41 GMT
>> Vector[] vec = new Vector[number];
>> for (int i = 0; i < number; i++) {
[quoted text clipped - 9 lines]
> I believe this is a compiler bug.
> Change the body of the for loop to:

Is it documented somewhere? I have bad experience
blaming the failure of my own code on compilers,
users etc... General rule at our office is - "you wrote
it and since hell still isn't very cold, it's probably
something YOU did wrong". Very sound sometimes.   :)

> Vector<Integer> tempvec = new Vector<Integer>();
> tempvec.add(new Integer(0));
> vec[i] = tempvec;

Two questions here.
1. Let's say i change the body of the for-loop. What do
i do about the declaration outside of it? Will i have to
use "<?>" anyway?
2. Won't the code you suggested go twice as slow, since
we reserve memory twice for every created object?

> Vector<Integer> vec[i] = new Vector<Integer> ();
> also emits weird error.

Yes, i was wonder - what DOES it mean? I remember an
error message "cannot resolve symbol" which ment "oh,
boy, you f"#!d up big time with instance variables" and
this one seems to be of the same informativeness, hehe.

Signature

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
           as a substitute for coffee

Ambition - a poor excuse for not having
                enough sense to be lazy

---------------------------------------------------



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.