Definition of an array
- An array is an ordered collection of data of the same type
- An array describes several pieces of data of the same type arranged in a certain order
- Where each piece of data is called an array element, and each array element can be accessed by a subscript
Array declaration and creation
- Array variables must be declared before arrays can be used in a program. Here is the syntax for declaring array variables:
dataType[] arrayRefVar;// Preferred method
dataType arrayRefVar[];// It has the same effect, but is not recommended as a preferred use
Copy the code
- The Java language uses the new operator to create arrays with the syntax:
dataType[] arrayRefVar = new dataType[arrayRefVar];
Copy the code
- The elements of an array are accessed by an index, which starts at 0
- Get the length of the array:
arrays.length
Copy the code
- The sample;
package com.Ji.array;
import javax.swing.*;
public class ArrayDemo01 {
public static void main(String[] args) {
int[] nums;// Declare an array
nums = new int[5];// Create an array
// Assign values to array elements
nums[0] =1;
nums[1] =2;
nums[2] =3;
nums[3] =4;
nums[4] =5;
// Compute the sum of all elements
int sum =0;
for (int i = 0; i <nums.length ; i++) {
sum = sum + nums[i];
System.out.println("Combined"+sum); }}}Copy the code
Three kinds of initialization and memory analysis
Memory analysis
- Java memory analysis
- The heap
- Holds new objects and arrays
- It can be shared by all threads and does not store other object references
- The stack
- Store the base variable type (including the base type value)
- The variable that refers to the object (which stores the address of the reference in the heap)
- Methods area
- Can be shared by all threads
- Contains all class and static variables
- The heap
Three initializations
- Static initialization
int[] a ={1.2.3.4.5}
Man[] mans={new Man[1.1].new Man[2.2]};Copy the code
- Dynamic initialization
int[] a =new int[2];
a[0] =1;
a[1] =2;
Copy the code
- Default initialization of an array
- An array is a reference type, and its elements are equivalent to instance variables of the class, so once an array is allocated, each element is implicitly initialized in the same way as instance variables
package com.Ji.array;
public class ArrayDemo02 {
public static void main(String[] args) {
// Static initialization
int[] a ={1.2.3.4};
System.out.println(a[0]);
// Dynamic initialization
int[] suns;
suns = new int[5];
suns[0] =10;
suns[1] =2;
System.out.println(suns[0]);
System.out.println(suns[1]); }}Copy the code
There are four basic features of arrays
- Its length is fixed, and once the array is created, its size cannot be changed
- The elements must be of the same type, with no mixed types allowed
- The elements in an array can be any data type, including primitive and reference types
- Array variables are reference types. Arrays can also be thought of as objects. Each element in an array corresponds to a member variable of that object. An array itself is an object, and in Java an object is in the heap, so whether an array holds a primitive type or any other object type, an array object itself is in the heap
Array bounds
- The legal interval of the subscript is [0,length-1]. If the subscript is out of bounds, an error will be reported.
public static void main(String[] args){
int[] a=new int[2];
System.out.println(a[2]);
}
Copy the code
- ArrayIndexOutOfBoundsException: array subscript cross-border abnormal!
- Summary:
- An array is an ordered collection of the same data type (which can be any type)
- Arrays are also objects. array elements are equivalent to object member variables
- The array length is fixed and immutable. If monthly ends, return :ArrayIndexOutOfBounds
Using arrays
- Normal For loop
- For-each loop
- Array as an input parameter to a method
- Array as the return value
package com.Ji.array;
public class ArrayDemo03 {
public static void main(String[] args) {
int[] arrays = {1.2.3.4.5};
// Prints all array elements
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
}
System.out.println("= = = = = = = = = = = = = = = = = = =");
// Compute the sum of all elements
int sum = 0;
for (int i = 0; i < arrays.length; i++) {
sum += arrays[i];
}
System.out.println("= = = = = = = = = = = = = = = = = = = =");
// Find the maximum value
int max =arrays[0];
for (int i = 0; i < arrays.length ; i++) {
if (arrays[i]>max){
max =arrays[i];
}
}
System.out.println("max="+max); }}Copy the code
package com.Ji.array;
public class ArrayDemo04 {
public static void main(String[] args) {
int[] arrays ={1.2.3.4.5};
// JDK1.5, no subscript
//for (int array : arrays) {
// System.out.println(array);
/ /}
//printArray(arrays);
int[] reverse = reverse(arrays);
printArray(reverse);
}
// Invert the array
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
// Reverse the operation
for (int i = 0,j=result.length-1; i < arrays.length; i++,j--) {
//result[] = arrays[i]
result[j]=arrays[i];
}
return result;
}
// Prints an array element
public static void printArray(int[] arrays) {
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]+""); }}}Copy the code
Multidimensional array
- A multidimensional array can be thought of as an array of arrays. For example, a two-dimensional array is a special one-dimensional array where each element is a one-dimensional array
- 2 d array
int a[][] =new int[2] [5];
Copy the code
The above two-dimensional array a can be thought of as a two-row, five-column array \
package com.Ji.array;
public class ArrayDemo05 {
public static void main(String[] args) {
/* 1,2 array[0] 2,3 array[1] 3,4 array[2] 4,5 array[3] */
int [][] array ={{1.2}, {2.3}, {3.4}, {4.5}};
for (int i = 0; i <array.length ; i++) {
for (int j = 0; j <array[i].length ; j++) { System.out.println(array[i][j]); }}}// Prints an array element
public static void printArray(int[] arrays) {
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]+""); }}}Copy the code
The Arrays class
- Arrays utility classes java.util
- There’s no method for intentional array objects, but there’s a utility class called Arrays in the API that you can use to do some basic manipulation with data objects, okay
- Check the JDK help documentation
- All the methods in the Arrays class are static modified static methods that can be called using class names and not using objects.
- It has the following common functions:
- To assign an array: use the fill method
- Sort arrays: By sort method, in ascending order
- Compare arrays: Compares the values of the elements in an array using equals
- Find array elements: binarySearch can be used to find sorted arrays
package com.Ji.array;
import java.util.Arrays;
public class ArrayDemo06 {
public static void main(String[] args) {
int[] a ={1.2.3.3.4231.12.32.45.12};
// Prints the arrays.tostring element
//System.out.println(Arrays.toString(a));
Arrays.sort(a);// Sort the array in ascending order
System.out.println(Arrays.toString(a));
Arrays.fill(a,2.4.0);// Array paddingSystem.out.println(Arrays.toString(a)); }}Copy the code
Bubble sort
- Bubble sort is undoubtedly one of the most famous sorting algorithms, there are eight kinds!
- Bubble code is still quite simple, two layers of circulation, the number of outer bubble rounds, a comparison of the inner layer
- If you look at the nested loop, you should immediately know that the time of this algorithm is O(n2).
package com.Ji.array.Array;
import java.util.Arrays;
public class Demo07 {
public static void main(String[] args) {
int[] a = {1.4.5.6.76.2.2.2.34.6.7};int[] sort = sort(a);// After calling our own sorting method, return a sorted array
System.out.println(Arrays.toString(sort));
}
1. Compare two adjacent elements in an array, if the first element is larger than the second, we swap their position 2. Each comparison produces a maximum or minimum number 3. The next round can be sorted 4 less. Loop until the end */
public static int[] sort(int[] array) {
// Temporary variables
int temp = 0;
// Outer loop, determine how many times we need to do this
for (int i = 0; i <array.length-1 ; i++) {
If the first number is larger than the second, switch places
for (int j = 0; j < array.length-1-i; j++) {
if (array[j+1]>array[j]){
temp = array[j];
array[j] =array[j+1];
array[j+1] = temp; }}}returnarray; }}Copy the code