Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Custom array li...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 6 Topic 15881 of 16566
Post > Topic >>

Custom array list not sorting with Collections.sort() + SSCCE

by Karsten Wutzke <kwutzke@[EMAIL PROTECTED] > Mar 13, 2008 at 03:08 PM

Hello all!

I've written a custom array list that simply denies adding null
elements and duplicates. I build lists with string, which at some
point need to get sorted alphanumerically (using Collections.sort).

However, when using the custom list with Collections.sort, the list is
*not* sorted, but standard array list is...

Does anyone know why it is not sorted?


Here's a SSCCE:


im****t java.util.*;

/**
 * An array list that does not allow duplicate and null elements.
 *
 * @[EMAIL PROTECTED]
 kw
 *
 * @[EMAIL PROTECTED]
 <E>
 */
public class NoDupesNoNullsArrayList<E> extends ArrayList<E>
{
	public static void main(String[] strArgs)
	{
		String[] strs = {"one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "ten", "ten"};

		System.out.println("Number of strings: " + strs.length);

		List<String> lsNormal = new ArrayList<String>();
		List<String> lsCustom = new NoDupesNoNullsArrayList<String>();

		//some manual null adds to show the custom list should work
		lsCustom.add(null);
		lsCustom.add(null);
		lsCustom.add(null);
		lsCustom.add(null);
		lsCustom.add(null);
		lsCustom.add(null);

		//add all
		for ( String str : strs )
		{
			lsNormal.add(str);
			lsCustom.add(str);
		}

		System.out.println("Number of list entries: normal = " +
lsNormal.size() + ", custom = " + lsCustom.size());

		//now do standard alphanumeric sort
		Collections.sort(lsNormal);
		Collections.sort(lsCustom);

		//result should be: eight, five, four, nine, one, seven, six, ten,
three, two

		System.out.println();
		System.out.print("Normal: ");

		//print normal sorted
		for ( String str : lsNormal )
		{
			System.out.print(str + ", ");
		}

		System.out.println();
		System.out.print("Custom: ");

		//print custom sorted
		for ( String str : lsCustom )
		{
			System.out.print(str + ", ");
		}

	}


	public NoDupesNoNullsArrayList()
	{
		super();
	}

	public NoDupesNoNullsArrayList(Collection<? extends E> cln)
	{
		super(cln);
	}

	public NoDupesNoNullsArrayList(int initialCapacity)
	{
		super(initialCapacity);
	}

	@[EMAIL PROTECTED]
 boolean add(E elem)
	{
		if ( elem == null )
		{
			return false;
		}

		if ( contains(elem) )
		{
			return false;
		}

		return super.add(elem);
	}

	@[EMAIL PROTECTED]
 void add(int index, E elem)
	{
		if ( elem == null )
		{
			return;
		}

		if ( contains(elem) )
		{
			return;
		}

		super.add(index, elem);
	}

	@[EMAIL PROTECTED]
 boolean addAll(Collection<? extends E> cln)
	{
		return addAll(size(), cln);
	}

	@[EMAIL PROTECTED]
 boolean addAll(int index, Collection<? extends E> cln)
	{
		if ( cln == this )
		{
			throw new IllegalArgumentException("Collection is this!");
		}

		if ( cln == null || containsAll(cln) )
		{
			return false;
		}

		//build list without dupes (always, no case as in NoNulls... lists)
		ArrayList<E> al = new ArrayList<E>(cln.size());

		Iterator<? extends E> itr = cln.iterator();

		while ( itr.hasNext() )
		{
			E elem = itr.next();

			if ( elem != null && !contains(elem) )
			{
				al.add(elem);
			}
		}

		cln = al;

		//allows dupes and nulls
		return super.addAll(index, cln);
	}

	@[EMAIL PROTECTED]
 E set(int index, E elem)
	{
		if ( elem == null )
		{
			return null;
		}

		if ( contains(elem) )
		{
			return null;
		}

		return super.set(index, elem);
	}

}


It might not appear THAT short, just don't care too much about the
array list implementation...

Any ideas?

Karsten
 




 6 Posts in Topic:
Custom array list not sorting with Collections.sort() + SSCCE
Karsten Wutzke <kwutzk  2008-03-13 15:08:54 
Re: Custom array list not sorting with Collections.sort() + SSCC
Steven Simpson <ss@[EM  2008-03-13 22:54:23 
Re: Custom array list not sorting with Collections.sort() + SSCC
Roedy Green <see_websi  2008-03-14 01:08:25 
Re: Custom array list not sorting with Collections.sort() + SSCC
Roedy Green <see_websi  2008-03-14 01:09:52 
Re: Custom array list not sorting with Collections.sort() + SSCC
Karsten Wutzke <kwutzk  2008-03-15 01:55:59 
Re: Custom array list not sorting with Collections.sort() + SSCC
Roedy Green <see_websi  2008-03-15 18:14:58 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 16:01:25 CST 2008.