0

Ok, I was misunderstanding the problem. After reading it a couple of times I figured out randInt is actually the method itself i am using to populate the array. So when it says to call randInt its some sort of recursive call I think. This is somehow what it should look like:

  static int[] randInt(int i, int j) {
    int[] temp = new int[(j - i) + 1];
    for ( i = 0; i < j; i++) {
        temp[i] = i + 1; // here i populate the array
    }
    System.out.println(Arrays.toString(temp)); // this prints [1, 2, 3, 4, 5]
    for ( i = 1; i < j;i++){
        swapReferences(temp[i], temp[randInt(0,i)] ); //this is some sort of recursive call that swaps the references 
        // btw that statement does not compile, how can i pass a method as a parameter?



    }
    return temp;
}

static void swapReferences(int a, int b) { //these parameters are wrong, need to be changed
   //Method to swap references

}

Sorry for the confusion, but I think thats how it should be correctly.

user1404664
  • 73
  • 1
  • 2
  • 8
  • @SotiriosDelimanolis Not its a different problem. – user1404664 Jan 25 '14 at 00:38
  • 3
    You'd think so, but you'd be wrong. – Sotirios Delimanolis Jan 25 '14 at 00:39
  • Different _scenario_, same problem. – takendarkk Jan 25 '14 at 00:46
  • 1
    You are passing the int value in each array to the swap method _by value_, so any operations you perform on the ints `a` and `b` are effectively erased as soon as the methods ends. The top answer in Sotirios' link goes over this situation. The solution is to create a `swap` method that takes an array and two indices as parameters, then swap the values in the input array using the given indices. – mdl Jan 25 '14 at 00:49

2 Answers2

2

Java is pass-by-value, so reassigning the parameters as you try to do will not work.

What you need to do is to have the array itself and two integer indices as parameters:

 int randInt = generate.nextInt(j-i) + 1; //this is gonna generate a # within the range of the array (so if array is size 5, generates something 1-5)
 for ( i = 1; i < j;i++){
        swapReferences(temp, i, randInt); //and this is my attempt at swapping the references
        randInt = generate.nextInt(i) + 1 ;
  }

  static void swapReferences(int[] array, int a, int b){
     int x = array[a];
     array[a] = array[b];
     array[b] = x;

 }

You can mutate parameters such as arrays passed into a method, as is being done here, but you can't reassign the parameters themselves.

Don Roby
  • 40,677
  • 6
  • 91
  • 113
  • Ah you were faster and more succinct than me +1! – mdewitt Jan 25 '14 at 00:52
  • This works correctly as it is, but the book for some reason wants me to implement it another way. I have edited the original post so you can see exactly what is being asked. – user1404664 Jan 25 '14 at 01:02
  • 1
    This isn't necessarily the case for your book, but I remember when I was in college some of the code in the books I had for class just didn't work as written. I remember one book that was clearly translated to Java from C, but poorly. So many of the methods didn't work without a bit of manipulation. – mdewitt Jan 25 '14 at 01:07
  • 1
    If the book is suggesting a method with the signature as shown, it probably is indeed a bad translation from another language that works differently. Perhaps you should throw it out and get a book by someone who actually knows Java. – Don Roby Jan 25 '14 at 01:17
  • @DonRoby I think I may have misunderstood the problem initially. I have edited the first post. Maybe it makes more sense now. Sorry for the confusion. – user1404664 Jan 25 '14 at 01:26
  • Sorry, no. It makes a bit less sense now. You've got a method that returns an `int[]` which you're trying to call recursively in a place that needs an `int` and not an array. I've got no clue what you're trying to do here. – Don Roby Jan 25 '14 at 01:33
  • @DonRoby The swapReferences method parameters were defined by me, since I do not know how to pass the parameters the book is asking me to pass. The parameters can be changed to anything. – user1404664 Jan 25 '14 at 01:36
2

You are just changing the ints that a and b are pointing to, but not what the indicies the array is pointing to. You need to change your swapReferences method to take the array as input, and something like the indicies to swap

static void swapReferences(int[] arr, int indexA, int index B){
    int x = arr[indexA];
    a = arr[indexB];
    b = x;
    arr[indexA] = a;
    arr[indexB] = b;
}

or

static void swapReferences(int[] arr, int indexA, int indexB){
    int x = arr[indexA];
    arr[indexA] = arr[indexB];
    arr[indexB] = x;
}
mdewitt
  • 2,526
  • 19
  • 23