Sorting sorrows / algorithm-like problem
Here is my problem:
I am trying to sort a known number of integers of different values, into "fronts." The first "front" contains all the (index locaitons of) integers with the lowest value; the second "front" contains all the integers of the next highest value; etc. etc. The worst case scenario is that if all the integers are of different values, there will be as many "fronts" as integers.
For instance, if the integers are: 0, 4, 12, 4, 1, 3, 9, 4 -- I would need to have the following infomration:
Front 1 = 0( as 0 has index location 0 )
Front 2 = 4( as 1 has index location 4 )
Front 3 = 5( as 3 has index location 5 )
Front 4 = 1, 3, 7 ( as there are 4's in these locations )
Front 5 = 6( as 9 has index location 6 )
Front 6 = 2( as 12 is in index location 2 )
The question is: What is the best way for me to store and process this? I am currently doing it with arrays and ArrayLists, but it is not very pretty. Does anyone have any suggestions? It looks simple, and probably is, but I am having difficulty with the number of fronts being different from the number of integers, and the fact that fronts can contain 1 to N numbers.
One way to do this is to replace your list of integers with a list objects containing your integers and the index position
ie
(0,0),(4,1),(12,2),(4,3),(1,4),(3,5),(9,6),(4,7)
then sort the list by your integer values
(0,0),(1,4),(3,5),(4,1),(4,3),(4,7),(9,6),(12,2)
to retrieve your fronts, just iterate over the list and chang fronts whenever your integer value changes
Sounds like an excellent idea. I will implement it and write back how things turned out. ; 0 )
I am having some trouble implementing the code... as I haven't
worked at all with Objects before... can anyone see what is
going wrong here-- see Problem 1 and Problem 2 in code below?
// My attempt at an Object that contains value and index.
public class Joined{
private int index;
private int value;
public Joined( int value, int index ){
this.index = index;
this.value = value;
}
public int value( ){
return this.value;
}
public int index( ){
return this.index;
}
}
And here is the program:
import java.util.*
public class JoinNumbers{
private int index, value;
public static void main( String [] args ){
Joined j = new Joined( 4, 0 );
Joined x = new Joined( 6, 1 );
Joined a = new Joined( 9, 2 );
Joined b = new Joined( 2, 3 );
ArrayList nums = new ArrayList( 4 );
nums.add( j );
nums.add( x );
nums.add( a );
nums.add( b );
// Problem 1: This does not sort properly.
//Collections.sort( nums );
Iterator iterator = nums.iterator();
while(iterator.hasNext()){
Object item = iterator.next();
System.out.println( );
System.out.print( item );
// Problem 2: Can't seem to be able to print
// the value, or index position.. .only the Object reference,
// how do i get the value? and get the index?
}
}
}
Sorted out the first part: (no pun intended!) Now to see if I can get it to be of any use.
public class front{
private int value, index;
public front( int valueIn, int indexIn ){
value = valueIn;
index = indexIn;
}
public int getValue( Object x ){
front d = (front) x;
return d.value;
}
public int getIndex( Object x ){
front d = (front) x;
return d.index;
}
}
Your front object will need to override Object.toString() and use one of the Comparable interface.
public class Joined implements Comparable
{
private int index;
private int value;
public Joined( int value, int index )
{
this.index = index;
this.value = value;
}
public int value( )
{
return this.value;
}
public int index( )
{
return this.index;
}
public String toString()
{
//How ever you want it to print.
}
public int compareTo(Object o) throws ClassCastException
{
if (o instanceOf Joined)
{
Joined j = (Joined)o; //So you can call o's methods.
//How ever these objects compare.
}
else
throw new ClassCastException("Cannot cast " + o.getClass()
+" to Joined";
}
}
BTW, An object's default toString() method (the one inherited from Object) gives you what you were refering to in your last comment.