Выводим массив в отсортированном по возрастанию веса порядке в Java. Где ошибка?
Подскажите пожалуйста, какие у меня ошибки в сортировке людей в массиве по возрастанию их веса?
public class Main {
public static void main(String[] args) {
Human[] humans = new Human[9];
for (int i = 0; i < humans.length; i++) {
humans[i] = new Human();
humans[i].setName("User" + i);
humans[i].setWeight(100-i);
}
public static void selectionSort(Human[] humans){
for (int i = 0; i < humans.length; i++) {
int min = humans[i].getWeight();
int minIndex = i;
for (int j = i + 1; j < humans.length; j++) {
if (humans[j].getWeight() < min) {
min = humans[j].getWeight();
minIndex = j;
}
}
int temp = humans[i].getWeight();
humans[i].getWeight() = humans[minIndex];
humans[minIndex] = temp;
}
}
for (int i = 0; i < humans.length; i++) {
System.out.println(humans[i].getName() + " " + humans[i].getWeight() + " ");
}
}
}
Ответы (2 шт):
Автор решения: btwlav
→ Ссылка
public class Test {
public static void main(String[] args) {
Human[] humans = new Human[9];
for (int i = 0; i < humans.length; i++) {
//перепишите заполнение веса
//humans[i] = new Human("User" + i, 100 - i);
humans[i] = new Human("User" + i, (int)(Math.random() * 100));
}
selectionSort(humans);
for (Human human : humans) {
System.out.println(human.getName() + " " + human.getWeight() + " ");
}
}
public static void selectionSort(Human[] humans){
for (int i = 0; i < humans.length - 1; i++) {
int minIndex = i;
for (int j = i; j < humans.length; j++) {
if (humans[j].getWeight() < humans[minIndex].getWeight()) {
minIndex = j;
}
}
//и сортировку
//int temp = humans[i].getWeight();
//humans[i].setWeight(humans[minIndex].getWeight());
//humans[minIndex].setWeight(temp);
Human temp = humans[i];
humans[i] = humans[minIndex];
humans[minIndex] = temp;
}
}
}
class Human {
private String name;
private int weight;
Human(String name, int weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
Автор решения: and73
→ Ссылка
Вот рабочий код
public class Main {
public static void main(String[] args) {
Human[] humans = new Human[9];
for (int i = 0; i < humans.length; i++) {
humans[i] = new Human("User" + i, (int)(Math.random() * 100));
}
selectionSort(humans);
for (Human human : humans) {
System.out.println(human.getName() + " " + human.getWeight() + " ");
}
}
public static void selectionSort(Human[] humans){
for (int i = 0; i < humans.length - 1; i++) {
int minIndex = i;
for (int j = i; j < humans.length; j++) {
if (humans[j].getWeight() < humans[minIndex].getWeight()) {
minIndex = j;
}
}
Human temp = humans[i];
humans[i] = humans[minIndex];
humans[minIndex] = temp;
}
}
}
public class Test {
}
class Human {
private String name;
private int weight;
Human(String name, int weight) {
this.name = name;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
Спасибо большое всем участникам и отдельно @btwlav!