07. Java Basics-Arrays (array)

Source: Internet
Author: User
Tags array definition array length

Java Basics-Arrays (array)

Directory

I. About arrays

Two. Memory structure of the array

Three. Array Definition FAQs

Some notes

Parameter passing of an array: The reference address is passed, and if the element in the memory address is changed in the parameter, the variable referencing that memory address is also changed.

I. About Arrays

The concept of arrays

A collection of the same type of data. (Can be either a basic data type or a reference data type)

Features of the array

    • Arrays are stored with elements of the same data type
    • The length of the array is the number of elements in the array
    • The element is numbered starting from 0, and the number is also called "Index": Index (subscript, corner)
    • The way an element in an array is accessed is by means of an array name + index, such as int [0]

The definition format of an array

The initialization method is divided into: dynamic initialization and static comfort

Dynamic initialization: the creation of arrays and the assignment of elements separately

1 /* 2 element type [] Array name = new element type [array length]; 3 */ 4     int New int [3];

Static initialization: Array elements are assigned values when the array is created

1 /* 2 element type [] Array name = new element type []{element 1, element 2,...}; 3 */ 4     int New int []{2,0,3,1}; 5     int [] arr = {2,0,3,1};    // simplified notation for static initialization

II. memory structure of an array 2.1 about memory structure

Java program at run time, in order to improve the efficiency of the memory of the different areas of the partition, each region has a specific way of processing data and memory management methods, mainly the following:

Stack memory: Used to store local variables, when the data is used up, the occupied space will be automatically released;

Heap Memory: Stores arrays and objects, and the instances created by new are stored in heap memory

Each instance has a memory address value;

The variables in the instance have default initialization values;

When an instance is no longer in use, it is reclaimed by the garbage collector at an indeterminate time.

Method Area: Storing class files and methods

Local method Stack: For local method use, OS-related

Program Counter--program Counter: Byte-code file count

2.2 Memory structure of the array

That is, based on the length of the data and the corresponding type, open up a memory space. (e.g. int[] arr = new int[5]; it opens up 5 positions of type int that can place elements of type int)

The array element is used in the following way: Array name + index (e.g. int [0]).

Three. FAQ for arrays

Array index out of bounds exception (arrayindexoutofboundsexception)

1 int New int [2];        // created an array that can put 2 elements of type int 2 System.out.println (arr[2]);    // The index is starting at 0, and the statement is an element that prints an index of 2 3 // Cause: An exception occurred accessing an index that does not exist in the array

Null pointer exception (NULLPOINTEREXCEPTION)

1 int New int [2]; 2 NULL ; 3 System.out.println (arr[0]); 4 // Cause: The arr reference does not point to the array, but the elements in the manipulation array

Traversal of an array

    • Array traversal: Each element is accessed sequentially, and the number of accesses is the number of elements.
    • Number of visits: The Length property determines the number of times the array is to be accessed.

Note: You can encapsulate a print statement into a method that is easy to use multiple times.

07. Java Basics-Arrays (array)

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.