Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use sparse Array in Java programming

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use sparse arrays in Java programming". The explanation in this article is simple and clear, easy to learn and understand. Please follow Xiaobian's train of thought to study and learn how to use sparse arrays in Java programming.

Basic introduction

When most of the elements in an array are 0 or an array of the same value, you can use a sparse array to save the array.

Sparse arrays are handled as follows:

The record array has several rows and columns, and how many different values there are.

Record the rows and rows of elements with different values in a small array to reduce the size of the program.

Give examples to illustrate

Original two-dimensional array

Original two-dimensional array

Converted two-dimensional array

The first line records how many rows and values there are in the original array (8)

Converted two-dimensional array

The idea of converting two-dimensional arrays to sparse arrays

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Traverse the original two-dimensional array to get the number of valid data sum

According to sum, you can create a sparse array sparseArr int (sum+1) (3)

Store the valid data of a two-dimensional array into a sparse array

The idea of transforming sparse array to original two-dimensional array

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

First read the first row of the sparse array and create the original two-dimensional array based on the data of the first row

Then read the last few rows of the sparse array and assign it to the original two-dimensional array.

Application example

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Use sparse arrays to retain similar two-dimensional arrays (chessboard / map), etc.

Save the sparse array to disk and restore the original two-dimensional array number

Code case

Package com.structures.sparsearray; public class SparseArray {public static void main (String [] args) {/ / create an original two-dimensional array 11x11 / / 0: no chess pieces, 1 for sunspots, 2 for white int [] [] chessArr1 = new int [11] [11]; chessArr1 [1] [2] = 1; chessArr1 [2] [3] = 2 / / output the original 2D array System.out.println ("original 2D array"); for (int [] ints: chessArr1) {for (int anInt: ints) {System.out.printf ("% d\ t", anInt);} System.out.println () } / / convert a two-dimensional array to a sparse array / / 1. First traverse the two-dimensional array to get the number of non-zero data. Int sum = 0; for (int [] ints: chessArr1) {for (int anInt: ints) {if (anInt! = 0) {sum++;} System.out.println ("sum =" + sum); / / 2. Create a corresponding sparse array int [] [] sparseArr = new int [sum + 1] [3]; / / assign a value to the sparse array sparseArr [0] [0] = 11; sparseArr [0] [1] = 11; sparseArr [0] [2] = sum; / / traverse the original array and store non-zero values in the sparse array int count = 0 / / count is used to record the number of non-zero data for (int I = 0; I < chessArr1.length; idata +) {for (count++; sparseArr j = 0; j < chessArr1 [I] .length; jlength +) {if (chessArr1 [I] [j]! = 0) {count++; sparseArr [count] [0] = I SparseArr [count] [1] = j; sparseArr [count] [2] = chessArr1 [I] [j];} / / output sparse array System.out.println (); System.out.println ("the resulting sparse array is ~ ~") For (int [] ints: sparseArr) {for (int anInt: ints) {if (anInt! = 0) {System.out.printf ("% d\ t", anInt);}} System.out.println () } / / restore the sparse array to the original array int [] [] chessArr2 = new int [sparseArr [0] [0]] [sparseArr [0] [1]]; for (int I = 0; I < sparseArr [0] [2]; iSparseArr [I + 1] [0]] [sparseArr [I + 1] [1]) = sparseArr [I + 1] [2] } / / restored original array System.out.println ("restored original array"); for (int [] ints: chessArr2) {for (int anInt: ints) {System.out.printf ("% d\ t", anInt);} System.out.println () } / * the original two-dimensional array 0 000 000 000 0 0 0 000 0 0 0 000 0 0 0 000 0 0 0 000 0 000 0 0 0 000 0 0 0 000 0 000 0 0 0 000 0 0 000 0 0 0 000 0 000 000 0 000 0 0 000 0 000 0 000 0 000 0 000 0 000 0 000 0 0 0 0 The sparse array obtained from 0 000 000 0 sum = 2 is the original array 0 000 0 000 0 0 0 0 0 0 0000000000000000 * / Thank you for your reading The above is the content of "how to use sparse arrays in Java programming". After the study of this article, I believe you have a deeper understanding of how to use sparse arrays in Java programming, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report