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