我从数据库成分中获取了一个列表,然后创建了一个新列表 somelist = ingredinets。我用 convert(somelist) 转换 somelist 的一些值。这正在改变这两个列表。有没有办法保留原始清单成分?可能,我做错了什么。我应该从数据库中获取第二个列表吗?
List <Ingredient> somelist = new ArrayList<>();
somelist = ingredients;
convert.showImperial(somelist);
public List<Ingredient> showImperial(List<Ingredient> ingredients) {
for (int i = 0; i < ingredients.size(); i++) {
switch (ingredients.get(i).getMeasurement()) {
case GRAMS: {
Double value = ingredients.get(i).getValue();
if (value >= 453.59237) {
value = value / 453.59237;
BigDecimal rounded = new BigDecimal(value);
rounded=rounded.setScale(2, RoundingMode.HALF_DOWN);
ingredients.get(i).setValue((rounded.doubleValue())); //to pound
ingredients.get(i).setMeasurement(POUNDS);
} else {
value = value * 16 / 453.59237; //to oz;
System.out.println(value);
BigDecimal rounded = new BigDecimal(value);
rounded = rounded.setScale(1, RoundingMode.HALF_DOWN);
// System.out.println(rounded);
// System.out.println(rounded.doubleValue());
ingredients.get(i).setValue(rounded.doubleValue());
ingredients.get(i).setMeasurement(OUNCE);
}
break;
}
case ML: {
double value = ingredients.get(i).getValue();
ingredients.get(i).setValue(value / 240);
ingredients.get(i).setMeasurement(CUP); //to cup
break;
}
default: {
break;
}
}
}
return ingredients;
}