2

In my previous question, I came to know how can I find Index value of Max Value in Array.

Now my problem is I'm trying to make 2 different Arrays by swappingthe array[0] with Max Value and another swapping array[0] with Min Value.

Here I'm able to do it, but the problem is, first I'm swapping array[0] with Min Value. And when I'm trying to swapping array[0] with Max Value, The array that is getting called in is the array output after swapping array[0] with Min value, but not the actual array in input.

import java.util.Arrays;

public class Cook {
    public static void main(String[] args) {
        int num[] = { 3, 1, 5, 2, 4 };
        int start = 0;
        int maxEnd = getMaxValue(num);
        int minEnd = getMinValue(num);
        System.out.println(Arrays.toString(num));
        getMinvalueString(start, maxEnd, minEnd, num);
        getMaxvalueString(start, maxEnd, minEnd, num);
    }

    private static void getMinvalueString(int start, int maxEnd, int minEnd, int[] num) {
        int[] num1 = num;
        int temp = num1[start];
        num1[start] = num1[minEnd];
        num1[minEnd] = temp;
        System.out.println("After replace Min: " + Arrays.toString(num1));

    }

    private static void getMaxvalueString(int start, int maxEnd, int minEnd, int[] num) {
        int[] num1 = num;
        int temp = num1[start];
        num1[start] = num1[maxEnd];
        num1[maxEnd] = temp;
        System.out.println("After replace: " + Arrays.toString(num1));

    }

    public static int getMaxValue(int[] num) {
        int maxValue = num[0];
        int getMaxIndex = 0;
        for (int i = 1; i < num.length; i++) {
            if (num[i] > maxValue) {
                maxValue = num[i];
                getMaxIndex = i;
            }
        }
        System.out.println(getMaxIndex);
        return getMaxIndex;

    }

    public static int getMinValue(int[] array) {
        int minValue = array[0];
        int getMinIndex = 0;
        for (int i = 1; i < array.length; i++) {
            if (array[i] < minValue) {
                minValue = array[i];
                getMinIndex = i;
            }
        }
        System.out.println(getMinIndex);
        return getMinIndex;
    }

}

Here Though I've made a new array num1, the same old array(num), is getting swapped. Please let me know how can I fix this.

Current Output

[3, 1, 5, 2, 4]
After replace Min: [1, 3, 5, 2, 4]
After replace: [5, 3, 1, 2, 4]

Expected Output

[3, 1, 5, 2, 4]
After replace Min: [1, 3, 5, 2, 4]
After replace: [5, 1, 3, 2, 4]

Thanks

user3872094
  • 3,269
  • 8
  • 33
  • 71

1 Answers1

4

num1 refers to the same array an num in both getMinvalueString and getMaxvalueString methods, so making changes in num1 affects the same array referred by num, which is the same array you declared an initialized in your main method.

If you wish to keep the original array unchanged, pass a copy of the array to each method :

    getMinvalueString(start, maxEnd, minEnd, Arrays.copyOf(num,num.length));
    getMaxvalueString(start, maxEnd, minEnd, Arrays.copyOf(num,num.length));
Eran
  • 387,369
  • 54
  • 702
  • 768
  • The methods affecting the array are `getMinvalueString` and `getMaxvalueString` though. – Manu Dec 08 '15 at 11:09