0

I am new to java and want to create a resultlist that looks something like this:

First the user inputs the event, then the list of results are shown (placement to the left, result and name).

Command> Javelin
Results for Javelin
1 86,00 William Mandela
2 83,50 Arthur Dent
3 63,00 Donald Dublett
4 58,00 Hawkeye Pierce
5 40,00 Ada Lovelace

If there are two or more identical results the Athletes share the placement.

 Command> 100 meters
Results for 100 meters
1 11,30 Donald Dublett
2 12,40 Tricia Mcmillan
2 12,40 Alan Turing
4 13,00 William Mandela
5 15,00 Arthur Dent
5 15,00 Ada Lovelace
7 16,00 Hawkeye Pierce

This is what i got so far, but it does not work.

for (int m = 1; m < ResultList.gamesResults.size(); m++) {
                    System.out.println(counter++ + ". " + ResultList.gamesResults.get(m-1).getResult());
                    if (ResultList.gamesResults.get(m).getResult() == ResultList.gamesResults.get(m-1).getResult()) {
                      System.out.println(counter + ". " + ResultList.gamesResults.get(m).getResult());
                    }

                } 

The name part is not important right now. What im trying to do is kind of a bubble sort, but instead i just check for same results. What do you guys think? Im i wasting my time on this? It feels like im on the right track. What im i missing?!

I don't want to use the Comparator interface, so you know.

3 Answers3

1

I don't know why you dont want to use Comparator. You need to write a class called Results :

private class Results
{
    double Result;
    String name;
    int rank;
}

Then build an arraylist as :

List<Results> list = new ArrayList<Results>();

Then sort them using a comparator that compares based on "Result" field like

Collections.sort( list , CustomComparator<Results> );

then now about the positions: You have the sorted list now. Iterate over them and do the below :

Iterator<Results> it = list.iterator();
int position = 0;
double toCompare = 0;
boolean changePosition = false;
while ( it.hasNext() )
{
   Results results = it.next();
   if( results.result > toCompare )
   {
      changePosition = true;
   } 
   else if( results.result == toCompare )
   {  
      changePosition = false;

   }

   if( changePosition )
   {
      results.rank = position++;
   }
   else
   {
      results.rank = position;
   }

   toCompare = results.result;

  }

I hope you get the idea.

Then print the entire arraylist.

SomeDude
  • 13,876
  • 5
  • 21
  • 44
0

First of all, why you don't want to use Comparator interface? Secondly, you can use Collections.reverseOrder() method along with Collections.sort() in order to sort the list in decreasing order:

Collections.sort(arraylist, Collections.reverseOrder());

However the reverse order sorting can also be done as following:

Collections.sort(list);
Collections.reverse(list);

This way the list will be sorted in ascending order first and then it will be reversed. Always store sorted array list and just return it when you need it.

m.aibin
  • 3,528
  • 4
  • 28
  • 47
0

Why not just sort and check and see if the next item is the same as the current one? You can write a basic bubble sort relatively easily. Although, I don't know why you would. Bubble sort is terrible.

Basically have a Boolean flag that says if something on this pass over the array has been swapped. Then you go through the array and say if item[i+1] > item[i] then swap and change the flag to false. If you make it to the end and the flag is still true, quit.

Then, iterate through one more time to print everything out and only increment your counter if item[i+1] != item[i].

Araymer
  • 1,315
  • 1
  • 10
  • 16