asiavoices
Programmer
Hello all,
I have a strange situation where when I try to add an object to a TreeSet collection, its only adding the first (1st) one and ignores the rest. There were no errors, etc. when compiling.
The funny thing is that when I use ArrayList, its okay (but not sorted as expected).
Here's part of my code:
public class MyApp
{
private TreeSet mylist;
public MyApp()
{
......
}
public static void main(String[] args)
{
MyApp myapp = new MyApp();
myapp.test();
}
public void test()
{
// Create my stuff objects
doStuff();
System.out.println("\n"
}
public void doStuff()
{
mylist = new TreeSet();
// add 1st element
Car c1 = new Car();
try
{
c1.setCode("12345678"
}
catch (Exception e){
System.out.println(e.toString());
}
try
{
c1.setName("Chrysler"
}
catch (Exception e)
{
System.out.println(e.toString());
}
// add 2nd element
Car c2 = new Car();
try
{
c2.setCode("fg1951235"
}
catch (Exception e){
System.out.println(e.toString());
}
try
{
c2.setName("Ford"
}
catch (Exception e)
{
System.out.println(e.toString());
}
// add 3rd element
Car c3 = new Car();
try
{
c3.setCode("2458878921"
}
catch (Exception e){
System.out.println(e.toString());
}
try
{
c3.setName("BMW"
}
catch (Exception e)
{
System.out.println(e.toString());
}
mylist.add(c1);
mylist.add(c2);
mylist.add(c3);
System.out.println("\nNo. of elements: " + mylist.size());
System.out.println("\nContents in the SortedSet: " + mylist);
Iterator thelist = mylist.iterator();
while (mylist.hasNext())
{
Car currentcar = (Car) thelist.next();
System.out.println("Car code:" + currentcar.getCode() + "\nName: " + currentcar.getName().trim());
}
} // end of MyApp
Results displayed: ==================
No. of elements: 1
Contents in the SortedSet: [null] <-- this is weird should be something like [Car@12453] or similar.
Car code: 12345678
Name: Chrysler
Any ideas why this may be happening?
Cheers,
Christopher
I have a strange situation where when I try to add an object to a TreeSet collection, its only adding the first (1st) one and ignores the rest. There were no errors, etc. when compiling.
The funny thing is that when I use ArrayList, its okay (but not sorted as expected).
Here's part of my code:
public class MyApp
{
private TreeSet mylist;
public MyApp()
{
......
}
public static void main(String[] args)
{
MyApp myapp = new MyApp();
myapp.test();
}
public void test()
{
// Create my stuff objects
doStuff();
System.out.println("\n"
}
public void doStuff()
{
mylist = new TreeSet();
// add 1st element
Car c1 = new Car();
try
{
c1.setCode("12345678"
}
catch (Exception e){
System.out.println(e.toString());
}
try
{
c1.setName("Chrysler"
}
catch (Exception e)
{
System.out.println(e.toString());
}
// add 2nd element
Car c2 = new Car();
try
{
c2.setCode("fg1951235"
}
catch (Exception e){
System.out.println(e.toString());
}
try
{
c2.setName("Ford"
}
catch (Exception e)
{
System.out.println(e.toString());
}
// add 3rd element
Car c3 = new Car();
try
{
c3.setCode("2458878921"
}
catch (Exception e){
System.out.println(e.toString());
}
try
{
c3.setName("BMW"
}
catch (Exception e)
{
System.out.println(e.toString());
}
mylist.add(c1);
mylist.add(c2);
mylist.add(c3);
System.out.println("\nNo. of elements: " + mylist.size());
System.out.println("\nContents in the SortedSet: " + mylist);
Iterator thelist = mylist.iterator();
while (mylist.hasNext())
{
Car currentcar = (Car) thelist.next();
System.out.println("Car code:" + currentcar.getCode() + "\nName: " + currentcar.getName().trim());
}
} // end of MyApp
Results displayed: ==================
No. of elements: 1
Contents in the SortedSet: [null] <-- this is weird should be something like [Car@12453] or similar.
Car code: 12345678
Name: Chrysler
Any ideas why this may be happening?
Cheers,
Christopher