java array learning
Array overview
An ordered collection of data of the same type
Basic characteristics of array
- The length is determined. Once created, the size cannot be changed
- The element data types must be the same, and mixed types are not allowed
- Where the element data type can be any data type
- Array objects themselves are in the heap
- An array is an object
Array declaration creation
step
- Array variables must be declared before arrays can be used in programs (array declarations)
Syntax:
int[] num; //Declare array method 1, preferred int num1[]; //Declaring array method 2(C and C + + style) is not preferred
- Use the new operator to create the array size (that is, the number of variables to store) (array creation)
Syntax:
int[] num=new int[20]; //It can store 20 int variables
- Array elements are accessed by index, and the array index starts from 0 (array assignment)
Syntax:
num[0]=1; num[1]=2; num[2]=2; num[3]=2; num[4]=2;
Method to get array length:
Array name.length
Little practice
- Calculates the sum of the elements in the array
package Mothed; public class NumberTeam { public static void main(String[] args) { int[] num=new int[5]; //It can store five int variables num[0]=1; num[1]=2; num[2]=2; num[3]=2; num[4]=2; int sum=0; for (int i = 0; i < num.length; i++) { sum+=num[i]; } System.out.println("The sum of the array is:"+sum); } }
Possible errors
ArrayIndexOutOfBoundsException: / / array index out of bounds exception
The output data cannot exceed the size of the created array, for example:
int[] num=new int[5]; //Only five variables were created System.out.println(num[5]); //Error, the sixth number cannot be output
Legal range of subscript: [0, length-1]
Three initializations of arrays
- initiate static
int[] n={1,5,7,8,9,7,4,1}; //Create + assign
- dynamic initialization
int[] num=new int[2]; //establish num[0]=1; num[1]=2; //assignment
- Default initialization of arrays
Dynamic initialization includes default initialization. If it is created without assignment, it will be initialized automatically by default. Character type initialization is 0 and string (string) initialization is null
Array usage
- Normal For loop
int sum=0; int[] b={1,2,8}; for (int i = 0; i < b.length; i++) { sum+=b[i]; } System.out.println(sum);
- For each loop
int[] ns={1,5,7,8,9,7,4,1};for (int n : ns) { System.out.println(n);}
- Array as method input parameter
int[] as={1,5,7,8,9,7,4,1};an(as); public static int an(int[] as){ for (int i = 0; i < as.length; i++) { System.out.println(as[i]); } return 0; }
- Array as return value
public static void main(String[] args) { int[] a={1,5,7,8,9}; //Define the value of an array a int[] n=an(a); //Call an method, assign array a to array asy, calculate, and finally return the value of array re, and define array n with the value of array re q(n); //Call the q method with array a as the parameter list } public static int[] an(int[] asy){ int[] re=new int[asy.length]; //Define an array re so that its size is equal to the size of array asy for (int i = 0, j= re.length-1; i < asy.length; i++,j--) { //Print the contents of the array in reverse re[j]=asy[i]; } return re; //Returns the value of the array re } public static void q(int[] as){ for (int i = 0; i < as.length; i++) { //Print each value in the array System.out.println(as[i]); } }
Multidimensional array
int[][] nui={{1,5},{2,7}};
Nesting of one-dimensional arrays
Arrays class
- . toString() prints the entire array
System.out.println(Arrays.toString(n)); //Print entire array
package Mothed; import java.util.Arrays; public class Array { public static void main(String[] args) { int[] n = {1, 15154, 4848, 116, 151, 118, 1, 1515}; System.out.println(Arrays.toString(n)); } }
package Mothed; public class Array { public static void main(String[] args) { int[] n = {1, 15154, 4848, 116, 151, 118, 1, 1515}; p(n); } public static void p(int[] s) { for (int i = 0; i < s.length; i++) { if (i == 0) { System.out.print("["); } if (i == s.length-1) { System.out.print(s[i]+"]"); } else { System.out.print(s[i] + ", "); } } } }
The results of the two pieces of code are the same, except that one uses the system Arrays class and the other is written by itself
- . sort() sorts the array in ascending order
public static void main(String[] args) { int[] n = {1, 15154, 4848, 116, 151, 118, 1, 1515}; Arrays.sort(n); System.out.print(Arrays.toString(n)); }
- . fill() array fill
public static void main(String[] args) { int[] n = {1, 15154, 4848, 116, 151, 118, 1, 1515}; Arrays.fill(n,0); System.out.print(Arrays.toString(n)); }
public static void main(String[] args) { int[] n = {1, 15154, 4848, 116, 151, 118, 1, 1515}; Arrays.fill(n,2,5,0); System.out.print(Arrays.toString(n)); }
Bubble sorting
- Arrange the numbers in the array from small to large
public static void main(String[] args) { int[] a2={1,5,2,9,4}; a(a2); System.out.println(Arrays.toString(a2)); } public static int[] a(int[] as){ int b=0; //Define a variable to facilitate position exchange when comparing sizes for (int i = 0; i < as.length-1; i++) { //Outer cycle, judge the number of cycles for (int i1 = 0; i1 < as.length-1-i; i1++) { //Inner loop, compare the size of two numbers if (as[i1]>as[i1+1]){ b=as[i1]; as[i1]=as[i1+1]; as[i1+1]=b; //If the former is bigger than the latter, the two exchange positions } } }return as; }
Time complexity: O(n2)
Sparse array
Is a data structure
How to handle sparse arrays:
- How many rows and columns are there in the record array? How many different values are there
- Record the elements, rows, columns and values with different values in a small array, so as to reduce the size of the program (compression)
0 0 0 0 0
0 0 1 0 0
0 0 0 2 0
0 0 0 0 0
The upper array can be expressed as a sparse array
Row sequence value
[0] 4 5 2
[1] 1 2 1
[2] 2 3 2
The implementation code is as follows:
package Mothed; import java.util.Arrays; public class Xs { public static void main(String[] args) { int number1=4; int number2=5; //Defines the size of a two-dimensional array int[][] a = new int[number1][number2]; a[1][2] = 1; a[2][3] = 2; //Assign a value to a part of a two-dimensional array System.out.println("Output start array"); for (int[] as : a) { for (int bs : as) { System.out.print(bs + "\t"); } System.out.println(); } //Strong loop traversal of two-dimensional array int num = 0; for (int i = 0; i < a.length; i++) { for (int i1 = 0; i1 < a[i].length; i1++) { if (a[i][i1] != 0) { num++; } } } //Traverse each value in the two-dimensional array to determine the number of non-zero values System.out.println("share"+num+"Non zero number"); int[][] xx = new int[num + 1][3]; //Create sparse array xx[0][0] = number1; xx[0][1] = number2; xx[0][2] = num; //Assign a value to the first row of the sparse array int sum = 0; for (int i = 0; i < a.length; i++) { for (int i1 = 0; i1 < a[i].length; i1++) { if (a[i][i1] != 0) { sum++; xx[sum][0] = i; xx[sum][1] = i1; xx[sum][2] = a[i][i1]; //Traverse each value in the two-dimensional array, determine the number of non-zero values, and pass the value into the corresponding position of the sparse array } } } System.out.println("Reduced to a sparse array as follows:"); for (int[] zz : xx) { for (int cc : zz) { System.out.print(cc+"\t"); } System.out.println(); } //Traversal sparse array System.out.println("Restore sparse array to original array:"); int[][] near=new int[xx[0][0]][xx[0][1]]; //Create a new array for (int i = 1; i < xx.length; i++) { near[xx[i][0]][xx[i][1]]=xx[i][2]; //Determine the position of non-zero numbers in the new array and assign a value } for (int[] zz : near) { for (int cc : zz) { System.out.print(cc+"\t"); } System.out.println(); } //Traversal restore array } }
Supplement: memory analysis
Heap: objects and arrays for storing new
Stack: store the basic variable type (it will contain the specific value of this basic type)
Method area