• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Generic method return type problem

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i wrote this code myself and i couldn't get it to compile with the <? super TestArrayList> method argument type, <? extends TestArrayList> doesn't work either is it because im adding to the list inside the method?, it seems that the problem is from the return type? please advice


indenting repaired by Bu.
[ September 03, 2007: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!

The problem lies here:


processList's method declaration states that it will return an
ArrayList<TestArrayList>
but you tried to return an array list of unknown.
This is not possible.

Either change the return type to <? super TestArrayList> or the paramter type to
processList(ArrayList<TestArrayList> input) { ...



By the way, Louie,
I repaired the indenting of your original posting for the sake of readability.

Service of the house, you are a new rancher.

Welcome to the Ranch!






To indent your code properly by yourself:

At the posting page, mark the part of your posting that should be indented. Usually that will be your code (or some tabellaric output).

Hit the - Button below.
that's it!


Yours,
Bu.
 
Louie Cheung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
i tried to change the return type to <? extends TestArrayList> but still i get compiler error like this:

java.util.ArrayList<capture of ? super TestArrayList>
output = processList(output);
^

why? i know changing the parameter to <TestArrayList> would work but i want to know how the wildcard works here, thanks a bunch
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Louie Cheung tried


to change the return type to <? extends TestArrayList>


super...

Bu.
 
Louie Cheung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Burkhard Hassel:
Louie Cheung tried

super...

Bu.



yes i tried, and super too Burkhard Hassel
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, I took a look and figurered out the problem:

You probably have this as the signature now:

public static ArrayList<? super TestArrayList> processList(ArrayList<? super TestArrayList> input)

but still the compiler complains about

output = processList(output);
^

Now go take a look at the output declaration:
ArrayList<TestArrayList> output

The reason that compiler complains is that ouput should be only refer to Type TestArrayList but the method return from processList is TestArrayList or its super types. Which creates conflict. One quick way to fix is also change the declaration of output to:
ArrayList<? super TestArrayList> output = new ArrayList<TestArrayList>();

or strip ? super from all of them
 
Louie Cheung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks burkhard/tony
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic