Some notes on the copyofrange method

When I was writing questions on Niuke.com today, I encountered a problem of rebuilding a binary tree. I started to do it,,, I have an idea, but I just don't know how to edit it,,, Finally, I went to look at other people's code,,, Suddenly realized,,,, there is such a useful method as copyofrange,,, I hate my ignorance before,,, Finally, I compiled the program,,, but, it actually reported an error! ! ! Reported wrong! ! ! I checked it several times, but still got an error. . . Finally,,, found that the problem may appear in the copyofrange method,,, first introduce our protagonist: copyOfRange method


This is the description of the method in jpi, which has three parameters: the known array, and the two limit indices from and to.

The reason why the landlord did not compile successfully before is because he misunderstood the index parameter. Here from refers to the starting index of the copied array. Where to start copying, from is equal to the index there; to refers to copying the array. The end index, the final copied element does not include the element at the end index ! ! ! Remember remember! ! !

The following code is used to further illustrate:

import java.util.Arrays;

public class Test9 {
	public static void main(String args[]) {
		int[] a= {1,2,3,4,5,6,7,8};
		int[] b=Arrays.copyOfRange(a,0,4);
		System.out.println(Arrays.toString(b));
	}
}

The result of the above code is:


It can be seen that the result is not [1,2,3,4,5], but [1,2,3,4]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325381751&siteId=291194637