Seeing JAVA-Array-Elementary Evolution 06

Seeing JAVA-Array-Elementary Evolution 06

Array definition

An ordered collection of the same type of data

There is a sequence, you can use the subscript to access

Array declaration

dataType[] arrayRefVar

Create array

dataType[] arrayRefVar = new dataType[arraySize]

data access

Accessed by index, starting from 0

Get array length

arrays.length

Basic characteristics of arrays

The length is determined, once determined, the size cannot be changed

The elements must be the same type, can be a basic type/reference type

Array variables are reference objects, arrays can be regarded as objects, and each element is equivalent to a member variable of the object

Java objects are in the heap, so the array object itself is in the heap

Java memory analysis

heap

Store new objects and arrays

Can be shared by all threads and will not store other object references

Stack

Store the basic variable type (including the specific value of this basic type)

The face of the reference object (put the reference to the specific address in the heap)

Method area

Shared by all threads

Contains all class and static variables

1. Declare the array:

int[] array = null;

2. Create an array

array = new int[10];

3. Assign values ​​to array elements

array[0]=1

initialization

Static initialization: create and assign

Dynamic initialization

Default initialization of the array: the array is a reference type, the element is equal to the class example

abnormal

The array subscript is out of bounds:

ArrayIndexOutofBoundsException

use

For-each loop

Array as method parameter

Array as return value

Multidimensional Arrays

int[][] arrayB = new int[2][4];

Arrays类

Array of tools

The methods are all static methods modified by static, which can be called directly by the class name

Common Functions:

  • Assign a value to the array: fill
  • Sort the array: sort (ascending order)
  • Compare array: equals
  • Find array elements: binarySearch method performs binary search operation on the sorted array

Bubble Sort

(8 sorting algorithms) sorting algorithm

O (n2)

Sparse array

Introduction:

When most elements in an array are 0 or the same value

The processing method is to record the number of rows and columns in the array, how many different values ​​there are, and record the elements with different values, rows, columns and values ​​in a small-scale array

Guess you like

Origin blog.csdn.net/rr18758236029/article/details/108422500