Results 1 to 6 of 6
  1. #1
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default 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!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by TopNFalvors View Post
    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!
    You implicitly cast something from type double to type int; as in:

    Java Code:
    double d= 3.5;
    int i= d; // <--- compiler complains here
    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.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    But what if I have something like this in the generic method:
    Java Code:
    for (int i : myArrayList)
    Where "myArrayList" could by of type integer or of type double.

    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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by TopNFalvors View Post
    But what if I have something like this in the generic method:
    Java Code:
    for (int i : myArrayList)
    Where "myArrayList" could by of type integer or of type double.

    How do I let the generic method know whether to use int i or double i?

    Thanks!
    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:

    Java Code:
    for (double d : myArrayList) { // possibly a widening cast to double
       int i= (int)d; // explicitly cast to int here
       ...
    }
    All in all I think your design is a bit shakey at best.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Thanks,

    so how would you pass an arrayList into a generic class? Or is that just a bad idea?

    Thanks!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by TopNFalvors View Post
    Thanks,

    so how would you pass an arrayList into a generic class? Or is that just a bad idea?

    Thanks!
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Generic Question
    By kwgivler in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 10:50 PM
  2. Replies: 5
    Last Post: 11-21-2010, 02:08 PM
  3. generic types
    By jon80 in forum New To Java
    Replies: 6
    Last Post: 06-12-2009, 10:29 PM
  4. Generic methods
    By andre1011 in forum Advanced Java
    Replies: 7
    Last Post: 02-25-2009, 02:17 PM
  5. Generic array
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 12:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •