我有两个Arraylist即 -
List<java_test> one=new ArrayList<java_test>();
List<java_test> two=new ArrayList<java_test>();
我有两个方法,它们分别返回定义的ArrayList值。 第一个arraylist,即一个与第二个arraylist相比有更多的值,即两个。
现在我想
比较两个数组列表的值并找出它们 差
并以表格格式显示。此外,因为fi 第一个Arraylsit 与相比具有更多值。当只有一个arraylsit的值 0 ,因为存在差异。
修改-1
我试图将计算差异作为 -
public LinkedHashMap < String, List < Double >> diff() {
for (int i = 0; i < one.size() && i < two.size(); i++) {
Comaprision refObj = one.get(i);
Comaprision stObj = two.get(i);
Double x = refObj.getBeam_current();
Double y = stObj.getBeam_current();
x = refObj.getBeam_energy();
y = stObj.getBeam_energy();
comparing(x, y);
x = refObj.getSt2_vs2_bag1_rb();
y = stObj.getSt2_vs2_bag1_rb();
comparing(x, y);
x = refObj.getSt2_vs2_bag2_rb();
y = stObj.getSt2_vs2_bag2_rb();
comparing(x, y);
x = refObj.getSt2_vs2_bag3_rb();
y = stObj.getSt2_vs2_bag3_rb();
comparing(x, y);
x = refObj.getSt2_vs2_bag4_rb();
y = stObj.getSt2_vs2_bag4_rb();
comparing(x, y);
dif.put("", z);
}
return dif;
}
public List < Double > comparing(Double x1, Double y1) {
if ((x1 > y1)) {
z.add(x1 - y1);
System.out.println("value of z is" + z);
} else {
z.add(y1 - x1);
}
return z;
}
&#13;
我收到的输出是 - 但它没有给出正确的区别。我所犯的错误是什么。?
答案 0 :(得分:1)
如果我正确理解了你的问题。如果数组列表具有相同索引的值,或者当只有一个数组列表具有相同索引的值时打印零,则要打印差异。
为此有两种可能性。
如果您的数组列表仅包含非零值。您可以按如下方式更改for循环的条件
for (int i = 0; i < one.size(); i++) // since you have already specified first array list is larger than the second.
然后按如下方式更改比较功能:
public List < Double > comparing(Double x1, Double y1) {
if(y1 == 0)
{
z.add(0);
}
else
if ((x1 > y1)) {
z.add(x1 - y1);
System.out.println("value of z is" + z);
} else {
z.add(y1 - x1);
}
return z;
}
如果你的arraylist包含零值。我建议做这样的事情
public LinkedHashMap < String, List < Double >> diff() {
for (int i = 0; i < one.size(); i++) {
if(i < two.size()){
Comaprision refObj = one.get(i);
Comaprision stObj = two.get(i);
Double x = refObj.getBeam_current();
Double y = stObj.getBeam_current();
x = refObj.getBeam_energy();
y = stObj.getBeam_energy();
comparing(x, y);
x = refObj.getSt2_vs2_bag1_rb();
y = stObj.getSt2_vs2_bag1_rb();
comparing(x, y);
x = refObj.getSt2_vs2_bag2_rb();
y = stObj.getSt2_vs2_bag2_rb();
comparing(x, y);
x = refObj.getSt2_vs2_bag3_rb();
y = stObj.getSt2_vs2_bag3_rb();
comparing(x, y);
x = refObj.getSt2_vs2_bag4_rb();
y = stObj.getSt2_vs2_bag4_rb();
comparing(x, y);
}
else{
for(i=0; i<(the number of variables your comparing in the if condition above); i++)
{
z.add(0.0);
}
}
}
dif.put("", z);
return dif;
}
public void comparing(Double x1, Double y1) {
if ((x1 > y1)) {
z.add(x1 - y1);
System.out.println("value of z is" + z);
} else {
z.add(y1 - x1);
}
}
答案 1 :(得分:0)
这可以解决这个问题。但我还没有尝试过使用自编类的对象。
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
/*Put values to both lists*/
list1.add("John");
list1.add("Doe");
list2.add("John");
list2.add("Doe");
/*Add other objects to list2. This will be the differences*/
list2.add("Jane");
list2.add("Roe");
System.out.println("Printing two lists before retaining differences");
System.out.println("list1: "+list1);
System.out.println("list2: "+list2+"\n");
System.out.println("Similarities removed. Differences stored to list2");
list2.removeAll(list1);
System.out.println("differences: "+list2);
}
}
答案 2 :(得分:0)
我不知道你的算法在进行比较时是否正确。例如,如果x1 = -1且x2 = 5:x2 - x1 = 5 - (-1)
= 5 + 1
= 6
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Double> list1 = new ArrayList<Double>();
List<Double> list2 = new ArrayList<Double>();
List<Double> result = new ArrayList<Double>(); //where result will be stored
//Load with values
list1.addAll(Arrays.asList(new Double[]{3.0,-5.0,7.0,-9.0,11.0,-13.0}));
list2.addAll(Arrays.asList(new Double[]{1.0,1.0,1.0,1.0,1.0,1.0}));
System.out.println("list1: "+list1);
System.out.println("list2: "+list2);
for(int i=0; i<list1.size() && i<list2.size(); i++) {
double d1 = list1.get(i);
double d2 = list2.get(i);
//If d1 is greater than d2, do d1 minus d2 otherwise do d2 minus d1
//I don't know if that is really the purpose of doing the difference in your code
//Store result to result list
result.add( d1>d2 ? d1-d2 : d2-d1);
}
//Print the result
System.out.println("result: "+result);
}