• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Array List

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Is the Array List class ordered or unordered? Does ordered mean the same thing as sorted? Or, whatever order i add elements to the Array List object, i get it back in the same order -- is it what we mean by ordered? for ex., if i add in the sequence "5" "4" "3" "2" "1" i get back in the same order.
Also, we expect a ClassCastException to be thrown when the object which is added is of incompatible type. In this context, consider the following code and its output :
<code>
// creating an Array list
/* ArrayList inherits AbstractList which inherits AbstractCollection
and implements the List interface
1. Lists are ordered -- doubtful
2. Lists allow duplicates
3. Lists allow duplicate null also
*/
import java.util.*;
class MyClass
{
private int i;
public MyClass(int j)
{
i=j;
}
}

public class ArrayListEx
{
public static void main(String[] args)
{
// Create the Array List
ArrayList al=new ArrayList();

al.add("First");
al.add("Second");
al.add("Third");
al.add(null);
al.add(null);

System.out.println("Contents of a1 : " + al);

MyClass m = new MyClass(2);
al.add(m);
System.out.println("Contents of a1 : " + al);


}
}
</code>
The output is
Contents of al:[First,Second,Third,null,null]
Contents of al:[First,Second,Third,null,null,MyClass@273d3c]
No ClassCastException was raised.
Any clarifications?g
------------------
Regards,
Shree
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Regarding the above question, TreeSet class raises the ClassCastException. Check the code below.ts.add(m) causes the exception. Also, Hash Set accepts unique null values to be added but TreeSEt does not. Is it due to any special thing associatd with the Tree?
<code>
// creating an Hash Set
/* TreeSet descends from AbstractSEt which inherits
AbstractCotsection
1. Hash set is not ordered
2. Sets don't allow duplicates
3. Sets allow null - but a unique null
*/
import java.util.*;
class MyClass
{
private int i;
public MyClass(int j)
{
i=j;
}
}

public class TreeSetEx
{
public static void main(String[] args)
{
// Create the Array List
TreeSet ts=new TreeSet();

ts.add("First");
ts.add("Second");
ts.add("Third");
//ts.add(null);
// ts.add(null);

System.out.println("Contents of ts : " + ts);

MyClass m = new MyClass(2);
// ts.add(m);
System.out.println("Contents of ts : " + ts);
// System.out.println("The first element is : " + ts.getFirst());
// System.out.println("The last element is : " + ts.getLast());
Iterator it=ts.iterator();
while (it.hasNext())
{
Object o = it.next();
System.out.println(" Element : " + o);
}

}
}
</code>

------------------
Regards,
Shree
[This message has been edited by shree vijay (edited December 17, 2000).]
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TreeSet is backed up by TreeMap which can't accept null key or any key which cannot be compared with the keys currently
in the map.......
TreeSet's add(Object obj) method calls TreeMap's put(Object key,Object vlaue) method and places obj in the place of
key.......
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
So i infer that both TreeMap and TreeSet cannot take null and both give sorted output. What about the ClassCastException issue?
------------------
Regards,
Shree
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ClassCastException is thrown when key cannot be compared with the keys currently in the map.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic