I have two ArrayLists of doubles created like this:
ArrayList <Double> axis_x = new ArrayList<Double>();
ArrayList <Double> axis_y = new ArrayList<Double>();
Now, I have seen a difference in the calculations when I use (because I wanted to simplify the calculations)
for(int j=0; j<axis_x.size();j ) {
double ax_x = Double.parseDouble(axis_x.get(j).toString()) Math.abs(left_scale);
axis_x.set(j,ax_x);//axis_y.get(i);
double ax_y=0.0f;
if(Double.parseDouble(axis_y.get(j).toString()) < 0.0f) {
ax_y = Math.abs(Double.parseDouble(axis_y.get(j).toString())) Math.abs(up_scale);
}else if(Double.parseDouble(axis_y.get(j).toString()) > 0.0f) {
ax_y = Math.abs(Double.parseDouble(axis_y.get(j).toString()) - Math.abs(up_scale));
}else if(Double.parseDouble(axis_y.get(j).toString()) == 0.0f) {
ax_y = Math.abs(up_scale);
}
axis_y.set(j,ax_y);
}
VS
for(int j=0; j<axis_x.size();j ) {
double ax_x = axis_x.get(j) Math.abs(left_scale);
axis_x.set(j,ax_x);
double ax_y=0.0f;
if(axis_y.get(j) < 0.0f) {
ax_y = Math.abs(axis_y.get(j)) Math.abs(up_scale);
}else if(axis_y.get(j) > 0.0f) {
ax_y = Math.abs(axis_y.get(j)) - Math.abs(up_scale);
}else if(axis_y.get(j) == 0.0f) {
ax_y = Math.abs(up_scale);
}
axis_y.set(j,ax_y);
}
Knowing that axis_y.get(i) should be a double, is Double.parseDouble(axis_y.get(j).toString()) not the same as axis_y.get(i)? Am I missing something here?
CodePudding user response:
Please, see the two mentioned ways of getting the Double item:
final ArrayList<Double> axis_x = new ArrayList<Double>();
// 1. Getting the Double item from the `List<Double>`
// without performing any conversions.
final int i = 0;
final Double x = axis_x.get(i);
// 2. Getting the Double item from the `List<Double>`
// with performing two redundant conversions.
final Double x =
// `String => Double` conversion.
Double.parseDouble(
// `Double => String` conversion.
axis_x.get(i).toString()
);
The way #1 should be used to get the item without performing any conversions.
The way #2 uses two redundant conversions: Double => String and String => Double.
Therefore, this way should be avoided.
CodePudding user response:
#1: final Double x = axis_x.get(i);
Double <= Double
#2: final Double x = parseDouble(axis_x.get(i).toString());
Double <= String <= Double conversion.
way 2 is not the smart and shorter way. also the way 1 is the exact value.
