Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Sorting the Collection(java.util.Collection) of Elements (http://www.velocityreviews.com/forums/t390313-sorting-the-collection-java-util-collection-of-elements.html)

Pradeep 01-24-2007 03:45 AM

Sorting the Collection(java.util.Collection) of Elements
 
Hi EveryBody,

Suppose that a collection(Java.util.Collection) contains some number of
elements and I have to iterate through the collection by comparing each
element in the collection with each other element so that no 2 elements
should be same...(Note: No two same elements having the same index
position in the collection shouldn't be eligible for comparison)...If
any one of u have any Idea so please Let Me know that..

Thanks& Regards,
Venkata Pradeep K.


Lew 01-24-2007 04:35 AM

Re: Sorting the Collection(java.util.Collection) of Elements
 
Pradeep wrote:
> Suppose that a collection(Java.util.Collection [sic]) contains some number of
> elements and I have to iterate through the collection by comparing each
> element in the collection with each other element so that no 2 elements
> should be same...(Note: No two same elements having the same index
> position in the collection shouldn't be eligible for comparison)...If
> any one of u have any Idea so please Let Me know that..


I strongly suggest that you read the java.util (note: lower-case 'j') docs:
<http://java.sun.com/javase/6/docs/api/java/util/package-summary.html>

This question has been coming up a lot lately. If this is homework you might
need to understand how to make such a collection. If that is the case, the
correct way to go about doing the homework is not to provide a canned answer
that you don't fully understand, but to think the problem through.

Try first progrmming with a pen[cil] and paper. Think through the questions of
your problem in detail. For example:

If you have to iterate through the collection (is that really the requirement,
or can you check at the insert attempt?), what does that mean? What is
required at each loop of the iteration? What does it mean to "compare each
element in the collection"? Does this require, say, nesting another loop
inside, or is there another way? What do the API docs have to say? What are
the initial conditions: do you only know that there is a Collection, or can
you restrict it to some subtype? If you can, then what subtype would help?
WHat are the differences between different Collections, or even different
implementations of the same Collection (such as the many flavors of List)?

As you break down the problem into teeny, tiny little steps it will become
clearer how to represent those steps in a program.

- Lew

Patricia Shanahan 01-24-2007 02:33 PM

Re: Sorting the Collection(java.util.Collection) of Elements
 
Pradeep wrote:
> Hi EveryBody,
>
> Suppose that a collection(Java.util.Collection) contains some number of
> elements and I have to iterate through the collection by comparing each
> element in the collection with each other element so that no 2 elements
> should be same...(Note: No two same elements having the same index
> position in the collection shouldn't be eligible for comparison)...If
> any one of u have any Idea so please Let Me know that..


Why not just create a HashSet with the elements?

Patricia


All times are GMT. The time now is 07:01 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.