0

Consider the following:

  • array1 contains a list.
  • int[] unSorted = array1; a new initialized array works as a (duplicate).
  • Arrays.sort(array1); suppose to sort only the original Array array1.

The problem occurs when I use the sort method Arrays.sort. It sorts all Arrays that are linked to array1, including the (duplicate)!! Why is that? It should change only the array that is declared inside the parentheses! Isn't it?

bad
  • 92
  • 10

6 Answers6

4

Your unSorted is not "a new initialized array". It is just a different name for the same array.

Arrays are Objects, not primitives. They are passed around as pointers, not as copies.

If you want a copy, use java.util.Arrays#copyOf.

Thilo
  • 257,207
  • 101
  • 511
  • 656
1

Consider the following example: array & copy both refer to the same object - which is not the case with store.

import java.util.Arrays;

    class Test {


        public static void main(String[] args) {

            // array & copy both refer to the same Object
            double[] array = {5,4,3,2,1};
            double[] copy = array;
            // the following array is filled up "by hand" - thus it has no reference to the previous object
            double[] store = new double[array.length];



            for (int i = 0; i < array.length ; i++) {
                store[i] = array[i];
            }


            Arrays.sort(array);


            System.out.println("array"); // 1,2,3,4,5
            for (double d: array)
                System.out.println(d);
            System.out.println("copy"); // 1,2,3,4,5
            for (double d: copy)
                System.out.println(d);
            System.out.println("store"); // 5,4,3,2,1
            for (double d: store)
                System.out.println(d);


            // Primitives show a different behaviour
            double a = 5;
            double b = a;
            System.out.println(a +  " / " + b); // 5 / 5
            a = 10;
            System.out.println(a +  " / " + b); // 10 / 5
        }

    }
Hips
  • 244
  • 1
  • 9
  • So what is the right method to copy an array? – bad Sep 16 '15 at 23:48
  • Either use the method outlined above or make use of the java.util.Arrays as someone mentioned if you were to create a backup of the original array. `double[] copy = Arrays.copyOf(originalArray, originalArray.length);` - changes made to _originalArray_ will not be applied to _copy_ – Hips Sep 16 '15 at 23:51
1

array1 isn't actually an array: it's a reference to an array. It's a bit like if you give someone a business card with your office address, that card isn't the actual office, but rather something that tells you how to reach the office.

When you do int[] unSorted = array1, you're making a copy of the reference, not the array. Again, it's like someone copying down the info on your business card.

And when you perform an operation on unSorted (like moving its elements to sort then), what you're actually doing is performing an operation on the object it points to. It's like saying, "go to the office specified on the business card called unSorted, and move the chair at desk 1 to desk 2."

If you then went to the office specified on the business card called array1, you would of course expect that the chair has moved there, since it's the same office.

If you want a copy, you have to create one. There are a few ways to do that, but the easiest is to use Arrays.copyOf.

yshavit
  • 42,327
  • 7
  • 87
  • 124
1

Look at it this way :

You have a controller for a television (the array of course) and manage to duplicate the controller : you still have one television with multiple controllers.

If you change the television channel with one controller, even if you didn't touch the others, the televisions state has changed.

The same logic has to be applied to your problem.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
1

Your unsorted variable is a reference.

Remember references point to an object.

So when you declared and instantiated array1, you may have done the following:

  int[] array1 = new int[/*Any positive integer*/]

When declaring your unsorted reference variable, you did not instantiate a new array, you simply created a reference variable which points to the same object as array1.

Consider the image below:

enter image description here

So array1 and unSorted point to the same object, so of course if you call a method on either one of them you will effect the same object.

RamanSB
  • 1,162
  • 9
  • 26
0

Because they are all the same array, not 'other arrays'.

You need to think about what you mean by 'all other arrays that are linked'.

user207421
  • 305,947
  • 44
  • 307
  • 483