[英]How do I sort an object from an array and print the entire sorted array?
我想對數組的權重 object 進行排序,然后我想打印整個排序后的數組,我該怎么做?
organize() 方法我不知道該怎么做才能對數組進行排序,我不能使用 Print,因為這些在測試中使用。
動物 class
package model;
public class Animal {
private String name;
private double weight;
private String classification;
public Animal(String nam, double wei, String classi) {
name = nam;
weight = wei;
classification = classi;
}
public String getName(){
return name;
}
public double getWeight(){
return weight;
}
public String getClassification(){
return classification;
}
@Override
public String toString(){
return "Animal: " + name + ", Peso: " + weight + "Kg, " + "De tipo: " + classification + " \n";
}
}
動物園 Class
package model;
import java.util.Arrays;
public class Zoo {
private Animal[] animals;
public Zoo() {
animals = new Animal[50];
}
public void addAnimal(Animal animal) {
boolean inserted = false;
for (int i = 0; i < animals.length && !inserted; i++) {
if (animals[i] == null) {
animals[i] = animal;
inserted = true;
}
}
}
public Animal organize() {
}
public double calculateAverage() {
double avg = 0;
int cont = 0;
for (int i = 0; i < animals.length; i++) {
if (animals[i] != null) {
avg = avg + animals[i].getWeight();
cont++;
}
}
avg = avg / cont;
return avg;
}
@Override
public String toString() {
return "Animales:\n" + Arrays.toString(animals);
}
}
測試 class
package test;
import java.util.Scanner;
import model.Animal;
import model.Zoo;
public class Test {
private Scanner console;
private Zoo zoo;
public Test() {
console = new Scanner(System.in);
zoo = new Zoo();
}
public void run() {
showMenu();
}
public void readData() {
Animal animal = readAnimal();
zoo.addAnimal(animal);
}
public Animal readAnimal() {
String name = "";
double weight = 0;
String classification = "";
System.out.println("Ingrese el animal");
name = console.nextLine();
System.out.println("Ingrese el peso del animal en Kg");
weight = Double.parseDouble(console.nextLine());
System.out.println("Ingrese el tipo del animal (mamifero,anfibio, etc)");
classification = console.nextLine();
Animal animal = new Animal(name, weight, classification);
return animal;
}
public void showMenu() {
int option = 0;
do {
String menu = "MENU PRINCIPAL\n\n1. Ingresar animal\n2. Organizar los animales por peso\n3. Busqueda de un grupo de animales por tipo\n4. Ver la informacion\n5. Hallar el promedio de peso de los animales ingresados\n6. Salir";
System.out.println(menu);
System.out.print("Digite la opcion: ");
option = Integer.parseInt(console.nextLine());
switch (option) {
case 1:
readData();
break;
case 2:
System.out.println("Los animales ordenados: ");
break;
case 3:
System.out.println("Aun no terminado");
break;
case 4:
System.out.println(zoo);
break;
case 5:
System.out.println("El promedio de peso de los animales ingresados es: " + zoo.calculateAverage() + " kg.");
break;
case 6:
System.exit(0);
break;
default:
System.err.println("Opcion no valida, intente de nuevo");
break;
}
System.out.println();
System.out.println();
} while (option != 6);
}
public static void main(String args[]) {
Test test = new Test();
test.run();
}
}
OUTPUT
主菜單
進入動物
狗
以公斤為單位輸入動物的體重
20
輸入動物類型(哺乳動物、兩棲動物等)
哺乳動物
我嘗試使用 array.sort 但我不知道如何打印它,我也嘗試使用 for cycles 但我也無法打印它。
如果您只想就地對數組進行排序,可以這樣做:
Arrays.sort(a, Comparator.comparing(Animal::getWeight));
如果您想創建一個按權重排序的新數組,則可以使用 Java 流:
sortedAnimals = Arrays.stream(animals).sorted(Comparator.comparing(Animal::getWeight)).toArray(Animal[]::new);
在任何一種情況下,要打印結果數組,只需對其進行迭代:
for (Animal a : animals)
System.out.print(a);
結果
Animal: Snake, Peso: 40.0Kg, De tipo: reptile
Animal: Dog, Peso: 200.0Kg, De tipo: mammal
Animal: Zebra, Peso: 350.0Kg, De tipo: mammal
Animal: Jiraffe, Peso: 450.0Kg, De tipo: mammal
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.