[Solved] Java single dimention arrays equating two variables of different size [closed]


Array variables are also references just like a class-typed variable. So x and y contain only references to an array somewhere in memory. Let’s look at that assignment:

But what does happen when we use “x=y” ?

Yes, just like with objects in java, the address to the array y is stored in x. The result is that x and y reference the same array and the array previously referenced by x is lost (and eventually garbage collected). If you instead want a copy of y to be assigned to x try:

x = Arrays.copyOf(y, y.length)

A tip: Try using the following code if you want to print the contents of an array:

System.out.println(Arrays.toString(x));

For more info about the Arrays class try: http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html

6

solved Java single dimention arrays equating two variables of different size [closed]