对于我的任务,我被要求执行以下操作:
如果我正确的话,我已经创建了if语句,但是我不确定如何引用数组列表中的每个Animal,所以我使用了伪代码和在ArrayList中将第一个数组称为" AL1"第二个是" AL2"等等。
以下是代码:
演示课程
import java.util.ArrayList;
public class Demo {
public static ArrayList<Animal> animalGroup; // make this an instance static variable to access in other class with main method;
public static void main(String[] args)
{
animalGroup = new ArrayList<>();
//Add new Animals with properties such as name and age?
animalGroup.add(new Wolf("Sam", 5));
animalGroup.add(new Parrot("George", 3));
animalGroup.add(new Wolf("Wesley", 7));
animalGroup.add(new Parrot("Pat", 10));
System.out.println("Compare Aimal ages" + AL1.getAge().compareTo(AL2.getAge, AL3.getAge(), AL4.getAge()));
int result = AL1AGE.compareTo(AL2AGE, AL3AGE, AL4AGE);
if(result == 3)
System.out.println(AL1.getName() + "comes before " + AL2.getName() + ", " + AL3.getName() + " and " + A|L4.getName());
if(result == 5)
System.out.println(AL2.getName() + "comes before " + AL3.getName() + ", " + AL4.getName() + " and after " + A|L1.getName());
if(result == 7)
System.out.println(AL3.getName() + "comes before " + AL4.getName() + " and after " + AL1.getName() + " and " + A|L2.getName());
else
System.out.println(AL4.getName() + "comes after " + AL1.getName() + ", " + AL2.getName() + " and " + A|L3.getName());
}
}
主要方法调用
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args)
{
System.out.println("************ArrayList after sorting************");
Collections.sort(animalGroup);
Collections.reverse(animalGroup);
for(Animal animal2 : animalGroup) {
System.out.println(animal2.getName()+","+animal2.getAge());
}
}
}
所需的输出
Sam comes before George, Wesley and Pat
George comes before Wesley, Pat and after Sam
Wesley comes before Pat and after Sam and George
Pat comes after Sam, George and Wesley
非常感谢有关如何实现预期结果的任何帮助,谢谢。
答案 0 :(得分:1)
如果您使用的是Java-8,那么您可以使用比较符对lambda进行排序:
animalGroup.sort((animalGroup r1,animalGroup r2) - &gt; r1.getAge() - r2.getAge());
答案 1 :(得分:1)
创建一个实现Comparator
的类。像这样:
import java.util.Comparator;
public class AgeComparator implements Comparator<Animal>{
@Override
public int compare(Animal animal1, Animal animal2) {
if(animal1.getAge() < animal2.getAge()){
return 1;
} else if (animal1.getAge() > animal2.getAge()) {
return -1;
} else {
return 0;
}
}
}
并使用您创建的AgeComparator
课程对Collections.sort()
进行排序。
public static void main(String[] args) {
System.out.println("************ArrayList after sorting************");
Collections.sort(animalGroup,new AgeComparator()); // Sort highest to lowest
for (Animal animal2 : animalGroup) {
System.out.println(animal2.getName() + "," + animal2.getAge());
}
}
答案 2 :(得分:0)
在单独的类中实现比较器并实现其方法并按年龄对它们进行排序,并在sort方法中使用两个参数而不是一个参数。 comparator tutorial
答案 3 :(得分:0)
如下所示..
创建动物POJO类
public class Animal implements Comparable<Animal>{
private String animalName;
private int animalAge;
public Animal(String animalName,int animalAge){
this.animalName = animalName;
this.animalAge = animalAge;
}
public String getAnimalName() {
return animalName;
}
public void setAnimalName(String animalName) {
this.animalName = animalName;
}
public int getAnimalAge() {
return animalAge;
}
public void setAnimalAge(int animalAge) {
this.animalAge = animalAge;
}
@Override
public int compareTo(Animal o) {
return this.getAnimalAge() - o.getAnimalAge();
}
@Override
public String toString() {
return "Animal name: "+getAnimalName()+" , Age: "+getAnimalAge();
}
}
主要课程如下......
public class Sample {
public static void main(String[] args) {
List<Animal> animalList = new ArrayList<Animal>();
animalList.add(new Animal("abc",3));
animalList.add(new Animal("bac",6));
animalList.add(new Animal("cve",19));
animalList.add(new Animal("yyy",13));
animalList.add(new Animal("zzz",9));
animalList.add(new Animal("ttt",33));
// Before sorting
for (Animal animal : animalList) {
System.out.println(animal.toString());
}
System.out.println("-------------------------");
// Sorting the animals
Collections.sort(animalList);
// After sorting
for (Animal animal : animalList) {
System.out.println(animal.toString());
}
}
}