views:

36

answers:

2

Hello, I need help dealing with an array in my java program. in my first class, "test", I set 4 variables and then send them to my other class (test2).

arr[i] = new test2(id, fname, lname, case);

at that point, variables are set and then I want to return those variables. So in the test2 class, I have a method that strictly returns one of those variables

public int getId(){
return id;
}

I understand this is a little stupid, but professor gets what professor wants I guess. What I want to do now is in my main method in "test" I want to retrieve that variable and sort the array based on that int. Unfortunately, I have to create my own sort function, but I think this would work for what I want to do.

for(j = 0; j < arr.length; j++){
   int indexMin =j;

   for(i = j; i < arr.length;i++){
    if(arr[i] < arr[indexMin]){
     indexMin = i;
    }
   }
   int tmp = arr[j];
   arr[j] = arr[indexMin];
   arr[indexMin] = tmp;
  }

I appreciate any help anyone could provide. Thank you

A: 

Make Test2 implements Comparable<Test2> and use java.util.Arrays.sort(Object[]) instead.

The compareTo(Test2) would look something like this:

@Override int public compareTo(Test2 other) {
   return
     (this.id < other.id) ? -1 :
     (this.id > other.id) ? +1 :
     0;
}

Even better is if you can switch to List<Test2> (Effective Java 2nd Edition: Item 25: Prefer lists to arrays).

See also


If you have to implement your own sort, then in place of arr[i] < arr[indexMin], you want arr[i].getId() < arr[indexMin].getId(). This assumes that it was declared Test2[] arr;

If for some reason it was declared as Object[] arr instead, then you'd just need to cast them to Test2 before you can invoke getId().

((Test2) arr[i]).getId() < ((Test2) arr[indexMin]).getId()

You'd also want to declare Test2 tmp instead of int tmp for the swap.


As for printing, you can use a for-each loop:

for (Test2 t : arr) {
   System.out.println(t);
}

This assumes that Test2 has @Overridepublic String toString() that is sensible for printing, but otherwise you can access the member methods individually.

See also

polygenelubricants
The OP has to implement the sort himself.
Péter Török
@Peter: Oh, it's a course professor? I thought maybe it's a research professor.
polygenelubricants
+2  A: 

So a few comments:

-Your loop looks like this:

for(i = j; i < arr.length; i++)

You should be declaring

for(int i = j; i< arr.length; i++);

Either you hadn't declared i yet, which would give you a compilation error, or you declared i earlier which is not ideal...you avoid bugs better by declaring variables as locally as possible.

-In this line you directly compare objects:

if(arr[i] < arr[indexMin]){

but if I understand your intent correctly, you want to be comparing the IDs, so this should look like

if(arr[i].getId() < arr[indexMin].getId()){

-It looks like arr is an array of test2 objects, but you assign one to an int for your swapping code:

int tmp = arr[j];

This should be

test2 tmp = arr[j];

As far as your algorithm, why don't you get your code up and running and then try testing with a few results. What about 4 objects with IDs 1, 2, 3 and 4? How about 4, 3, 2, and 1? You'll learn more by playing around with it manually than if I tell you an algorithm here. Don't be afraid to add some statements that help you see exactly what's going on when. For example, maybe you might change the last 4 lines to look like:

System.out.println("About to swap id " + arr[j].getId() + " from index " + j + " with minimum " + arr[indexMin].getId() + " at index " + indexMin);
test2 tmp = arr[j];
arr[j] = arr[indexMin];
arr[indexMin] = tmp;

This will help you sooner get to the bottom of what's happening when in your program, and don't be afraid to add more similar stuff.

JF