In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.