Arron Starmer Arron Starmer - 24 days ago 9
Java Question

Sort objects highest-lowest in Java with ArrayList?

Write a method to return the Toy that occurs in the list most frequent and another method to sort the toys by count.

This is my code

import java.util.ArrayList;

public class ToyStore {
private ArrayList<Toy> toyList;

public ToyStore() {
}

public void loadToys(String toys) {
toyList = new ArrayList<Toy>();
for (String item : toys.split(" ")) {
Toy t = getThatToy(item);
if (t == null) {
toyList.add(new Toy(item));
} else {
t.setCount(t.getCount() + 1);
}
}
}

public Toy getThatToy(String nm) {
for (Toy item : toyList) {
if (item.getName().equals(nm)) {
return item;
}
}
return null;
}

public String getMostFrequentToy() {
int position = 0;
int maximum = Integer.MIN_VALUE;
for (int i = toyList.size() - 1; i >= 0; i--) {
if (toyList.get(i).getCount() > maximum)
maximum = toyList.get(i).getCount();
position = i;
}
return toyList.get(position).getName();
}

public void sortToysByCount() {
ArrayList<Toy> t = new ArrayList<Toy>();
int count = 0;
int size = toyList.size();

for (int i = size; i > 0; i--) {
t.add(new Toy(getMostFrequentToy()));
t.get(count).setCount(getThatToy(getMostFrequentToy()).getCount());
toyList.remove(getThatToy(getMostFrequentToy()));
count++;
}

toyList = t;
}

public String toString() {
return toyList + "" + "\n" + "max == " + getMostFrequentToy();
}
}


Here is the method I care about

public void sortToysByCount() {
ArrayList<Toy> t = new ArrayList<Toy>();
int count = 0;
int size = toyList.size();

for (int i = size; i > 0; i--) {
t.add(new Toy(getMostFrequentToy()));
t.get(count).setCount(getThatToy(getMostFrequentToy()).getCount());
toyList.remove(getThatToy(getMostFrequentToy()));
count++;
}

toyList = t;
}


Here is my output

[sorry 4, bat 1, train 2, teddy 2, ball 2]


Here is what I want

[sorry 4, train 2, teddy 2, ball 2, bat 1];


What is wrong in my code? How do I do it?

Answer

The problem is in your getMostFrequentToy() method:

Replace

        if (toyList.get(i).getCount() > maximum)
            maximum = toyList.get(i).getCount();
        position = i;

with

        if (toyList.get(i).getCount() > maximum) {
            maximum = toyList.get(i).getCount();
            position = i;
        }

because you want to get the position that corresponds to that maximum.

Comments