[Java Basics] Array 1

Source: Internet
Author: User

[Java Basics] Array 1
1 /**2 file path: G:\JavaByHands\ array3 file name: Arraytest.java4 Write Time: 2016/6/135 Write a description: Learn how arrays are used ~ ~6 knowledge Source: Crazy Java handouts7 */8 9 /*Ten arrays are a data structure used to store multiple data ~ ~ One a data is made into an array element A array elements can be accessed through the index of an array element - Java requires that all array elements have the same data type - once the array initialization is complete, the space occupied by the array in memory will be fixed and the length immutable the arrays can store either the base data type or the reference data type - The array itself is a reference type -  - definition array: type[] arrayname;==> type [] array name; +  - initializing an array + 1: Static initialization: Specifies the initial value of each array element when initialized, and the system determines the length of the array A format: arrayname = new type[]{element1,element2 ...}; at Simplified: arrayname = {element1,element2 ...}; {This will cause an error ... No initialization ... } - freq used: type[] arrayname = {element1,element2 ...}; - Type is the data type of the array element and must be the same as the type that was used to define the variable - can also be a subclass of the type used when defining an array - array elements are enclosed in curly braces and separated by commas. -      in 2: Dynamic initialization: Dynamic initialization refers to the length of the specified array, with the system specifying the initial value for each array element - format: arrayname = new Type[length]; to Length is the size of the array, that is, the number of elements that can hold the array, type int + type with static initialization ~ ~ -          the Rules: * Reshape: Default value is 0 $ Float Type: Default value is 0Panax Notoginseng character Type: Default value is ' \u0000 ' - Boolean: The default value is False the Reference type: Default value is null +              A ! Note: Static initialization and dynamic initialization cannot be used at the same time ~ ~ the      + Working with Arrays - accessing array elements and assigning values to arrays of elements is typically accomplished by adding index values after the array reference variable. $ index: square brackets plus int type Value ~~java index starting at 0 $ string[] Arrx = {"1", "2", "3", "4", "5"}; String[] Arrx = {"Zhao", "Qian", "Sun", "Li", "Zhou"}; -                       |   |   |   |                        |    |    |    |    | | - index value [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] the important things to say three times: - The index value must be starting from 0, and the last index value is the length of the array-1!!! Wuyi The index value must be starting from 0, and the last index value is the length of the array-1!!!  the The index value must be starting from 0, and the last index value is the length of the array-1!!!  -      Wu array Lengths: length - gets the values in the array, which can be traversed by the for loop. About int[] arr = {1,2,3,4,5,6}; $ for (int i = 0; i < arr.length; i + +) { -          - System.out.println (i); -          A     } +      the jdk1.5 above provides a foreach loop - int arr2 = {1,3,5,7,9}; $ for (int i:arr2) { the          the System.out.println (i); the          the     } - when iterating over an array element using a Foreach loop, the loop variable is equivalent to a temporary variable, changing the variable without changing the value in the array ~ ~ in      the  the */ About  the  Public classarraytest{ the      the      Public Static voidMain (string[] args) { +          -         //statically initializing an array the         int[] arr;Bayiarr =New int[]{1,2,3,4,5}; the          the         /*Note: This will be an error in writing ... Although the book has this kind of writing ... But the actual test time error ... - * Also measured with eclipse ... Still error - *array constants can only is used in initializers array constants can be used only for initialization the * Still need to practice AH ~ ~ the          */ the         //int[] arr1; the         //arr1 = {1,2,3,4,5}; -  the         int[] arr1 = {1,2,3,4,5}; the          the         int[] arr2 = {1,2,3,4,5};94          the         //Initializes an array where the array element is a subclass of the defined type the object[] Objarr; theObjarr =NewString[] {"Object", "subclass"};98          About         //dynamic initialization of arrays -         int[] Arr3 =New int[5];101         //Initializes an array where the array element is a subclass of the defined type102object[] ObjArr1 =NewString[3];103         104         //Assigning a value to an array that is dynamically initialized theArr3[0] = 5;106ARR3[4] = 7;107         108         //outputs the value of a single array element109         inti = arr1[3]; the System.out.println (i);111          the         //Iterating through an array113          for(intj = 0; J < Arr2.length; J + +) { theSystem.out.print ("arr2[" + j + "] =" + Arr2[j] + ","); the         } the118         119         //foreach Loop -          for(intk:arr1) {121 System.out.print (k);122         }123                 124     } the}

[Java Basics] Array 1

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.