How to convert sparse array and two-dimensional array in Java?
Apr 21, 2023 am 10:22 AM
java
Stop talking nonsense and go directly to the code
package com.malajava.sparsearray; public class SparseArray { public static void main(String[] args) { //创建一个11*11二维数组 int array[][] = new int[11][11]; array[1][2]=1; array[2][3]=2; System.out.println("------------------二维数组------------------"); for (int[]row:array){ for (int data: row){ System.out.printf("%d\t",data); } System.out.println(); } //二维数组转稀疏数组 //先遍历二维数组,得到非零数据的个数 int sum=0; for (int[]row:array){ for (int data: row){ if(data != 0) sum++; } } System.out.println("----------------------------------------"); System.out.println("非零个数为:"+sum); int sparseArray[][] = new int[sum+1][3]; sparseArray[0][0] =11; sparseArray[0][1]=11; sparseArray[0][2]=sum; //遍历二维数组将非零数字存入稀疏数组 int count=0; //用于记录是第几个非零数据 for (int i=0;i<p> Running results: <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/465/014/168204373194330.jpg" class="lazy" alt="How to convert sparse array and two-dimensional array in Java?"></p>
Copy after login
The above is the detailed content of How to convert sparse array and two-dimensional array in Java?. For more information, please follow other related articles on the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Break or return from Java 8 stream forEach?
