Problems with thinking problems cited by value in Java from leetcode 88

Problems with thinking problems cited by value in Java from leetcode 88

Nums1 function given as follows with the two arrays combined nums2 and places the result in nums1.

 public void merge(int[] nums1, int m, int[] nums2, int n) {
          int[] tar=new int[m+n];
	        int count=0;
	        int i=0;
	        int j=0;
	        while(count<(m+n)) {
	        	if(nums1[i]>=nums2[j]||i>=m) {
	        		tar[count]=nums2[j];
	        		count++;
	        		j++;
	        	}else if(nums1[i]<nums2[j]||j>=n) {
	        		tar[count]=nums1[i];
	        		i++;
	        		count++;
	        	}
	        }
	        
	        
	        nums1=tar.clone();

	        for(int xx=0;xx<tar.length;xx++) {
	        	nums1[xx]=tar[xx];
	        }
    }

As the above code block when I got back after the target array tar tar assigned nums1 found nums1 array array has not changed.
(As before are used to return back or some other method to solve the problem have not thought about this before so much)
final test pass value

 public void changer(int x) {
	    	x=10;
	    	System.out.println("函数中修改:"+x);
	    }
//main中
		int a=0;
		System.out.println("a: "+a);
		h.changer(a);
		System.out.println("a1: "+a);

最后输出结果:
a: 0
函数中修改:10
a1: 0

But there is no reference in Java and C by value in. And only Java parameter passing by value

I.e., by value: the value of his change in the function by value function outside the process. Only references will change.

1.java only passed by value, so-called by address (reference) transfer, also belongs passed by value, but the "value" is an address;

2. For a reference type parameter is passed by value and pass the value of a reference type is, in fact, is the address of the object;

1. java parameter values are passed.
2. java all variables are to be like for like references;

3. Or: copies are passed past, except that the copy is a basic type or a reference data;

4. The form parameters of the function, a copy arguments passed; copying between the reference variable [address], copies a value between the basic variables (known as Direct amount) in memory;
7. object itself, and address of the object is two things, between function if you want to pass an object [], can only be achieved by passing the address of the object;

Next write function to solve the above problems

Released nine original articles · won praise 4 · Views 4248

Guess you like

Origin blog.csdn.net/qq_42239081/article/details/100189850