The definition of array and how to use it

Preface

Array! ! !


Tip: The following is the content of this article, the following cases are for reference

1. What is an array?

An array is a collection of multiple variables of the same type (all elements stored in an array must be of the same type), and it can be referenced by a common name.
1. It belongs to a complex data type
2. An ordered data collection composed of elements of the same type
3. Java arrays are fixed and cannot be extended [Once the length is declared, it cannot be modified]
4. Basic data types or objects can be stored
5. Arrays can Defined as any data type, and can be divided into one-dimensional array or multi-dimensional array

2. How to use

type var-name[ ];

##For example: int [] arr1 = new int[10];

int arr2[] = new int[20];
Both definitions are correct

The elements in the array must be of the same type and cannot store elements of different types, unless you
can use Object[] to manipulate the elements stored in the array through the subscript of the array. Note that the subscript starts from 0, such as arr[0]+=12; Modify the value of the element
System.out.println(arr[0]); Get the element
Before using the array, you must first define it and use it. The definition method: int[] arr or int arr[]
After the array variable is declared, the initialization operation must be performed , That is, define the length of the array arr=new int[5], here it means to open
an array that can store 5 int type data, these elements are continuously stored in a
simple type of array after initializing the length, each element has a default value
after creating an array, each element has a default value, if the reference type is directed, the default value is null; if the class is a simple
type byte short int long float double, the default value is 0, the default value if the char type It is \0. If the
type is boolean , the default is false.
In Java, direct assignment is allowed. Initial
int[] arr={1,2,3,4,5,6,7}; Note that int[] cannot write specific The corresponding length
can also be written as new int[]{1,2,3,4,5,6,7}, but int[] cannot have a length value
int[] arr1 = new int[10]; // int[] Or arr2[] are used to declare the array type, int is used to declare that each
element is of the int type
int arr2[] = new int[20]; //The required space must be declared before using the array, that is Elements stored
Number, once declared, you cannot modify
int[] brr;
brr[0]=123; //Syntax error
1
2
3
4
5
int[] brr=new int[10];
brr[0] = 123.; //Type Must be consistent brr[0] represents the 0th element of the array, [] is the index subscript value-
serial number
Object[] brr=new Object[10];
brr[0] = 123.;
brr[1]="sdafsd ";
1
2
3
4
5
6
int[] arr=new int[10];
//All numeric data defaults to 0, boolean type defaults to false, char type defaults to'\0'
System.out.println(arr[1 ]); //The output is 0
1
2
3
Integer[] arr=new Integer[10];
System.out.println(arr[1]); //The output is null
1
2
Declare an int type one-dimensional array: int number[];
[] Is on the left side of the byte, which has an effect on all variables after char[] s, t;
although the array declares the variable type. But there is no actual value, its value is null. In order to make the array number an actual, physical
integer array, you must use the operator new to assign an address to it and assign it to the number
operator. New is an operator specifically used to allocate memory. The format is: array-var = new type[size]; The opened space is
continuous and can be quickly located by subscripts.
For example, the above code: int[] kk=new int[10]; There is an attribute length in the
length attribute
array object Represents the length of the array.
Use the operator new to allocate the array. You must specify the type of array element and the number of array elements. After the
array is allocated with the operator new , the elements in the array will be automatically initialized to a value. The specific value of this value is related to the type. If it is a numeric type [integer, floating
point, char type], the automatic initial value is 0, if the boolean type, the automatic initial value is false,
such as s = newchar[26]; program, generate an array containing 26 character values, and the initial value of each array element is set to '\ u0000'
when creating the array of simple types, if it is numeric type (integer or floating point), the default element is 0; if it is character, the default meta-
prime to \ u0000 [Note This is not the character 0, the value of character 0 is 48]; if it is boolean, the default element is false
//If an array of complex type is created, such as String[]b=new String[10], the default element is
Access to null array elements
The elements in the array can be accessed through subscripts. The syntax for access is: The name of the data variable [subscript]
All elements in the array will be initialized to zero. Next, you can assign a value to each element in the array

int[] arr;//Declare an integer array variable arr
System.out.println(arr); //Syntax error, must be declared before use, first assign initial value before use

This is not realistic enough to assign an array value
. Initialize
at the same time as the declaration. Specify the initial value of the element when the array is defined, which is called the initialization of the array
int a[]={1,2,3,4,5};
Declare an array and initialize the array Content
int[] score = {90, 85, 55, 94, 77}; //It can also be written as new int[]{90, 85, 55, 94, 77}, but it is not allowed to be written as new
int[5]{90 , 85, 55, 94, 77}, syntax error,
formatted output,
error analysis: you can see the error message ArrayIndexOutOfBoundsException on the console, indicating that the array is out of bounds, that is, to access
a non-existent element.
When the array element is taken out, the array subscript index Cannot exceed the array range [0 to length-1], if it exceeds the array range,
ArrayIndexOutOfBoundsException will occur . For example, if the number of elements in an array is 5, and the sixth one is taken, the program
will prompt the error that the subscript is out of bounds.
You can declare the length of the array in a dynamic way instead of determining the array in advance. size

Guess you like

Origin blog.csdn.net/weixin_42437438/article/details/112384563