大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
int[] temp = new int[10];
arrays.length
数组内存分析数组内存分为
int[] temp1 = {1, 2, 3};
int[] temp2 = new int[3];
temp2[0] = 1;
temp2[1] = 2;
temp2[2] = 3;
一些常用功能:
System.out.println(Arrays.toString(temp1));
Arrays.sort(temp1);
Arrays.fill(temp1, 0); // 全部填充
Arrays.fill(temp1, 2, 4, 0); // 部分填充, 区间为[2, 4)
temp1.equals(temp2)
System.out.println(Arrays.binarySearch(temp1, 2));
稀疏数组package com.cen.array;
public class Array_lessSct {public static void main(String[] args) {// 原始数组
int[][] temp1 = new int[10][10];
temp1[1][1] = 1;
temp1[2][2] = 2;
// 打印原始数组
System.out.println("原始数组");
for (int i = 0; i< 10; i++) {for (int j = 0; j< 10; j++) {System.out.print(temp1[i][j] + " ");
}
System.out.println();
}
// 稀疏数组
int n = temp1.length;
int m = temp1[0].length;
int[][] temp2 = new int[n*m+1][3];
temp2[0][0] = n;
temp2[0][1] = m;
for (int i = 0; i< n; i++) {for (int j = 0; j< m; j++) {if(temp1[i][j]!=0){int now_cnt = ++temp2[0][2];
temp2[now_cnt][0] = i;
temp2[now_cnt][1] = j;
temp2[now_cnt][2] = temp1[i][j];
}
}
}
// 打印稀疏数组
System.out.println("稀疏数组");
int cnt = temp2[0][2];
for(int i=0;i<=cnt;i++){System.out.printf("%d %d %d\n", temp2[i][0], temp2[i][1], temp2[i][2]);
}
// 还原原始数组
int[][] temp3 = new int[10][10];
for(int i=1;i<=cnt;i++){temp3[temp2[i][0]][temp2[i][1]] = temp2[i][2];
}
// 打印还原结果
System.out.println("还原数组");
for (int i = 0; i< 10; i++) {for (int j = 0; j< 10; j++) {System.out.print(temp3[i][j] + " ");
}
System.out.println();
}
}
}
输出:
原始数组
0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
稀疏数组
10 10 2
1 1 1
2 2 2
还原数组
0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
参考遇见狂神说. 【狂神说Java】Java零基础学习视频通俗易懂
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧