Calling arrays from other methods in a different class

Posted by Jake H on Stack Overflow See other posts from Stack Overflow or by Jake H
Published on 2010-05-06T13:19:20Z Indexed on 2010/05/06 13:38 UTC
Read the original article Hit count: 219

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays