Java Array Learning Notes

Source: Internet
Author: User
Tags array length

Array

Number leader in Java like this (define array)

int [] arr; // Recommended int arr[];  
first, the definitionwhat is an array?

An array is the same type of an object sequence or primitive type data sequence that is encapsulated together with an identifier name. a simple set of elements of the same type .

Why use arrays?

When you need to store a large amount of data, such as the need to read 100 numbers, then you need to define 100 variables, and the array solves this problem, through the array container to the same type of elements with an identifier, easy to store and manipulate.

Second, format

Format 1: element type [] Array name = new element type [element number or array length]

Such as:

int New int [10];

Format 2: element type [] Array name = new element type []{element, element, element,.....}

Such as:

int New int []{1,2,3,4}; int [] arr = {1,2,3,4}; // special initialization, storage space allocation (equivalent to using new) is the responsibility of the compiler. 
Iii. Advantages and disadvantages

Advantages:
1. Query elements faster by index
2, can store a large amount of data
3. It is convenient to iterate through the array by index

Disadvantages:
1, according to the content to find the element speed slow
2. Once the size of the array is determined, it cannot be changed.
3, arrays can only store one type of data
4. Efficiency of adding and removing elements is slow
5. No method is encapsulated and all operations need to be defined by the user.

Iv. Common Anomalies

Null pointer exception (NULLPOINTEREXCEPTION)

Typically, you create only the reference to an array without pointing to any objects, and are used by other objects or methods. such as:

Static int [] arr;  Public Static void Main (string[] args) {      for (int i = 0; i < arr.length; i++) {         System.out.print ln (arr[i]);}       }

Array out of bounds (arrayindexoutofboundsexception)

The index value of the array starts at 0 and accesses the non-existent index value. such as:

Static int [] arr = {1, 2, 3 };  Public Static void Main (string[] args) {    System.out.println (arr[-1]);    System.out.println (arr[3]);}

Java Array Learning Notes

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.