我的问题:1)我的set()方法返回一个未定义的类型错误2)我的比较工具从升序返回值,但我需要降序(从大到小)
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.*;
public class Main {
public static void main(String[] args) {
Equipment equ1 = new Equipment("Name", 1.00, 2.00, 3.00);
Equipment equ2 = new Equipment("Name", 1.00, 2.00, 3.00);
Equipment equ3 = new Equipment("Name", 1.00, 2.00, 3.00);
//Equipment Set 1 from User Input
System.out.println("Enter Equipment Set 1 Name: ");
Scanner input = new Scanner (System.in);
String equName1 = input.nextLine();
equ1.set(0, equName1);
System.out.println("Enter Equipment Set 1 Gain: ");
Double equGain1 = input.nextDouble();
equ1.set(1, equGain1);
System.out.println("Enter Equipment Set 1 Cost: ");
Double equCost1 = input.nextDouble();
equ1.set(2, equCost1);
double roi1 = (equGain1 - equCost1) / equCost1;
equ1.set(3, roi1); //Place ROI at index 3
//Equipment Set 2 from User Input
System.out.println("Enter Equipment Set 2 Name: ");
String equName2 = input.nextLine();
equ2.set(0, equName2);
System.out.println("Enter Equipment Set 2 Gain: ");
Double equGain2 = input.nextDouble();
equ2.set(1, equGain2);
System.out.println("Enter Equipment Set 2 Cost: ");
Double equCost2 = input.nextDouble();
equ2.set(2, equCost2);
double roi2 = (equGain1 - equCost1) / equCost1;
equ2.set(3, roi2);
//Equipment Set 3 from User Input
System.out.println("Enter Equipment Set 3 Name: ");
String equName3 = input.nextLine();
equ3.set(0, equName3);
System.out.println("Enter Equipment Set 3 Gain: ");
Double equGain3 = input.nextDouble();
equ3.set(1, equGain3);
System.out.println("Enter Equipment Set 3 Cost: ");
Double equCost3 = input.nextDouble();
equ3.set(2, equCost3);
double roi3 = (equGain1 - equCost1) / equCost1;
equ3.set(3, roi3);
List<Equipment> equipment = new ArrayList<Equipment>();
equipment.add(equ1); //Add each list to the array list
equipment.add(equ2);
equipment.add(equ3);
Collections.sort(equipment, new Sort());
System.out.println(equipment);
}
}
Equipment.java
public class Equipment { //This class seeks to define the elements and the structure of Equipment
String equName;
double equGain;
double equLoss;
double roi;
public Equipment(String equName, double equGain, double equLoss, double roi) {
this.equName = equName;
this.equGain = equGain;
this.equLoss = equLoss;
this.roi = roi;
}
public String toString() {
return "Equipment: " + equName + " Gain: " + equGain + "Loss: " + equLoss + "ROI: " + roi;
}
}
sort.java
import java.util.Comparator;
public class Sort implements Comparator<Equipment> { //Implementing a comparison tool for lists
@Override
public int compare(Equipment o1, Equipment o2) { //I want to compare one list to another - Update from Java 8
return Double.compare(o1.getroi() - o2.getroi()); //I want to compare the ROI value of each list
}
}
您这里的主要问题是缺乏面向对象编程的知识。我建议了解这个范例是如何工作的。您的代码中有多个问题,所以让我们一步一步来解决:
对于第一个问题,您似乎没有为模型定义setter和getter方法。定义它们并使类的属性私有。
public class Equipment {
private String equName;
private double equGain;
private double equLoss;
private double roi;
public Equipment() {
}
public Equipment(String equName, double equGain, double equLoss, double roi) {
this.equName = equName;
this.equGain = equGain;
this.equLoss = equLoss;
this.roi = roi;
}
public String getEquName() {
return equName;
}
public void setEquName(String equName) {
this.equName = equName;
}
public double getEquGain() {
return equGain;
}
public void setEquGain(double equGain) {
this.equGain = equGain;
}
public double getEquLoss() {
return equLoss;
}
public void setEquLoss(double equLoss) {
this.equLoss = equLoss;
}
public double getRoi() {
return roi;
}
public void setRoi(double roi) {
this.roi = roi;
}
public String toString() {
return "Equipment: " + equName + " Gain: " + equGain + "Loss: " + equLoss + "ROI: " + roi;
}
}
您的比较器方法使用double.compare(double obj1,double obj2)进行比较。你的方法用错了。你不必把两个双打互相减去。您将两个double值作为方法的参数。您应该交换方法参数以获得升序或降序:例如:
Double.compare(obj1.getRoi(),obj2.getRoit()) --> ascending order
Double.compare(obj2.getRoi(),obj1.getRoit()) --> descending order
sort.class
import java.util.Comparator;
public class Sort implements Comparator<Equipment> { //Implementing a comparison tool for lists
@Override
public int compare(Equipment o1, Equipment o2) { //I want to compare one list to another - Update from Java 8
return Double.compare(o2.getRoi(), o1.getRoi());
}
}
在你的主要方面,你混淆了变量。对于您计算的每一个用户输入,第一个对象是ROI,而不是其他对象。除此之外,每个设备的属性都应该用setter方法代替set方法。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ArrayTest {
public static void main(String[] args) {
Equipment equ1 = new Equipment();
Equipment equ2 = new Equipment();
Equipment equ3 = new Equipment();
//Equipment Set 1 from User Input
System.out.println("Enter Equipment Set 1 Name: ");
Scanner input = new Scanner(System.in);
String equName1 = input.nextLine();
equ1.setEquName(equName1);
System.out.println("Enter Equipment Set 1 Gain: ");
Double equGain1 = input.nextDouble();
equ1.setEquGain(equGain1);
System.out.println("Enter Equipment Set 1 Cost: ");
Double equCost1 = input.nextDouble();
equ1.setEquLoss(equCost1);
double roi1 = (equGain1 - equCost1) / equCost1;
equ1.setRoi(roi1); //Place ROI at index 3
//Equipment Set 2 from User Input
System.out.println("Enter Equipment Set 2 Name: ");
String equName2 = input.nextLine();
equ2.setEquName(equName2);
System.out.println("Enter Equipment Set 2 Gain: ");
Double equGain2 = input.nextDouble();
equ2.setEquGain(equGain2);
System.out.println("Enter Equipment Set 2 Cost: ");
Double equCost2 = input.nextDouble();
equ2.setEquLoss(equCost2);
double roi2 = (equGain2 - equCost2) / equCost2;
equ2.setRoi(roi2);
//Equipment Set 3 from User Input
System.out.println("Enter Equipment Set 3 Name: ");
String equName3 = input.nextLine();
equ3.setEquName(equName3);
System.out.println("Enter Equipment Set 3 Gain: ");
Double equGain3 = input.nextDouble();
equ3.setEquGain(equGain3);
System.out.println("Enter Equipment Set 3 Cost: ");
Double equCost3 = input.nextDouble();
equ3.setEquLoss(equCost3);
double roi3 = (equGain3 - equCost3) / equCost3;
equ3.setRoi(roi3);
List<Equipment> equipment = new ArrayList<>();
equipment.add(equ1); //Add each list to the array list
equipment.add(equ2);
equipment.add(equ3);
equipment.sort(new Sort());
System.out.println(equipment);
}
}
如何在Spring MVC中将输入元素绑定到数组列表元素? 视图模型: 模型属性: 控制器 然后,在jsp中,我有4个隐藏的输入,它们表示evaluatedId、evaluatorId、evoluatorName和evaluatedName的字段- 更新: 通过此更新,我收到以下错误:
我有一个arraylist类似于 经过一些处理后,我的res arraylist将包含 [1,1,2] [1,2] [1] [2] 这些元素,但我希望排序的顺序类似于 [1] [11] [12] [2] [2] 所以我所做的是 但它不起作用
我有下面的代码- 是包含int属性的类对象列表-参考号:即
我有这个: 有没有办法可以得到更新后的列表,而不是就地更新原始列表?
我想通过列表中的第一个元素进行过滤,然后通过第二个元素进行分组。 在使用Java Stream获得平均值之前,我过滤了第一个元素,即index。这样做合适吗?
这工作正常: 我得到了一个非常好的JSON数组和对象。 现在的问题是,我喜欢将我的数据作为有效载荷放入响应中,以便能够设置标头和状态,或者返回一个错误对象作为有效载荷。但是不管用。 Mai 16,2012 7:00:35 PMorg.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor写响应错误消息警告:未找到响应类ArrayList的消息正文编写器。