Writing a method meant to store a list element to a variable in order to switch it with the next element in the array.
There are currently two variables for storage (which may or may not mean there's an extra).
The goal is to use the correct iterator (unless there's a better method) to switch the stored element with the next in the fewest lines possible.
public void sort(List<Point> lst) {
for (int st = 0; st < lst.size(); st++) { //defines first element-to-compare.
for (int wt = 1; wt< lst.size(); wt++) { //defines second element-to-compare.
double one = lst.get(st).distanceToOrigin(); //stores variable describing distance-to-origin for point one;
//if lst.get(st)>lst.get(wt), am switching element places in list.
//if lst.get(st) > lst.get(wt), switch the pair of consecutive elements.
double two = lst.get(wt).distanceToOrigin(); //stores variable describing distance-to-origin for point two;
//represents element to switch if lst.get(wt) < lst.get(st)
Point tmp1;
Point tmp2;
if (one > two){
tmp1 = lst.get(st);
lst.remove(lst.get(st));
tmp2 = lst.nextPoint();
}
}
}
}
Right now I'm using the hasNext()
method in order to check if there is another element after lst.get(st):
if (one > two) {
tmp1 = lst.get(st);
lst.remove(lst.get(st));
while (lst.distanceToOrigin.hasNext()) { //this line does not work in editor.
//Attempting to refine.
//TODO switch elements described by double one and double two.
}
}
Insight is greatly appreciated.