Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert a 2D array to a sparse array by java

Shulou Source: shulou.com Published: 2022-05-31 17:49:23 09月13日 Update

This article mainly explains how java converts a two-dimensional array into a sparse array. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how java converts two-dimensional arrays into sparse arrays.

Characteristics

1. It can compress data and reduce the use of memory space.

Process

2. Record the coordinates and values of the array elements.

3. The sparse array has three columns, which are rows, columns and values, and the number of rows is the number of different values of the original array plus 1.

Array [0] records a set of rows and columns, as well as the number of different values

Each row then records a value marked under the row and column of the original array and its own value.

Example

Public static void main (String [] args) {/ / create an original 2D array / / 0: no chess pieces, 1: sunspot, 2: Baizi int chessArr1 [] [] = new int [11] [11]; / / first fix the elements of the 2D array, and then optimize ~ chessArr1 [1] [2] = 1; chessArr1 [2] [3] = 2; chessArr1 [4] [5] = 2 / / output the original 2D array: System.out.println ("original 2D array:"); printArray (chessArr1); / / convert the 2D array to a sparse array / / 1. First, we iterate through the two-dimensional array and get the number of non-zero data int sum = 0; for (int I = 0; I < 11; iTunes +) {for (int j = 0; j < 11; jacks +) {if (chessArr1 [I] [j]! = 0) {sum++;}} / / 2. Create a corresponding sparse array int sparesArr [] [] = new int [sum + 1] [3]; / / assign a value to the sparse array sparesArr [0] [0] = 11; sparesArr [0] [1] = 11; sparesArr [0] [2] = sum; / / ergodic two-dimensional array, and store non-zero values in sparesArr int count = 0 for (int I = 0) I < 11; iSum +) {for (int j = 0; j < 11; jaun +) {if (chessArr1 [I] [j]! = 0) {count++; sparesArr [count] [0] = I; sparesArr [count] [1] = j; sparesArr [count] [2] = chessArr1 [I] [j] } / / output sparse array form System.out.println (); System.out.println ("the resulting sparse array is:"); printArray (sparesArr); System.out.println () / / restore the sparse array to a two-dimensional array / / read the first row of elements of the sparse array first, and according to its data, create the original two-dimensional array int chessArr2 [] [] = new int [sparesArr [0] [0]] [sparesArr [0] [1]]; / / read the elements of the next few rows of the sparse array (starting from the second row) and assign values to the original two-dimensional array to for (int I = 1) I < sparesArr.length; Array +) {chessArr2 [sparesArr [I] [0]] [sparesArr [I] [1]] = sparesArr [I] [2];} / output the restored 2D array System.out.println (); System.out.println ("restored two-dimensional array"); printArray (chessArr2);} / / print array public static void printArray (int [] [] array) {for (int I = 0) I < array.length; iTunes +) {for (int j = 0; j < array [0] .length; jacks +) {System.out.printf ("% d\ t", array [I] [j]);} System.out.println ();}} so far, I believe you have a better understanding of "how java converts a two-dimensional array into a sparse array", so you might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Array two-dimensional sparse raw element data jackers output different content learning practical deeper row subscript number interest memory coordinates instance Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info Microsoft Shulou Information MySQL