Java entry (3) -- array, java entry Array

Source: Internet
Author: User

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

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.