• Post Reply Bookmark Topic Watch Topic
  • New Topic

Type errors with collections  RSS feed

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the first time I've tried to use collections, and I have two typecast related problems.

FIRST PROBLEM:

In a bean, I have an arraylist, where subtopic is a class defined so as to also be a bean, declared thusly:



I can't figure out how to return an array from the getter function, getMyTopics, without getting typcast warnings from the java compiler. I've tried both of the following:



and...



In both cases the compiler says I need to run it with -Xlint, which I've done, but all it tells me is it is unhappy with my typing (an "incompatible types" warning with -Xlint turned on.

I have declared subtopic as a child of Object.

SECOND PROBLEM:

In a utility function for loading the array list, where the intent is to use it for two different types but perform the same fucntion, -Xlint again complains about types. Here it is:



In this case, o can be a subtopic, as above, or another type also derived from Object. With -Xlint, the compiler says:


/WebWork/smsinfo/src/dbMgr/user.java:146: incompatible types
found : java.lang.Object[]
required: dbMgr.tribe[]
public tribe[] getMyTribes(){return myTribes.toArray();}
^
/WebWork/smsinfo/src/dbMgr/user.java:153: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
for(int i= 0; i < o.length; i++) l.add(o[i]);

Thanks for the help!!!
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, after a little more book searching, I fixed the first problem by doing this:



I'm still looking for the correct way to do the second question and make -Xlint quit complaining.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still looking for the correct way to do the second question and make -Xlint quit complaining.


In second case, l is basically a raw list. Use following signature for the loadList() method:



Naseem
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, thanks for the reply!

I tried that, then I get hard errors (not just warnings) when I call load list with a class derived from Object (like the subtopic class I mentioned).
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and I guess I should also point out that I tried casting argument in the calling routine as well. So I've had loadlist like this:



where I get the warning, and like this:



which give me a hard error when I call it with either:

or:


In the first case, I get a type mismatch in the argument list, in the second case the compiler tells me they are inconvertible types.

NOTE AGAIN: subtopic is a descendant of Object.

TIA
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Allen Williams:
Oh, and I guess I should also point out that I tried casting argument in the calling routine as well. So I've had loadlist like this:



where I get the warning, and like this:



which give me a hard error when I call it with either:

or:


In the first case, I get a type mismatch in the argument list, in the second case the compiler tells me they are inconvertible types.

NOTE AGAIN: subtopic is a descendant of Object.

TIA


Is subtopic[]_variable a valid java identifier? No

Try this, it will work.



Naseem
[ September 09, 2006: Message edited by: Naseem Khan ]
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologize most profusely- I messed up my variable names. I didn't use subtopic[]_variable as the name.

Here what I have:



then, there is:



So, you can see I am trying to use loadlist with two different types, which are both descendants of Object.

Thanks...
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I can see what you are trying to achieve. Always try to post complete code not in smalll parts as it makes difficult to understand.

As your type of myTopics and myTribes are ArrayList<subtopic> and ArrayList<tribe> respectively, so definitely you can't use ArrayList<Object> as type in the loadList() method as I suggested you earlier.

You have to go for overloading and provide the exact type. Declare signature like this...



Similary for populating myTribes loadList(ArrayList<tribe> l, tribe[] o)

Naseem
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also make the method a complete generic method

[ September 09, 2006: Message edited by: Garrett Rowe ]
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naseem Khan:
You have to go for overloading and provide the exact type.


I don't think so. I might be wrong, but I think

private <T> void loadList(ArrayList<T> l, T[] o)

should work.

By the way, if l==null and o.length > 0, the code runs into a NullPointerException. So the if statement is probably rather useless as is...?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops that should be:

[ September 09, 2006: Message edited by: Garrett Rowe ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


I don't think so. I might be wrong, but I think

private <T> void loadList(ArrayList<T> l, T[] o)

should work.

By the way, if l==null and o.length > 0, the code runs into a NullPointerException. So the if statement is probably rather useless as is...?


Yes your code will definitely work and in fact its a better approach than going for a separate method declaration for each and every ArrayList parameterized type.


Naseem
[ September 09, 2006: Message edited by: Naseem Khan ]
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh boy! I started learning collections today, and get to move right into generics!

I will go read the book on this, but just for closure here, how do you call this generic?

Also, sorry about the incomplete code earlier. It seems a fine line between a post having a whole bunch of irrelevant stuff and screwing up like I did. Appreciate the patience.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collections in 1.5 and generics are highly coupled topics. To use the method you would just use:



Although for completenes, if the type of List deosn't matter you could also do this.

[ September 09, 2006: Message edited by: Garrett Rowe ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Allen Williams:
I will go read the book on this, but just for closure here, how do you call this generic?

Also, sorry about the incomplete code earlier. It seems a fine line between a post having a whole bunch of irrelevant stuff and screwing up like I did. Appreciate the patience.


Never mind. Cheers Here is one FAQ on Java Generics which you must read. Its goes in great detail, explain each and every areas of java programming using generics like using class literals, arrays in generics etc.

Naseem
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Rowe:
...if the type of List deosn't matter you could also do this.


[ September 09, 2006: Message edited by: Garrett Rowe ]

If the List does have to be an ArrayList, you could pass it to the ArrayList constructor.

ArrayList<Foo> al = new ArrayList<Foo>(list);
 
Allen Williams
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot gentlemen! I will take it from here, though I'm sure I'll be back soon
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!