简体   繁体   中英

How do I compare two object array elements?

I'm having a bit of trouble with this code I'm writing. In it, I'm comparing two object elements. I'm trying to use > but I'm finding that it's not working. Is there another way for me to compare them like this without using >? (Also, I'm a beginner so I apologize in advance if the code isn't well done or formatted completely correctly.

private static int indexOfMinInRange(Reservation[] array, int low, int high) {





int index;
    Reservation number;
    int holdIndex;

number = array[0];
holdIndex =1;

for(index = 0; index < array.length; index++) {
    System.out.println(index);
    if(number > array[index]) {

        number = array[index];
        holdIndex = index;

    }//end if


}//end for loop



return holdIndex;

}//end indexOfMaxInRange
public static Reservation[] sortArray(Reservation[] arrayGiven) {

        int i;
        int index;
        boolean haveSwapped;



        haveSwapped = true;
        i = 0;

        while(haveSwapped == true) {

            haveSwapped = false;

            for(i = 0; i + 1 < arrayGiven.length; i++) {

                if (arrayGiven[i] > arrayGiven[i + 1]) {

                    swapElement(arrayGiven, i, i + 1);
                    haveSwapped = true;

                }//end if 




            }//end for loop (swapping)



        }//end while loop

        for(index = 0; index < arrayGiven.length; index++) {

                System.out.println(arrayGiven[index]);

        }//end for loop (printing)


        return arrayGiven;

    }//end sortArray

If you are not sure the type that each object is,you can use Object#compareTo() method to compare them.

Each type of the element should implement this method properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM