繁体   English   中英

在Java中对arraylist进行排序并在arraylist中进行比较

[英]sort arraylist and compare in arraylist in java

我是Java的初学者。 我的代码有问题。 我想设计程序,但陷入了ArrayList。 如何添加新价格并显示所有价格小于或等于新食物的价格的食物? 然后按价格对列表f进行排序。 请帮助我,非常感谢。

1.接受一个名为p的新价格:列出所有价格<= p的食品
2.排序列表按价格升序排序后的输出列表

public class Food {

    //states
    private String name;
    private double price;

    //accessor
    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    //mutator
    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }


    //methods
    public Food() {
        name = "";
        price = 0;
    }

    public Food(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public double getSalePrice() {
        double tax = 0.07;//7%;
        if(name.toLowerCase().startsWith("k")) tax = 0.05;
        return price + price * tax;
    }

    public void print() {
        System.out.printf("%-20s%-10.1f ($)%-10.1f ($)\n",
                name,price,getSalePrice());
        //System.out.println(String.format("%-20s%-10.1f ($)%-10.1f ($)\n",
             //   name,price,getSalePrice()));
    }
    public int listp(ArrayList<Food> f, int priceP) { 
     int s,i,n;
     n = f.size();
     s = 0;
     for(i=0;i<n;i++) {
       if(f.get(i).price > priceP) s++;  
     }
     return(s);
    }    
}

public class Main {

    public static void main(String[] args) {
        //a list of food
         Scanner in = new Scanner(System.in);
        final int MAX = 10;
        Food [] f = new Food[MAX];
        f[0] = new Food("BBQ",3.35);
        f[1] = new Food("KFC",3.3);
        f[2] = new Food("Ga 36",4.5);
        int n = 3;
        while(true) {          
            System.out.print("Enter food name: ");
            String name = in.nextLine();
            System.out.print("Enter food price: ");
            double price = Double.parseDouble(in.nextLine());
            f[n] = new Food();
            //f[n].name = name;f[n].price = price;
            f[n].setName(name);
            f[n].setPrice(price);
            n++;
            System.out.print("Add more food (yes/no)? ");
            String s = in.nextLine();
            if(s.equalsIgnoreCase("no")) break;
        }
        //output list
        for (int i = 0; i < n; i++) {
            f[i].print();
        }
        System.out.print("Enter price of food p:");
        double price = Double.parseDouble(in.nextLine());
        System.out.println(listp(f,price));

    }
    public int listp(ArrayList<Food> f, int priceP) { 
        int s,i,n;
        n = f.size();
        s = 0;
        for(i=0;i<n;i++) {
        if(f.get(i).getPrice() > priceP) s++;  
        }
        return(s);
    }       
}

1。

public List<Food> findFoodWhichPriceLessThan(Collection<Food> listOfFood, double price){
    return listOfFood.stream().filter(it -> it.getPrice() <= price).collect(Collectors.toList());
}

2。

  public void sortFoodByPrice(List<Food> listOfFood){
    Collections.sort(listOfFood, Comparator.comparingDouble(Food::getPrice));
  }

要么

  public List<Food> sortFoodByPrice2(List<Food> listOfFood){
    return listOfFood.stream().sorted(Comparator.comparingDouble(Food::getPrice)).collect(Collectors.toList());
  }

这是一个可行的解决方案,其中将数组转换为列表,并且将listp方法更改为使用确实符合您要求的流:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        //a list of food
        Scanner in = new Scanner(System.in);
        List<Food> f = new ArrayList<>();
        f.add(new Food("BBQ", 3.35));
        f.add(new Food("KFC", 3.3));
        f.add(new Food("Ga 36", 4.5));
        int n = 3;
        while (true) {
            System.out.print("Enter food name: ");
            String name = in.nextLine();
            System.out.print("Enter food price: ");
            double price = Double.parseDouble(in.nextLine());
            Food food = new Food();
            //f[n].name = name;f[n].price = price;
            food.setName(name);
            food.setPrice(price);
            f.add(food);
            n++;
            System.out.print("Add more food (yes/no)? ");
            String s = in.nextLine();
            System.out.println(s);
            if (s.trim().equalsIgnoreCase("no")) break;
        }
        System.out.print("Enter price of food p:");
        double price = Double.parseDouble(in.nextLine());
        listp(f, price);

    }

    private static void listp(List<Food> f, double priceP) {
        f.stream()
                .filter(food -> food.getPrice() <= priceP)
                .sorted(Comparator.comparingDouble(Food::getPrice))
                .forEach(food -> System.out.printf("%s : %s%n", food.getName(), food.getPrice()));
    }
}

这是listp方法的一种变体,它允许您将列表限制为数字:

private static void listp(List<Food> f, double priceP, int limit) {
    f.stream()
            .filter(food -> food.getPrice() <= priceP)
            .sorted(Comparator.comparingDouble(Food::getPrice))
            .limit(limit)
            .forEach(food -> System.out.printf("%s : %s%n", food.getName(), food.getPrice()));
}

这样可以确保打印出的列表始终与limit参数中指定的时间一样长。

用法示例:

listp(f, price, 2);

即使总列表超过此数量,这也将最多打印两个项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM