[英]How to find minimum, maximum and average value from a Generic ArrayList
I have 3 classes Pet, Cat and Dog classes. 我有3类宠物,猫和狗课程。 Cat and Dog classes are childs of Pet class and I've overrided toString method. Cat和Dog类是Pet类的子类,我已经重写了toString方法。 The toString method that I overrided in Dog class prints the name field and weight field of Dog class. 我在Dog类中重写的toString方法将打印Dog类的名称字段和权重字段。 I've created an ArrayList called mainList and I need a method which calculates minimum, maximum and avarage value of weights in ArrayList. 我已经创建了一个名为mainList的ArrayList,并且需要一个方法来计算ArrayList中权重的最小,最大和平均值。 Can you tell me how can I create a method that calculates minimum, maximum and avarage values of weights of Dog Objects in ArrayList? 您能告诉我如何创建一种方法来计算ArrayList中Dog对象权重的最小,最大和平均值吗?
My classes are down below... 我的课程低于...
Pet Class: 宠物课:
public class Pet
{
private String name;
public String getName()
{
return name;
}
protected void setName(String newName)
{
name = newName;
}
public Pet(String petName) {
name = petName;
}
}
Dog Class: 狗类:
public class Dog extends Pet
{
private Double weight;
public double getWeight()
{
return weight;
}
protected void setWeight(double newWeight)
{
weight = newWeight;
}
public String toString() {
return String.format("%s , %f", getName(), getWeight());
}
public Dog(String petName, double dogWeight)
{
super(petName);
weight = dogWeight;
}
}
PetTest Class: PetTest类别:
import java.util.*;
public class PetTest
{
static ArrayList<Pet> mainList = new ArrayList<Pet>();
static Iterator<Pet> mainIter = mainList.iterator();
static Scanner keyboard = new Scanner(System.in);
public static void eighthCase()
{
// I need a method here
}
public static void listThemAll()
{
for(Pet pet: mainList)
{
System.out.println(pet.toString());
}
}
public static void listDogs()
{
for(Pet pet: mainList)
{
if (pet instanceof Dog)
{
System.out.println(pet.toString());
}
}//end of for loop
}//end of listDogs method
public static void listCats()
{
for(Pet pet: mainList)
{
if(pet instanceof Cat)
{
System.out.println(pet.toString());
}
}//end of for loop
}//end of listCats method
public static void addDog(String dogName,Double dogWeight)
{
Pet dog = new Dog(dogName, dogWeight);
mainList.add(dog);
}//end of addDog method
public static void addCat(String catName, String furColor)
{
Pet cat = new Cat(catName, furColor);
mainList.add(cat);
}//end of addCat method
public static void removeDog(String dogName)
{
for(Pet pet : mainList)
{
if((pet instanceof Dog) && (pet.getName().equals(dogName)))
{
mainList.remove(pet);
}//end of if statement
}//end of for loop
}//end of removeDog method
public static void removeCat(String catName)
{
for(Pet pet : mainList)
{
if((pet instanceof Cat) && (pet.getName().equals(catName)))
{
mainList.remove(pet);
}//end of if statement
}//end of for loop
}//end of removeCat method
public static void showMenu()
{
System.out.println("1. Add dog ");
System.out.println("2. Add cat");
System.out.println("3. Remove dog");
System.out.println("4. Remove cat");
System.out.println("5. List dogs");
System.out.println("6. List cats");
System.out.println("7. List all pets");
System.out.println("8. Show min, max and average weight of dogs");
System.out.println("0. Quit");
int action = keyboard.nextInt();
Scanner parameter1 = new Scanner(System.in);
Scanner parameter2 = new Scanner(System.in);
while(action > 0 && action <= 8)
{
switch(action)
{
case 1:
System.out.println("Type in the name of the dog that you want to add.");
String dogName = parameter1.next();
System.out.println("Type in the weight of the dog that you want to add.");
Double dogWeight = parameter2.nextDouble();
addDog(dogName,dogWeight);
showMenu();
break;
case 2:
System.out.println("Type in the name of the cat that you want to add.");
String catName = parameter1.next();
System.out.println("Type in the color of the cat that you want to add.");
String furColor = parameter2.next();
addCat(catName,furColor);
showMenu();
break;
case 3:
System.out.println("Type in the name of dog that you want to remove.");
String dogToRemove = parameter1.next();
removeDog(dogToRemove);
showMenu();
break;
case 4:
System.out.println("Type in the name of dog that you want to remove.");
String catToRemove = parameter1.next();
removeDog(catToRemove);
showMenu();
break;
case 5:
listDogs();
showMenu();
break;
case 6:
listCats();
showMenu();
break;
case 7:
listThemAll();
showMenu();
break;
case 8:
eighthCase();
break;
}//end of switch statement
}//end of while loop
}//end of showMenu method
public static void main(String[] args)
{
showMenu();
}//end of main method
}//end of the class
In Java 8 this can be done with Stream
s: 在Java 8中,这可以通过Stream
来完成:
mainList.stream()
.filter (animal -> animal instanceof Dog)
.mapToDouble(animal -> ((Dog)animal).getWeight())
.min()
//.max()
//.average()
.get()
And you may get the same result with Java 7: 使用Java 7可能会得到相同的结果:
List<Dog> dogs = new ArrayList<>();
for (Animal animal : mainList)
if (animal instanceof Dog)
dogs.add((Dog)animal);
double min = dogs.get(0).getWeight();
double max = dogs.get(0).getWeight();
double sum = 0;
for (Dog dog : dogs) {
min = Math.min(min, dog.getWeight());
max = Math.max(max, dog.getWeight());
sum += dog.getWeight();
}
System.out.format("min = %i, max = %i, average = %f%n", min, max, sum * 1.0 / dogs.size());
// or by implementing comparator
Comparator <Dog> dogComparator = new Comparator<>() {
public int compare (Dog dog1, Dog dog2) {
return Double.compare(dog1.getWeight(), dog2.getWeight());
}
};
double min = Collections.min(dogs, dogComparator).getWeight();
double max = Collections.max(dogs, dogComparator).getWeight();
I would do it like this (bottom one is shorter): 我会这样做(下一个较短):
public static void calculateDogValues(){
List<Double> dogWeightList = new ArrayList<>();
double max = 0, min = 0, average = 0;
for(Pet pet: mainList){
if (pet instanceof Dog){ //add all dog weights to list
dogWeightList.add(pet.getWeight());
}
}
Collections.sort(dogWeightList); //sort the list
min = dogWeightList.get(0); //get smallest value
max = dogWeightList.get(dogWeightList.size()-1); //get highest value
for (double weight : dogWeightList) { //make sum of all weights
average += weight;
}
average = average/dogWeightList.size(); //sum/number of weights
System.out.println("Minimum weight is " + min);
System.out.println("Maximum weight is " + max);
System.out.println("Average weight is " + average);
}
Or like this without sort: 或者像这样没有排序:
double max = 0, min = 0, average = 0, amountOfDogs = 0;
for(Pet dog: mainList){
if (dog instanceof Dog){
min = Math.min(min, dog.getWeight());
max = Math.max(max, dog.getWeight());
average += dog.getWeight();
amountOfDogs++;
}
}
average = average/amountOfDogs;
System.out.println("Minimum weight is " + min);
System.out.println("Maximum weight is " + max);
System.out.println("Average weight is " + average);
}
Also make your pet class this: 也让您的宠物班级:
public class Pet{
private String name;
private double weight;
public String getName()
{
return name;
}
protected void setName(String newName)
{
name = newName;
}
public Pet(String petName) {
name = petName;
}
public void setWeight(double newWeight){
weight = newWeight;
}
public double getWeight(){
return weight;
}
}
There was no weight in superclass. 超类没有权重。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.