1

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.

JaaS
  • 51
  • 6
  • 1
    has next is not an iterator – Willi Mentzel Jul 27 '16 at 12:55
  • Can you be more precise which element you want to exchange which other? – J Fabian Meier Jul 27 '16 at 12:55
  • The two elements in need of switch have been highlighted in the comments (first block); hasNext() has been clarified. – JaaS Jul 27 '16 at 13:05
  • if you want to switch position of two elements in array, maybe you should consider method `set(index,element)` it's replacing element at indexed position with passed element – user902383 Jul 27 '16 at 13:05
  • I tried using it, but am getting caught writing the correct index. Is the correct format tmp1 = lst.get(st); lst.set(lst.get(st), lst.get(wt))? – JaaS Jul 27 '16 at 13:09

1 Answers1

1

You can use the methods of List for changing the elements order:

if(one > two) {
    Point tmp1 = list.get(st);
    Point tmp2 = list.get(wt);
    lst.set(st, tmp2);
    lst.set(wt, tmp1);
}
//....

Another approach: If each Point-Object "knows" the origin, it could also be an option to use the Comparable-Interface:

public class Point implements Comparable {
     Point origin;
     //other variables...
     //constructor and methods...
     @Override
     public int compareTo(Point other) {
         Double.compare(this.distanceToOrigin(), other.distanceToOrigin());
     }
}

And your sort()-method:

public void sort(List<Point> lst) {
    Collections.sort(lst);
}
Supahupe
  • 515
  • 2
  • 11
  • This is very helpful for comparing two points; do the new Point class and Collections.sort methods sort the entire list? – JaaS Jul 27 '16 at 13:35
  • Yes, it should sort the list depending on the value of what you implement in the compareTo()-Method. Internally, Collections.sort() will sort the elements by comparing them. This comparison is done by the overrided compareTo-Method of the Comparables stored in the Collection to sort. Here is another example where Double-Objects are sorted (Wrapper-class for double): http://stackoverflow.com/questions/16252269/how-to-sort-a-list-arraylist-in-java . Classes like Integer or Double already implement Comparable: http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html – Supahupe Jul 27 '16 at 14:22
  • Please upvote the answer if it solved your problem :) – Supahupe Jul 27 '16 at 15:38