Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Arrays and Generics

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Arrays and Generics

    I'm having trouble with a generic method that can accept ArrayLists of type integer and type double.

    The program fails to compile because it says myList is of type "T" and i is of type "double".

    Here's a snippet of code:

    public double calculateTotals(ArrayList<T> list)
    {
           private ArrayList<T> myList = new ArrayList<T>(10);
           myList = list;
     
           for (double i : myList) {
            ....do stuff
           }
    }
     
    public static void main(String args[])
    {
    ArrayList<Double> doubleArrayList = new ArrayList<Double>(10);
    ...populate array...
     
    double totals = calculateTotals(doubleArrayList);
     
    }

    I'm trying to figure out what I'm doing wrong. I thought myList would become an arraylist of type double since that's what I'm passing in.

    Any clues would be greatly appreciated!

    Thanks!


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Arrays and Generics

    First, you can't iterate over a generic list expecting it to be a double (this is the compile time checking at work that makes generics so powerful). You need to iterate over the list using the generic
    for ( T t : myList ){
    //do something
    }

    Second, you are accessing the method in a static way - define it as static or define a class that implements the method. Lastly, I presume based upon the function name you want to calculate the total regardless of whether the list contains int's, double's, etc...in which case you might want to have T extends Number, so you can use the doubleValue method to calculate the total

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Arrays and Generics

    Oh ok, that makes sense.

    I changed my for loop to use type "T"...

    once in the loop, I need to apply math operations to the data, but I can't use addition, subtraction, etc on type "T". How does one do math operations on generic types?

    Thanks!

    Quote Originally Posted by copeg View Post
    First, you can't iterate over a generic list expecting it to be a double (this is the compile time checking at work that makes generics so powerful). You need to iterate over the list using the generic
    for ( T t : myList ){
    //do something
    }

    Second, you are accessing the method in a static way - define it as static or define a class that implements the method. Lastly, I presume based upon the function name you want to calculate the total regardless of whether the list contains int's, double's, etc...in which case you might want to have T extends Number, so you can use the doubleValue method to calculate the total

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Arrays and Generics

    Ok, well I figured my other issue out myself...basically when using:
    for (T t : myList)

    I had to do this when performing math operations:

     
    total = (Double)t - constantRate;

  5. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Arrays and Generics

    Read my post above again - if you defined T to extends Number, there will be no need to cast (which defeats the purpose of generics anyways)
    see Number (Java Platform SE 6)
    public class MyClass<T extends Number>{
       public void myFunction(List<T> list){
     
         for ( T t : list ){
             //use a method defined by number here
         }
       }
    }

Similar Threads

  1. How to use Generics (1 generic anyway)
    By javapenguin in forum The Cafe
    Replies: 4
    Last Post: February 7th, 2011, 05:15 PM
  2. Generics problem
    By ankur.trapasiya in forum Collections and Generics
    Replies: 1
    Last Post: January 22nd, 2011, 01:52 PM
  3. Generics
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 21
    Last Post: December 6th, 2010, 06:08 PM
  4. Trying to somehow Compare Generics
    By Omega_ryan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:58 PM
  5. Generics and Reflection
    By Kassiuz in forum Collections and Generics
    Replies: 3
    Last Post: March 15th, 2010, 09:32 AM