Java basics: array creation, initialization, reference, classification

Array Basics

An array is a data structure that contains elements of the same type. This means that all elements in the array are of the same data type.

Let's take an example:

This is an array of seven elements, all integers, and the green box below the array is called the index, which always starts at zero and goes up to n-1 elements. In this example, there are seven elements, indexed zero to six.

Arrays have the following three main characteristics:

  • Dynamic Allocation : In an array, memory is created dynamically, which reduces the amount of storage required by the code.
  • Array name : All elements are stored under a name which is used whenever we work with arrays.
  • Positional Contiguous : Elements in an array are stored in adjacent positions, which allows users to easily find the position of their elements.

Array declaration/creation, initialization, assignment

To use an array, you must go through the steps of declaration or creation to store the array information.

Then specify the size of the array when generating, and allocate a memory area matching the specified size in memory when generating.

array declaration

数组类型 [ ] 数组名;

type[] arrayName;

For example:

int[] intArray;   // 声明一个int类型的数组
String[] stringArray;   // 声明一个String类型的数组

array creation

You can also use new to create like other objects in java:

数组类型[] 数组名 = new 数组类型[size]; 

type[] arrayName = new type[size];

For example:

int[] intArray = new int[5];

array initialization

An alternative to declaring and creating an array using the new operator is to specify an initializer. An initializer is a method of creating an array by directly assigning data when it is created. The size of the array when specifying an initializer is given by The number of data to be allocated is determined.

数组类型[ ] 数组名 = {data, data, ...};

type[ ] arrayName = {data, data, ...};

For example:

String[] javaArray = {"Spring", "SpringBoot", "SpringCloud"};

Array classification

Arrays contain three categories:

one-dimensional array

One-dimensional arrays are also known as linear arrays, where elements are stored in one row.

For example:

int arr [ ]  =  new  int [ 3 ];

int[] arr Reference to an array of 3 integers:

Two-dimensional array

Two-dimensional arrays store data in rows and columns:

For example:

int[ ][ ] arr = new int[3][ ];
arr[0] = new int[3];
arr[1] = new int[5];
arr[2] = new int[4];

A row of 3-element arrays is first created, the first element creates a one-dimensional array with 3 elements, the second element creates a one-dimensional array with 5 elements, and the third element creates a one-dimensional array with 4 elements array.

Multidimensional Arrays

A multidimensional array is a combination of two or more arrays or nested arrays, similar to a two-dimensional array.

array reference

We also said at the beginning of the article that arrays use index numbers to address each element in the array. The index numbers start from 0 and go to the length of the array -1. For example, an array with a size of 10 has index numbers from 0 to 9. .

So if you want to access the values ​​in the array, you can pass:

array[index];

For example:

String[] cites = { "南京", "苏州", "无锡", "南通", "淮安", "常州", "徐州"};

System.out.println("恐龙园在江苏" + cites[5]);

Output result:

恐龙园在江苏常州

Summarize

This article introduces Java arrays to you. First, it introduces the basics of arrays, then introduces the creation, initialization, and classification of arrays, and finally introduces the references of arrays.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132289878