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 convert sparse array to two-dimensional array with Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "Java how to achieve sparse array and two-dimensional array conversion", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Java how to achieve sparse array and two-dimensional array conversion" bar!

Cut the crap and go straight to the code.

Package com.malajava.sparsearray;public class SparseArray {public static void main (String [] args) {/ / create an 11 / 11 2D array int array [] [] = new int [11] [11]; array [1] [2] = 1; array [2] [3] = 2 System.out.println ("- 2D array -"); for (int [] row:array) {for (int data: row) {System.out.printf ("% d\ t", data);} System.out.println () } / / the two-dimensional array changes to a sparse array / / first traverses the two-dimensional array to get the number of non-zero data int sum=0; for (int [] row:array) {for (int data: row) {if (data! = 0) sum++ }} System.out.println ("- -"); System.out.println ("non-zero number is:" + sum); int sparseArray [] [] = new int [sum+1] [3]; sparseArray [0] [0] = 11; sparseArray [0] [1] = 11 SparseArray [0] [2] = sum; / / traversing a two-dimensional array stores non-zero numbers in a sparse array int count=0; / / used to record the number of non-zero data for

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

Internet Technology

Wechat

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

12
Report