what? Can you please explain?
Main Topics
Browse All TopicsI have to make a selection sort in Java using an object array. This is the code I have so far:
public static void selectionSort(Object[] array3) {
//TODO: implement a linear search
for (int i = 0; i < array3.length - 1; i++){
for (int j = i+1; j < array3.length; j++){
if (array3[i] > array3[j]){
int temp = array3[i];
array3[i] = array3[j];
array3[j] = temp;
}
}
}
}
I'm using NetBeans IDE 6.7.1 and there's a red error line underneath the following line:
if (array3[i] > array3[j]){
And the error is "operator > cannot be applied to java.lang.Object, java.lang.Object
Can anyone help me write this? I'm a newbie with Java writing this for a class assignment.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
public static void selectionSort(Object[] array3) {
// TODO: implement a linear search
for (int i = 0; i < array3.length - 1; i++) {
for (int j = i + 1; j < array3.length; j++) {
if ((Integer)array3[i] > (Integer)array3[j]) {
int temp = (Integer)array3[i];
array3[i] = array3[j];
array3[j] = temp;
}
}
}
}
Ok, the error went away, but the test class I wrote is giving me an error:
public void testSelectionSort() {
System.out.println("select
Object[] array3 = new Object[10];
array3[0] = "charlie";
array3[1] = "gila";
array3[2] = "alpha";
array3[3] = "zulu";
array3[4] = "yankee";
array3[5] = "whiskey";
array3[6] = "november";
array3[7] = "romeo";
array3[8] = "bravo";
array3[9] = "hotel";
MyArrays.selectionSort(arr
System.out.println("Sorted
for (int k = 0; k < array3.length; k++)
System.out.println("Array "+ (k + 1) + ": " + array3[k]);
// TODO review the generated test code and remove the default call to fail.
}
Any ideas?
Read this, look for "Casting Objects" section.
http://java.sun.com/docs/b
That didn't help. I tried using casting in the test method (below) basically adding MyArrays.selectionSort(Int
public void testSelectionSort() {
System.out.println("select
Object[] array3 = new Object[10];
array3[0] = "charlie";
array3[1] = "gila";
array3[2] = "alpha";
array3[3] = "zulu";
array3[4] = "yankee";
array3[5] = "whiskey";
array3[6] = "november";
array3[7] = "romeo";
array3[8] = "bravo";
array3[9] = "hotel";
MyArrays.selectionSort((In
System.out.println("Sorted
for (int k = 0; k < array3.length; k++)
System.out.println("Array "+ (k + 1) + ": " + array3[k]);
// TODO review the generated test code and remove the default call to fail.
}
Business Accounts
Answer for Membership
by: afibarraPosted on 2009-11-05 at 12:23:17ID: 25753542
You need to "cast" your array elements to an Integer type.