Results 1 to 6 of 6
Thread: generic parameters
- 03-18-2011, 03:37 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
generic parameters
Hi,
I have a generic method that can take an "integer" or "double" arrayList as a parameter and returns a value of type "double".
It works when I pass a "double" arrayList, but keeps warning about "loss of precision" when I pass an integer arrayList.
Is there anyway around this?
Thanks!
- 03-18-2011, 03:52 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
You implicitly cast something from type double to type int; as in:
You have to make those casts explicit to show the compiler that you know what you're doing and to make it keep its mouth shut.Java Code:double d= 3.5; int i= d; // <--- compiler complains here
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-18-2011, 04:02 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
But what if I have something like this in the generic method:
Where "myArrayList" could by of type integer or of type double.Java Code:for (int i : myArrayList)
How do I let the generic method know whether to use int i or double i?
Thanks!Last edited by TopNFalvors; 03-18-2011 at 04:03 PM. Reason: forgot to say thanks
- 03-18-2011, 04:24 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
In your for-loop you simply assume that your list contains Integers (there's also autoboxing doing some work here) which might not be true, i.e. the list might contain Doubles. So have to anticipate for that:
All in all I think your design is a bit shakey at best.Java Code:for (double d : myArrayList) { // possibly a widening cast to double int i= (int)d; // explicitly cast to int here ... }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-18-2011, 05:09 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 41
- Rep Power
- 0
Thanks,
so how would you pass an arrayList into a generic class? Or is that just a bad idea?
Thanks!
- 03-18-2011, 08:46 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
Generics generally aren't a bad idea; they just can't handle primitive data types very well. If you design a class (or interface) around a type T (not primitive) your class can also handle types C<T> where C is any type that can handle a type T. Think of List<T> or even ArrayList<T> or whatever.
Your example deals with autoboxing as well (without you possibly knowing it) which makes thing even more complex because primitives (such as ints or doubles) cannot be handles by generics.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Generic Question
By kwgivler in forum New To JavaReplies: 5Last Post: 03-10-2011, 10:50 PM -
how to pass parameters from a method to another which accepts to parameters?possible?
By amrmb09 in forum Advanced JavaReplies: 5Last Post: 11-21-2010, 02:08 PM -
generic types
By jon80 in forum New To JavaReplies: 6Last Post: 06-12-2009, 10:29 PM -
Generic methods
By andre1011 in forum Advanced JavaReplies: 7Last Post: 02-25-2009, 02:17 PM -
Generic array
By eva in forum New To JavaReplies: 3Last Post: 12-23-2007, 12:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks