1

I understand that when we say something equals another array we are in essence pointing to the array with our new name. If we wanted to copy an array we either need to go element by element or we need to use a method/java package.

public class puzzle { 
    public static void main(String[] args) { 
        int [] x= {1,2,4,6}; 
        double [] u= 3.0, 4.0, 5.0, 6.0,7.0}; 
        double [] v = {2.0, 4.0, 5.0}; 

        puzzle(u,v,x); //1 

        puzzle (v,u,x); //2 
    }
    public static void puzzle(double [] first, double [] second, int [] third){
        double [] temp; 
        temp=first; 
        temp[2]=42.0; 
        **second= first;** 
        second[0]= 2.34; 
    }
}

we want to see what the values of x, u, v are after this has been run for (u,v,x) and for (v,u,x)

in the second puzzle v is only length 3 containing 2.34, 4.0, 42.0. Why is it only three long instead of six (2.34,4.0,42.0,6.0,7.0)

does this have to do with array v being only three long and therefore is a fixed size of three and ends up cutting off the other numbers?
( i did not choose the names)

  • Please update your question with correct syntax – Courage Apr 25 '17 at 04:08
  • And please: when English is not your first language : use simple short sentences. Subject verb object. Next sentence. I have a very hard time just understanding your written text yet alone your source code that uses (sorry) horrible names for everything. Code should be written to be read by humans! – GhostCat Apr 25 '17 at 04:16
  • Maybe you are expecting pass-by-reference behavior. Java is always pass-by-value. When the example code says `second = first`, this just changes the local variables. The variables `u` and `v` in main aren't changed by that assignment. http://stackoverflow.com/q/40480/2891664 – Radiodef Apr 25 '17 at 04:31
  • Please clarify what you are asking for? How to swap arrays? How to edit array? How to copy array? Or how to write some algorithm for solving puzzle? – Krzysztof Cichocki Apr 25 '17 at 05:16
  • why does java cut off the last 2 elements of the array v when we call the method in the second case. puzzle(v,u,x). – Potato potato Apr 25 '17 at 05:19

3 Answers3

1

To copy array you have to loop through each element of array1 and put it into array2:

int[] array1=new int[]{1,2,3,4,5};
int[] array2=new int[5];
for(int i=0;i<array2.length;i++){
     array2[i]=array1[i];
}

And to edit array you put new value at specified index :

array1[2]=9;
Jay Smith
  • 2,331
  • 3
  • 16
  • 27
  • 1
    You can also use `arr.clone()` or `Arrays.copyOf(arr, arr.length)` to copy an array without a loop. – Radiodef Apr 25 '17 at 04:28
1

There is no difference between temp[2] and first[2]: temp and first refer to the same array.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

I think I understood your problem(if not ignore this answer). what you have to understand here is that when the puzzle function is executed for the 1st time, you did second=first. After that executes second will point to the same as first. But this will not have any effect on u and v. They will always point to the same location as they were before. The same repeats when the puzzle executes for the 2nd time.

The following pictures might give you more clarity. *The address values just an assumption. When the puzzle function is invoked for the first time and just after temp=first the situation is like thisenter image description here

The situation just after second[0]=2.34 isenter image description here

When the puzzle function is invoked for the second time and just after temp=first the situation is like this enter image description here

The situation just after second[0]=2.34 isenter image description here

The location to which u and v points to is never changed.