• 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

K&B generics doubt

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chandra,
I am confused
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
I once met a man from Nantucket. He had a tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic