An array of Java foundations

Source: Internet
Author: User

There are three differences between arrays and other kinds of containers: efficiency, type, and the ability to preserve basic types. In Java, an array is the most efficient way to store and randomly access an object's reference sequence, which is a simple linear sequence ( arrays and generics can be converted )

 PackageUnit_sixteen;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.List; Public classContainercomparison { Public Static voidMain (string[] args) {berylliumsphere[] spheres=NewBerylliumsphere[10];  for(inti = 0; I < 5; i++) {Spheres[i]=NewBerylliumsphere ();        } System.out.println (Arrays.tostring (spheres)); System.out.println (spheres[4]); List<BerylliumSphere> spherelist =NewArraylist<berylliumsphere>();  for(inti = 0; I < 5; i++) {Spherelist.add (NewBerylliumsphere ());        } System.out.println (Spherelist); System.out.println (Spherelist.get (4)); //The array length is fixed, but it can be interchanged with the set generic, and the rest of the array is efficiency!         int[] integers = {0, 1, 2, 3, 4, 5 };        System.out.println (arrays.tostring (integers)); System.out.println (integers[4]); List<Integer> intlist =NewArraylist<integer> (arrays.aslist (0, 1, 2,                3, 4, 5)); Intlist.add (97);        System.out.println (intlist); System.out.println (Intlist.get (4)); }}//There is only one public class in a classclassBerylliumsphere {Private Static Longcounter; Private Final Longid = counter++;  PublicString toString () {return"Sphere" +ID; }}

The object array and the base type array are almost identical in usage, except that the object array holds the reference , and the primitive type array directly holds the value of the base type.

 PackageUnit_sixteen;Importjava.util.Arrays; Public classarrayoptions { Public Static voidMain (string[] args) {berylliumsphere[] A; Berylliumsphere[] B=NewBerylliumsphere[5]; System.out.println ("B:" +arrays.tostring (b)); Berylliumsphere[] C=NewBerylliumsphere[4];  for(inti = 0; i < c.length; i++) {            if(C[i] = =NULL) {C[i]=NewBerylliumsphere (); }} berylliumsphere[] D= {NewBerylliumsphere (),NewBerylliumsphere (),Newberylliumsphere ()}; A=NewBerylliumsphere[] {NewBerylliumsphere (),Newberylliumsphere ()}; System.out.println ("A.length=" +a.length); System.out.println ("B.length=" +b.length); System.out.println ("C.length=" +c.length); System.out.println ("D.length=" +d.length); //point A to the reference to DA =D; System.out.println ("A.length=" +a.length); int[] e; int[] f =New int[5]; System.out.println ("F:" +arrays.tostring (f)); int[] G =New int[4];  for(inti = 0; i < g.length; i++) {G[i]= i *i; }        int[] h = {11, 47, 93 }; System.out.println ("F.length=" +f.length); System.out.println ("G.length=" +g.length); System.out.println ("H.length=" +h.length); //point E to the reference to HE =h; System.out.println ("E.length=" +e.length); E=New int[] {1, 2 }; System.out.println ("E.length=" +e.length); }}

sort (t[] a) and sort (t[] a,int fromindex,int toindex), where sort (int[] a,int fromindex,int toindex) is Sorts the specified range of arrays of the specified type in ascending order of numbers. The sort ranges from index fromIndex( including ) to index toindex ( not included ). If Fromindex==toindex, the sort range is empty;BinarySearch (t[] b,t key) and BinarySearch ( int[] a,int fromindex,int toindex,int key ),where BinarySearch (t[] a,int fromindex,int toindex,int key) Use the binary search method to search for the specified range of array of type int to get the specified value. The scope must be sorted (by method) before this call is made sort(int[], int, int) . If the range is not sorted, the result is indeterminate. If the range contains more than one element with the specified value, it is not guaranteed which one is found;equals (data type [], data type []) returns trueif the two specified array of data types are equal to each other. If two arrays contain the same number of elements, and all corresponding element pairs in two arrays are equal, the two arrays are considered equal

 PackageUnit_sixteen;ImportJava.lang.reflect.Array;Importjava.util.Arrays; Public classPractice { Public Static voidMain (string[] args) {int[] A =New int[] {11, 1, 2, 3, 4, 6, 4, 3, 5, 7 }; int[] B =New int[] {11, 1, 2, 3, 4, 6, 4, 3, 5, 7 }; int[] C =New int[] {5, 1, 2, 3, 4, 6, 4, 3, 5, 9 };        System.out.println (Arrays.tostring (a)); // //Array Sorting//Arrays.sort (a); //System.out.println (Arrays.tostring (a)); //Arrays.sort (b, 3, 8); //System.out.println (arrays.tostring (b)); // //to sort an array before calling BinarySearch//Arrays.sort (a); //int m = Arrays.binarysearch (A, 11); //System.out.println (m); //Arrays.sort (b, 3, 8); //int n = arrays.binarysearch (b, 3, 8, 6); //System.out.println (n);        BooleanBlone =Arrays.equals (A, b); BooleanBltwo =Arrays.equals (A, c);        System.out.println (Blone);    System.out.println (Bltwo); }}

An array of Java foundations

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.