Data Structures ——-stacks, queues, and Arrays (iii)

Source: Internet
Author: User
Tags array length arrays data structures

In this article we present an array of the following data structures:


In programming languages, arrays are a type of data that we are more familiar with. Almost all programming languages set the array type to a fixed type.


the logical structure and basic operation of an array

Arrays can be regarded as a generalization of linear tables, and one-dimensional arrays are called constants,

one-dimensional array ":

Official explanation: An array of elements that consist of a set of data elements of the same type and stored in a contiguous set of storage cells we call arrays.

Two-dimensional arrays:

If the data element in a one-dimensional array is also a one-dimensional array structure, we call it a two-dimensional array. A two-dimensional array can be seen as a linear table of n-column vectors.

three-dimensional array ":

And so on, if the elements in a one-dimensional array are two-dimensional array structures, we become three-dimensional arrays .

N-dimensional array ":

Generally, an n-dimensional array can be viewed as a linear table of elements as n-1-dimensional arrays.

Having said so many official explanations, what exactly is an array?

Give me a chestnut:


The tadpoles we can call arrays.

Basic Operations for arrays:

Typically, there are only two basic operations of an array:

1) Read: Given a set of subscripts, returns the element content of that position.

2) Write: Given a set of subscripts, modify the element content of that location.



Second, the storage structure of the array:

The memory cell address of a one-dimensional array element is contiguous,

There are two types of storage methods for a two-dimensional array:

1) A sequence-based storage.

2) store with the main order of the line sequence.

In the C language, arrays are stored in a row-based manner, and in some other languages, arrays are stored in a sequence-based way.


The special matrices and sparse matrices in the compression storage of matrices are not described here, and are interested in self-study.


The following is the focus of this article:

Third, arrays in Java

Arrays in Java are not very difficult knowledge, but from the syntax of the words, just a lot of programmers have been using arrays, but often the Java array memory allocation is not accurate enough to grasp. I believe that through this article, we will certainly be aware of the codeing in the future.


1. Initialization of arrays

The Java array is static

Java is a typical static language, so the Java array is static, that is, when the array is initialized, the memory space and the array length of the array are immutable. Arrays in Java must be initialized before they can be used.

That is, create an actual array object, allocate memory space for it in memory, and specify an initial value for each array element.

There are two ways to initialize an array:

1) Static initialization: The initial value of each array element is explicitly specified by ourselves (the programmer) at initialization, and the length of the array is determined by the system.

2) Dynamic initialization: When initializing we only specify the length of the array, and the system assigns the initial value.

Regardless of the method used to initialize the array, the length of the array cannot be changed once the initialization is complete. Java only allows us to access the length of the array through the size of the array.

Tip : Do not use static initialization and dynamic initialization at the same time. To be blunt, you should not specify both the length of the array and the initial values for the arrays when it is initialized.


It says that the dynamic initialization array is assigned the initial value by the system.

The following are the system default assignment values:

1) array element is an integer type in the base type (byte, short, int, long,) array element initial value is 0

Array elements are floating-point types in the base type (float, double,) the value of the array element is 0.0

The array element is the base type of the character type (char,) the value of the array element is \u000

The array element is a Boolean type (Boolean,) in the base type, and the value of the array element is False

2) When array elements are reference types (classes, interfaces, and arrays), the initial value of the array element is null.

Tip : An array variable in Java is simply a variable of a reference type, not an array object itself, only an array variable can be used in a program if it points to a valid array object.

data variables and array objects must be clearly distinguished. (similar to a pointer in C language.) )

An array variable is simply a reference variable, and an array object is a contiguous memory space that is stored in the heap memory. The initialization of an array is not performed by an array variable, but instead it is created in heap memory-that is, the array object is allocated a contiguous amount of memory space, and the length of the contiguous memory space is the length of the array.

Special note ":

Arrays in Java must be initialized before they are used. A local variable in Java must be assigned an initial value by the programmer, even if it is null.

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.