• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

whats the error in the program(java generics)

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class abc
{
public static void main(String[] args)
{
Collection<?> coll = new ArrayList<String>();
coll.add("charan");
coll.add("sri");
System.out.println(coll.get(1));
}
}



and the error message is

abc.java:5: cannot find symbol
symbol : class Collection
location: class abc
Collection<?> coll = new ArrayList<String>();
^


abc.java:5: cannot find symbol
symbol : class ArrayList
location: class abc
Collection<?> coll = new ArrayList<String>();

I am using jdk 5.0 but still not able to compile the program.Any one please tell me whats the problem.

Thanks,
Charan.
[ September 21, 2005: Message edited by: charan sri ]
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charan,

First hint. What package contains Collection and ArrayList? How do you tell a Java program to use a particular package? (And if you don't know, feel free to ask)

There are still more problems after that, but you will learn more stepping through the problems one by one.

Regards, Andrew
 
charan sri
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew ,

first thanks for the immediate response Andrews.


I Imported both java.util.* and java.lang.*

if i use the below statement it's working fine .

ArrayList<String> coll = new ArrayList<String>(); instead of
Collection<String> coll = new ArrayList<String>();

again if i use
ArrayList<?> coll = new ArrayList<String>(); it's giving errors


I want to know how we use wild card characters?



Thanks,
Charan.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not import java.lang.*; is is already imported. What is the error you are getting now?
You can write:

Collection coll = new ArrayList<String>();

But your code will not compile because Collection does not have a get(int) method.
[ September 21, 2005: Message edited by: Barry Gaunt ]
 
charan sri
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,
Thanks for your reply.


ok I removed the import statement.

but the error if i use

ArrayList<?> coll = new ArrayList<String>(); in the program is


abc.java:9: cannot find symbol
symbol : method add(java.lang.String)
location: class java.util.ArrayList<capture of ?>
coll.add("charan");
^

abc.java:10: cannot find symbol
symbol : method add(java.lang.String)
location: class java.util.ArrayList<capture of ?>
coll.add("sri");
^


If I use
ArrayList<String> coll = new ArrayList<String>(); it's not giving any error.


Thanks,
Charan.
 
charan sri
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akshay,

May be the declarations are right.
Would you please tell me how will you insert data into the arraylist if u declare as you said.

Thanks,
Charan.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks as if you cannot add elements to a collection of type Collection<?>.
See http://www.langer.camelot.de/GenericsFAQ/FAQSections/ParameterizedTypes.html#FAQ304
[ September 21, 2005: Message edited by: Barry Gaunt ]
 
charan sri
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,

If u cannod add elements using wildcard instantiation then whats the use of it.

Thanks,
Charan.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know - I'll guess we will have to read the FAQ from start to finish.
 
charan sri
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok Barry I am in the middle of reading Angelika FAQ rite now.May be I will get an answer after completely reading it.]

Thanks,
Charan.
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Charan
Collections<String> coll= new ArrayList<String>();
is right, but then, the methods of the List interface will not be available to coll, therefore use
List<String> coll= new ArrayList<String>();
but even now the methods available to ArrayList and not List like trimToSize() will not be available.

This interface declaration is used only for interoperability. i.e when you want to use different implementaions of List(or Collections) for performance purposes.

now look,
if I had a Collections<String> coll= new ArrayList<String>();
and at some later point of time I would like to remove duplicates, I can just say

coll = new HashSet(coll);

and I will never have to break the existing code, but this comes at a price. Simply because you donot have a get() method in Set. In other words, the compiler tries to prevent all operations that are not available to collection because it is not sure what implementation that reference might hold at a given time.

now for the wildcards thing.
the declarations are valid alright, but ? just means "unknown type"
so it will never allow you to add anything, because it never knows what type will be added- and that is the purpose of generics- to eliminate inserting types that might be incompatible.

The only reasons I can think (I don't have much experience in this area) of is to create a
List<?> list= new ArrayList<String>(oldlist);
and then
list.get(i);

and

when you want to pass it as an argument for a generic method and you don't know what exact kind you will be passing, except that it is going to be a List (or whatever you have in mind)

I'll give you a very trivial example
{
List<String> list1= new ArrayList<String>();

List<?> listToDump= list1
dump(listToDump);
List<Character> list1= new ArrayList<Character>();
listToDump= list2
dump(listToDump);
List<Integer> list1= new ArrayList<Integer>();
listToDump= list3
dump(listToDump);
}
static <T> void dump(List<T> ls){
ls.clear();
}

of course other uses are bounded wildcards and parametrized methods
you'll have to look deeper to know about them. Try starting with the generics tutorial.

another trivial example is
public static int count_1(List<?> list) {
int count = 0;
for (Object n : list) {
count++;
}
return count;
}
[ September 21, 2005: Message edited by: Akshay Kiran ]
 
charan sri
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the Info Akshay.
The above description gave me a good idea about the basic wildcard usage
If I have any doubts regarding them i will post them here.

Thanks,
Charan.
 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic