当前位置:网站首页>Print sparse arrays and restore
Print sparse arrays and restore
2022-06-11 06:00:00 【visionkiwi】
Sparse array
public static void main(String[] args) {
int[][] arr = new int[11][11];
arr[1][2] = 1;
arr[2][3] = 1;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
// Get valid number
int sum = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i][j]!=0){
sum++;
}
}
}
System.out.println(" Valid number is :"+sum);
// Create a sparse array
int[][] arr01 = new int[sum+1][3];
arr01[0][0] = 11;
arr01[0][1] = 11;
arr01[0][2] = sum;
// Traversing a two-dimensional array , Store non-zero values in a sparse array
int count = 0;
for (int i = 0; i < arr.length; i++) {
// Traverse 2D outer layer
for (int j = 0; j < arr[i].length; j++) {
// Traverse the two-dimensional inner layer
if (arr[i][j]!=0){
count++;
arr01[count][0] = i;
arr01[count][1] = j;
arr01[count][2] = arr[i][j];
}
}
}
// Output sparse array
for (int i = 0; i < arr01.length; i++) {
System.out.println(arr01[i][0]+"\t"+arr01[i][1]+"\t"+arr01[1][2]+"\t");
}
// Sparse array restore
System.out.println(" Restore ");
// Read sparse arrays
int[][] array3 = new int[arr01[0][0]][arr01[0][1]];
// Restore the value of the element
for (int i = 1; i < arr01.length; i++) {
array3[arr01[i][0]][arr01[i][1]] = arr01[i][2];
}
// Print
System.out.println(" Output restored array ");
System.out.println("=================================================================");
for (int[] ints : arr) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
}
边栏推荐
- Linux Installation redis
- Goodbye 2021 Hello 2022
- Getting started with kotlin
- Control your phone with genymotion scratch
- Super details to teach you how to use Jenkins to realize automatic jar package deployment
- 使用Batch管理VHD
- Xposed bypasses 360 reinforcement to get a real classloader
- 修复【无 Internet, 安全】问题
- Using Internet of things technology to accelerate digital transformation
- 使用Batch设置IP地址
猜你喜欢

YOLOv5的Tricks | 【Trick8】图片采样策略——按数据集各类别权重采样

What happened to the young man who loved to write code -- approaching the "Yao Guang young man" of Huawei cloud

那个酷爱写代码的少年后来怎么样了——走近华为云“瑶光少年”
![Yoyov5's tricks | [trick8] image sampling strategy -- Sampling by the weight of each category of the dataset](/img/54/f6a3e0ef1f77901506642784e6d3b7.png)
Yoyov5's tricks | [trick8] image sampling strategy -- Sampling by the weight of each category of the dataset

Which company is better in JIRA organizational structure management?

Using Internet of things technology to accelerate digital transformation

20多种云协作功能,3分钟聊透企业的数据安全经

JIRA software annual summary: release of 12 important functions

Multithreading tutorial (XXVII) CPU cache and pseudo sharing
![[元数据]LinkedIn-DataHub](/img/6a/247ee77dfc219d26651e5d93b04c98.png)
[元数据]LinkedIn-DataHub
随机推荐
使用Batch设置IP地址
How to use the markdown editor
修复Yum依赖冲突
Multi thread tutorial (30) meta sharing mode
Distributed framework ray - detailed introduction to starting ray and connecting clusters
PgSQL reports an error: current transaction is aborted, commands ignored until end of transaction block
NDK learning notes (IV) functions, classes and exceptions of swig
Compliance management 101: processes, planning and challenges
Do we really need conference headphones?
Managing VHDS using batch
Further efficient identification of memory leakage based on memory optimization tool leakcanary and bytecode instrumentation technology
那个酷爱写代码的少年后来怎么样了——走近华为云“瑶光少年”
Activity start process record
Completabilefuture asynchronous task choreography usage and explanation
Fix Yum dependency conflict
[must see for game development] 3-step configuration p4ignore + wonderful Q & A analysis (reprinted from user articles)
Do you know the functions of getbit and setbit in redis?
What do you need to know about Amazon evaluation?
Experimental report on information management and information system [information security and confidentiality] of Huazhong Agricultural University
Multithreading tutorial (XXVIII) unsafe class