directory
- Learning materials
- Definition of an array
- Memory analysis
- Using arrays
- Multidimensional array
- The Arrays class
- Bubble sort
- The sparse array
Learning materials
B station crazy god said: www.bilibili.com/video/BV12J…
Definition of an array
An array is an ordered collection of the same type.
Array elements are accessed by subscripts, starting at 0.
Arrays are also objects, and array elements act as member variables of objects
The length of an array is immutable if an exception occurs: ArrayIndexOutofBounds
package com.zy7y.array;
import java.util.Arrays;
/ * * *@ProjectName: JavaSE
* @PackageName: com.zy7y.array
* @Author: zy7y
* @Date: 2020/8/14 下午8:27
* @Description: * / an array
public class ArrayStudy {
public static void main(String[] args) {
// Define an array of type int; Keep NUMs on the stack
int[] nums;
nums = new int[10]; // 10 is the length of the array; Put 10 arrays of int types in the heap
nums[0] = 1; // Assign to the array
// arrays. toString(array), prints the contents of the array
System.out.println(Arrays.toString(nums)); // [1, 0, 0, 0, 0, 0, 0, 0, 0]
int sum = 0;
// Get the length of the array
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
System.out.println(sum);
// Static initialization: create + assign
int[] a = {1.2.3.4.5.6};
System.out.println(a[0]);
// Dynamic initialization: includes default initialization
int[] b = new int[] {1.2.3.4.5};
System.out.println(b[1]); }}Copy the code
Memory analysis
The graph LR Java memory –> heap heap –> holds objects and arrays for new –> can be shared by all threads, and does not hold other object references to Java memory –> stack –> holds the basic variable type, containing the specific value of this basic type Java memory -> method area -> method area that can be shared by all threads -> contains all classes and static variables
Using arrays
package com.zy7y.array;
import java.util.Arrays;
/ * * *@ProjectName: JavaSE
* @PackageName: com.zy7y.array
* @Author: zy7y
* @Date: 2020/8/14 8:53 PM *@Description: * /
public class UseArray {
public static void main(String[] args) {
int[] arrays = {1.2.3.4.5};
// Prints all elements
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
}
for (int array: arrays) {
System.out.println(array);
}
// Compute the sum of all elements
int sum = 0;
for (int i = 0; i < arrays.length; i++) {
sum += arrays[i];
}
System.out.println(sum);
int total = 0;
for (int array:arrays) {
total += array;
}
System.out.println(total);
System.out.println(Arrays.toString(reverse(arrays)));
}
// Invert the array
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
// result.length -1 j is the maximum subscript of result and then the edge is smaller, and I is the minimum subscript of result
for (int i = 0, j = result.length - 1 ; i < arrays.length; i++, j--) {
result[j] = arrays[i];
}
returnresult; }}Copy the code
Multidimensional array
Arrays are nested within arrays
// Define a two-dimensional array and copy it
int[][] array = new int[] [] {{1.23.1}, {2.3}};
System.out.println(array[0] [1]); {1,23,1} and then the first 23 in the array
Copy the code
The Arrays class
package com.zy7y.array;
import java.util.Arrays;
/ * * *@ProjectName: JavaSE
* @PackageName: com.zy7y.array
* @Author: zy7y
* @Date: 2020/8/14 9:16 PM *@Description: the Arrays class * /
public class UseArrays {
public static void main(String[] args) {
int[] a = {1.23.445.123};
int[] b = {1};
System.out.println(Arrays.toString(a));
/ / sorting
Arrays.sort(a);
System.out.println(Arrays.toString(a));
// Compare two arrays
boolean result = Arrays.equals(a,b);
System.out.println(result);
// Array.copyof (array, copy the length of array)
int[] c = Arrays.copyOf(a, a.length);
System.out.println(Arrays.toString(c));
printArray(c);
}
// Copy an arrays.tostring
public static void printArray(int[] arrays) {
System.out.print("[");
for (int array:arrays) {
if (array == arrays[arrays.length-1]){
System.out.print(array + "]");
}else {
System.out.print(array + ","); }}}}Copy the code
Bubble sort
package com.zy7y.array;
import java.util.Arrays;
/ * * *@ProjectName: JavaSE
* @PackageName: com.zy7y.array
* @Author: zy7y
* @Date: 2020/8/14 9:29 PM *@Description: Bubble sort */
public class Bubble {
public static void main(String[] args) {
// Compare two adjacent elements in an array and swap if the first element is larger than the second
// Each comparison produces a maximum or minimum number;
// The next round can be less ordered
int[] arrays = {1.452.23.3455.7786.12.44};
bubbleSort(arrays);
}
public static void bubbleSort(int[] arrays){
// The length is 8, and the subscript is 7, but the final one does not change position
for (int i = 0; i < arrays.length - 1; i++) {
// Specify a bit
boolean off = false;
for (int j = 0; j < arrays.length - 1 - i; j++) {
if (arrays[j] > arrays[j + 1]) {
int temp = arrays[j];
arrays[j] = arrays[j + 1];
arrays[j + 1] = temp;
off = true; }}// When the swap completes, break;
if(! off){break; } } System.out.println(Arrays.toString(arrays)); }}Copy the code
The sparse array
Many of the values in the two-dimensional array are 0 by default, recording more meaningless data
package com.zy7y.array;
/ * * *@ProjectName: JavaSE
* @PackageName: com.zy7y.array
* @Author: zy7y
* @Date: 2020/8/14 9:54 PM *@Description: * /
public class SparseArray {
public static void main(String[] args) {
// Generate an 11 * 11 array
int[][] array = new int[11] [11];
array[3] [9] = 27;
array[1] [10] = 10;
array[5] [8] = 40;
array[0] [3] = 30;
// printArrays(array);
printArray(array);
System.out.println("==== compressed into a sparse array ====");
System.out.println("Row column value");
printArray(packedArray(array));
System.out.println("==== restore from sparse array ======");
printArrays(revivification(packedArray(array)));
}
// Print a two-dimensional array
public static void printArrays(int[][] array){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + "\t"); } System.out.println(); }}// The second way to print a two-dimensional array
public static void printArray(int[][] array){
for (int[] arr:array) {
for (int a:arr) {
System.out.print(a + "\t"); } System.out.println(); }}// Compress to a sparse array
public static int[][] packedArray(int[][] array){
// Count the number of valid values to get the number of lines
int count = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if(array[i][j] ! =0) {
count += 1; }}}int[][] packed = new int[count+1] [3];
// The number of lines in the original array
packed[0] [0] = 11;
// The number of columns in the original array
packed[0] [1] = 11;
// The number of valid values in the original array
packed[0] [2] = count;
int row = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if(array[i][j] ! =0) {
// Count the number of valid values as the number of rows
row += 1;
packed[row][0] = i;
packed[row][1] = j;
packed[row][2] = array[i][j]; }}}return packed;
}
// Restore to a sparse array
public static int[][] revivification(int[][] array) {
int[][] arrays = new int[array[0] [0]][array[0] [1]].for (int i = 1; i < array.length; i++) {
arrays[array[i][0]][array[i][1]] = array[i][2];
}
returnarrays; }}Copy the code