java - What is wrong with my if condition? -


i've been trying figure out 3 hours. believe syntax correct method isn't working. class takes arraylist of type point2d (class defined) holds x , y elements of point. attempting order arraylist ascending point sizes (ex. (200, 200), (200, 400), (400, 200), (400, 300), (400, 400)...etc, sort x points, y points). if condition in loop of indexofsmallest smallest, if correct should working, sort x values correctly. ideas? thank in advance!!

import java.util.arraylist;  public class point2dselectionsort {     public point2dselectionsort() {     }      public static void sort(arraylist<point2d> a) {         int index, index2,indexofnextsmallest;              (index = 0; index < a.size( ) - 1; index++){               indexofnextsmallest =                              indexofsmallest(index, a);               interchange(index,indexofnextsmallest, a);             }              //a.get(0), a.get(1),...,a.get(index) sorted.      }      /**      * precondition : , j legal indices arraylist a.      * postcondition: a.get(i) , a.get(j) have been interchanged.      */      private static void interchange(                                 int i, int j, arraylist<point2d> a) {         point2d temp;         temp = a.get(i);         a.set(i, a.get(j));         a.set(j, temp);      }      /**      * @return  index of lexicographically first value among      * a.get(startindex), a.get(startindex+1),...,a.get(a.size( ) - 1)      */     private static int indexofsmallest(                                  int startindex, arraylist<point2d> a) {         point2d min = a.get(startindex);         int indexofmin = startindex;         //point2d otherpair = (point2d) min;         (int index = startindex + 1; index < a.size( ); index++){              if((a.get(index).getfirst() < min.getfirst()) || (( a.get(index).getfirst() == min.getfirst() ) && ( a.get(index).getsecond() < min.getsecond() ))){                    min = a.get(index);                    indexofmin = index;              }          }         return indexofmin;    } } 

your entire class unnecessary, because can replaced call collection.sort()

list<point2d> list; // assuming  collections.sort(list, new comparator<point2d>() {     public int compare(point2d a, point2d b) {         return a.x == b.x ? a.y - b.y : a.x - b.x;     } }); 

voila!


if assignment, , must use class, incorporate code method (discarding sort method):

public static void sort(arraylist<point2d> a) {     collections.sort(list, new comparator<point2d>() {         public int compare(point2d a, point2d b) {             return a.x == b.x ? a.y - b.y : a.x - b.x;         }     }); } 

job done.

to answer question of "what's wrong if statement", answer "it exists".


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -