Java entry (3) -- array, java entry Array
Array
Overview: An array is a single data type that a container can store a fixed number of data types.
Feature: The container length is fixed and the Data Types of elements in the array must be consistent.
Define an array:
1. Declare first, and then initialize
Declaration: Data Type [] array name; // identifier naming rules and specifications
String [] names;
Initialization:
Array name = new data type [length];
Names = new String [4]; // The maximum index is 3.
2. Declare initialization at the same time
Data Type [] array name = new data type [length]; // recommended
String [] names = new String [4];
Equal to left
String [] String array type
Names array name // address for storing arrays \ reference
Right of equal sign
New is to open up space for arrays.
The elements in the String array can only be of the String type.
[] Indicates an array. // any brackets that appear are arrays.
4 represents the length of the array
3. Quickly define an array
1. Data Type [] array name = {value 1, value 2, value 3,...}; // recommended
String [] names = {"Zhang San", "Li Si", "Wang Wu", "Zhao Liu "};
2. Data Type [] array name = new data type [] {value 1, value 2, value 3 ,...};
String [] names = new String [] {"Zhang San", "Li Si", "Wang Wu", "Zhao Liu "};
3. Length of the array
The length of the array is an attribute of the array. Once the array is generated, the length is determined.
Array name. length // calculate the length of the array
The array has numbers \ subscript \ Index
Index: The maximum index of array elements starting from 0 is: length of the array-1
Assignment: assign values to the array name [Index] = value based on the index; // names [0] = "jack ";
Value: array name [Index]; // names [0];
4. Common Errors:
Error 1:ArrayIndexOutOfBoundsException: array out-of-bounds exception
Cause: access to an index that does not exist
Solution: access existing Indexes
Error 2:StringIndexOutOfBoundsException: An exception occurred while the string index was out of bounds.
Cause: access to an index that does not exist
Solution: access existing Indexes