81,115
社区成员
发帖
与我相关
我的任务
分享
public class TT {
public static void main(String[] args) {
List<Product> ls = new ArrayList<Product>();
ls.add(new Product(10,10));
ls.add(new Product(5,10));
ls.add(new Product(5,20));
ls.add(new Product(5,30));
ls.add(new Product(20,30));
int size = ls.size();
for(int i = 0;i < size - 1;i++){
for(int j = i + 1;j < size;j++){
if(ls.get(i).getPrice() > ls.get(j).getPrice()){
Product max = ls.get(i);
Product min = ls.get(j);
ls.remove(i);
ls.remove(j);
ls.add(i,min);
ls.add(j,max);
}
if(ls.get(i).getPrice() == ls.get(j).getPrice()){
ls.get(i).addNum(ls.get(j).getNum());
ls.remove(j);
j--;
size--;
}
}
}
for(int i = 0;i < ls.size();i++){
System.out.println("price: " + ls.get(i).getPrice() + ",num: " + ls.get(i).getNum());
}
}
}
class Product {
private double price;
private int num;
public Product(int num, double price){
this.price = price;
this.num = num;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public void addNum(int num){
this.num = this.num + num;
}
}
public List<Domain> sort(List list){
//这里面将你的思路转成代码
//最后将处理的结果返回
}