Java Learning Note Five Java array

Source: Internet
Author: User

Array

Concept

A collection of the same type of data. The array is actually a container.

The benefits of arrays

The elements in the array can be automatically numbered starting with 0, allowing them to be manipulated.

Format 1:

Element type [] array name = new element type [ element number or array length ];

Example:int[] arr = new int[5];

Format 2:

Element type [] array name = new element type []{ element, element,...};

int[] arr = new int[]{3,5,1,7};

Int[] arr = {3,5,1,7};

If you need to store a large amount of data, for example, if you need to read 100 numbers, then you need to define 100 variables, obviously repeated 100 times the code, is not very significant. How to solve this problem, the Java language provides an array of data structures, is a container can store the same data types of elements, you can store 100 numbers in an array.

1 Concept of arrays

A collection of the same type of data. The array is actually a container. There is a lot of data involved in the operation, so the first thing to do is what to do. Not how to do the operation but how to save the data to facilitate later operations, then the array is a way to store data, where the data can be stored in what we call the container, the contents of the container is the elements of the array, An array can hold any type of data, although it can be any type of data, but a well-defined array can only have one element, that is, once the array is defined, the data types stored inside are determined.

2 Benefits of arrays

What is the difference between saving data and not saving data? The best thing about arrays is that you can automatically number the elements that are stored in them. Note that the numbering is starting from 0. Easy to manipulate the data.

For example, the student's number, the student number can be used to find the corresponding students.

3 format of the array

element type [] Array name = new element type [element number or array length];

Example: int[] arr = new INT[5];

Case:

Requirement: To define a container that can store 3 integers

Realize:

1 declaring an array variable

In order to use an array, you must declare the array in your program and specify the element type of the array

= Left Half part:

First write the left to make clear that the element type is int, the container uses an array, then how to identify the array? Then use a special symbol [] in brackets to denote. To use an array is to give the array a name, so here we give the array the name X. and then the equals sign.

The code reflects:

int [] X

Note: int x[] is also a format for creating arrays. It is recommended to declare an array in the form of int [] X.

2 Creating an array

= Right Half part:

To use a new keyword. It is called New. New is used to generate a container entity in memory, the data to be stored is needed space, the space to store a lot of data with the new operator to open, new int[3]; This 3 is the number of elements. The part on the right is the definition of a real array in memory that can store 3 elements.

New Int[3] Did two things, first creating an array with new int[3] and then assigning the array reference to the array variable x.

int [] x=new int[3];

What type is X?

Any variable must have its own data type. Note that this x is not of type int. int represents the type of element inside the container. Then x is the array type.

An array is a separate data type. Data types are divided into 2 major factions, which are divided into basic data types and reference data types. The second big pie is the reference data type. So now you have access to one of the three types of reference data. That is, the array type [] brackets represent the array.

4. int[] arr = new Int[5]; what happened in memory?

Memory any program, run the time need to open up in memory space. int[] arr = new INT[5]; What does this program do in memory? This involves the space that the Java virtual machine opens up when it executes the program, so how much space does Java open up? Continue to learn about the memory structure of Java.

definition of an array

Format 1:

element type [] Array name = new element type [element number or array length];

Example: int[] arr = new INT[5];

Format 2:

element type [] Array name = new element type []{element, element, ...};

int[] arr = new int[]{3,5,1,7};

Int[] arr = {3,5,1,7};

Note: When allocating space to an array, you must specify the number of elements that the array can store to determine the size of the array. The size of the array cannot be modified after the array is created. You can use the Length property to get the size of the array.

Iterating through an array

Array Initialization

Format of the array

int[] x = new int[3];

X[0] = 1;

X[1] = 2;

Another definition: This form can directly define the length of the array, as well as the contents of the elements in the array

int[] x = {1, 2, 3};

Int[] X=new int[]{1,2,3};

Initialization Mode 1: Do not use operator new

Int[] arr = {1, 2, 3, 4, 5};

int[] arr2 = new int[] {1, 2, 3, 4, 5};

Initialization Mode 2:

Int[] arr3=new int[3];

Arr3[0]=1;

arr3[1]=5;

arr3[2]=6;

If operator new is not used in array initialization. Note: The following notation is incorrect.

Int[] arr;

arr={1,2,3,4,5};

At this point, the array must be initialized, the declaration, the creation, the initialization is placed in one statement, the separation will produce a syntax error.

So you can only write as follows:

Int[] arr={1,2,3,4,5};

Array Traversal

Public static void Main (string[] args) {

int[] x = {1, 2, 3};

for (int y = 0; y < 3; y++) {

System. out.println (X[y]);

System.out.println ("x[" +y+ "]=" +x[y]); printing effect x[0]=1;

} //Then this is the first common operation of the array. Traverse

}

There is an attribute in the array that gets the number of elements in the array, that is, the length of the array. Array name. length

Public static void Main (string[] args) {

int[] x = {1, 2, 3};

for (int y = 0; y < x.length; y++) {

System. out.println (X[y]);

System.out.println ("x[" +y+ "]=" +x[y]); Printing effect x[0]=1;

} //Then this is the first common operation of the array. Traverse

}

Common Exceptions for arraysThe most common problems in arrays are:

1. NullPointerException NULL pointer exception
Cause: The reference type variable does not point to any object, and the property of the object is accessed or the method that called the object. \


2. ArrayIndexOutOfBoundsException index value is out of bounds.
Cause: A non-existent index value was accessed.

An array of corner labels out of bounds exception:, note: The array's Corner label starts at 0.

Public static void Main (string[] args) {

int[] x = {1, 2, 3};

System. out.println (x[3]);

Java.lang.ArrayIndexOutOfBoundsException

}

Two NULL pointer exceptions:

public static void main (string[)  args)  {

int[] x = { 1, 2, 3 };

null;

Out.println (x[1]);

//  Java.lang.NullPointerException

}

Array:

When to use arrays: when the elements are more convenient to manipulate these arrays, they are first stored temporarily, and the container used is the array.

Characteristics:

The array length is fixed.

memory analysis of arrays





two-dimensional arrays

Use of arrays

Traverse: ToString () returns the elements of an array as a string

Sort: sort () sorts the array in ascending order

Find: BinarySearch () finds the specified element in the specified array, returns the index of the element, if no return is found (-insertion point-1) Note: When using the Find function, the array must be sorted first.

Two-dimensional arrays: the essence is that storage is a one-dimensional array.

Array definition:

Array type [] Array name = new array type [number of one-dimensional array] [number of elements in each one-dimensional array];



Question: why a.length = 3, A[0].length = 4?

Initialization of the array:

Static initialization :

int [] [] A = new int[][]{{12,34,45,89},{34,56,78,10},{1,3,6,4}};

Dynamic initialization:

Java Learning Note Five Java 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.