Android Note: Arrays in Java

Source: Internet
Author: User

The byte stream obtained by the socket communication used in the process of communicating with the embedded device is typically converted to a byte array, and an array of bytes needs to be split according to the protocol. For regular repeated splits can be used, because Java can not be directly in the memory operation in C, such as using a struct memory copy may be directly implemented, here is to consider the use of array copies of the way to achieve.

Java array Replication Reference java.lang.System.arraycopy and Java.util.Arrays.copyOf method of reference array copy from Baidu space

 <   length   =a[i";  }  the second way to assign values directly:  int[] Array1   
 Here's a look at the System.arraycopy function, from the Java API. Let's have a look. public static Voidarraycopy (object src, int srcpos, object dest, int destpos, int length) copies an array from the specified source array, copying from the specified position to the target array Positioning end. A sub-sequence of the array component is copied from the source array referenced by SRC to the target array of dest references. The number of the component being copied is equal to the length parameter. The components in the source array that are positioned between Srcpos to Srcpos+length-1 are copied to the Destpos to destpos+length-1 location in the destination array, respectively. If the parameter src and dest refer to the same array object, the execution of the copy is as if the first component of the Srcpos to Srcpos+length-1 location is copied to a temporary array with the length component. The contents of this temporary array are then copied to the Destpos to destpos+length-1 location of the destination array. If dest is null, the NullPointerException exception is thrown. If SRC is null, the NullPointerException exception is thrown and the destination array is not modified. Otherwise, the arraystoreexception exception is thrown and the target array is not modified as long as any of the following conditions are true: the SRC parameter refers to a non-array object. The dest parameter refers to a non-array object. The SRC parameter and the dest parameter refer to arrays whose component types are of different primitive types. The SRC parameter refers to an array with a basic component type, and the dest parameter refers to an array with a reference component type. The SRC parameter refers to an array with a reference component type, and the dest parameter refers to an array with the base component type. Otherwise, if any of the following conditions are true, the indexoutofboundsexception exception is thrown and the target array is not modified: The Srcpos parameter is negative. The Destpos parameter is negative. The length parameter is negative. Srcpos+length is greater than src.length, which is the length of the source array. Destpos+length is greater than dest.length, which is the length of the destination array. Otherwise, a Arraystoreexception exception is thrown if the actual component in the source array srcpos to the srcpos+length-1 position is not converted to the component type of the destination array by allocating the transform. In this case, theK is set to the smallest non-negative integer smaller than the length, so that src[srcpos+k] cannot be converted to the component type of the target array, and when an exception is thrown, the source array component from Srcpos to srcpos+k-1 location has been copied to Destpos to destpos+ in the destination array K-1 position, and other locations in the destination array are not modified. (Because of the limitations that have been described in detail, this paragraph can only be applied effectively to cases where two arrays have a reference type of component type.) ) Parameter: src-source array. Srcpos-the starting position in the source array. dest-the target array. Destpos-the starting position in the target data. length-the number of array elements to copy. Thrown: indexoutofboundsexception-If replication results in access to data outside the array range. arraystoreexception-if the elements in the SRC array cannot be stored in the dest array because of a type mismatch. nullpointerexception-if SRC or dest is null.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.