K&B generics doubt
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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,
cmbhatt
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I am confused

-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
cmbhatt
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
| Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |






