aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B generics doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B generics doubt" Watch "K&B generics doubt" New topic
Author

K&B generics doubt

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi All,
Source: k&b

Look at the following code below

public static void main(String [] args)
{
// INSERT DECLARATION HERE

for(int i=0; i<=10; i++)
{
List<Integer> row=new ArrayList<Integer>();
for(int j=0; j<=10; j++)
row.add(i * j);
table.add(row);

}
for(List<Integer> row :table)
{
System.out.println(row);
}
}
one of the option is

List<List<Integer>> table =new ArrayList<List<Integer>>();

I didnt get the option. What does this mean List<List<Integer>> and this ArrayList<List<Integer>>(). Can anybody explain me?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
table.add(row); // Line #1

If you see the Line #1, table.add(row); It directly means we want table
to be something where we could add a List itself; Each element of the table
would be a List itself. Till now what you saw is, each element of list being
Integer, String, Animal,Dog or so. List is also an object so it can also
be an element of the List.

Each object (List) added to the "table" would be holding Integer objects.
So we have

List<List<Integer>> table = new ArrayList<List<Integer>>();

It can't be
List<List<Integer>> table = new ArrayList<ArrayList<Integer>>();
Because you want to add List and List can't be added to where ArrayList
is needed. This is only applicable when you write

ArrayList<Integer> row = new ArrayList<Integer>();

Polymorphism applies to base type. If List is required you can pass
ArrayList but vice versa is not true.


Thanks,
[ May 15, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Chandra,
I did not understand the below statement.

It can't be
List<List<Integer>> table = new ArrayList<ArrayList<Integer>>();
Because you want to add List and List can't be added to where ArrayList
is needed
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by nik arora:
Hi Chandra,
I did not understand the below statement.

It can't be
List<List<Integer>> table = new ArrayList<ArrayList<Integer>>();
Because you want to add List and List can't be added to where ArrayList
is needed


OK, See the code below:



Thanks,
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi chandra,
I am confused
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hey

nik
Do'nt be confused

Chandra is saying Polymorphism applies to base type only



List<List<Integer>> table = new ArrayList<List<Integer>>();

here you are saying the elements which are present in the List (see the italic one) are List elements.Not ArrayList elements.CAN WE ASSIGN THE OBJECT OF SUPER CLASS TO THE REFERENNCE OF SUBCLASS.Think about this once.

Thanks

Anil Kumar
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,

Adding to Anil's description:

See the code below: You may get the concept much clearly.




Integer implements Comparable
or
Integer IS-A Comparable

Thanks,
[ May 15, 2007: Message edited by: Chandra Bhatt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by nik arora:
Hi chandra,
I am confused


This tends to happen when the answer to the original question is "enhanced" with extra information which the questioner has not asked for. It takes time to absorb these new concepts and it is easy to get too complex too fast.

It can also happen when an example piece of code gets "changed slightly" into a variation of the original problem.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Yeming Hu
Ranch Hand

Joined: May 14, 2007
Posts: 37
Originally posted by nik arora:

one of the option is

List<List<Integer>> table =new ArrayList<List<Integer>>();

I didnt get the option. What does this mean List<List<Integer>> and this ArrayList<List<Integer>>(). Can anybody explain me?


List<List<Integer>> -- a List of Lists of Integers
ArrayList<List<Integer>> -- an ArrayList of Lists of Integers

List has the following implementation class: ArrayList, LinkedList. ArrayList is a List, but a List is not necessary an ArrayList. Therefore List<List<Integer>> can mean a List of ArrayLists of Integers (List<ArrayList<Integer>> ) or a list of LinkedLists of Integers(List<LinkedList<Integer>> ) .
[ May 16, 2007: Message edited by: Yeming Hu ]

Best Wishes,<br /> Yeming
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B generics doubt