数组及内存分析
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EmsN1iIn-1628604149945)(C:\Users\ZZ09J1672\AppData\Roaming\Typora\typora-user-images\image-20210810205813775.png)]](https://i-blog.csdnimg.cn/blog_migrate/aae3033b228fcf8550c9b24cc647cd10.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x8c3pe2F-1628604149949)(C:\Users\ZZ09J1672\AppData\Roaming\Typora\typora-user-images\image-20210810210002262.png)]](https://i-blog.csdnimg.cn/blog_migrate/1c8b85a57d83e1ab2777f07c6a1ef49e.png)
public class Array {
public static void main(String[] args) {
int[] nums;
nums = new int[5];
nums[0] = 1;
nums[1] = 2;
nums[2] = 3;
nums[3] = 4;
nums[4] = 5;
int[] nums2 = new int[5];
int[] nums3 = new int[]{1,2,3,4,5};
int[] nums4 = {1,2,3,4,5};
int sum = 0;
for(int i=0;i<nums.length;i++){
sum = sum + nums[i];
}
System.out.println("总和为:"+sum);
for(int num : nums){
System.out.println(num);
}
}
}
数组的使用
public class ArrayUse {
public static void main(String[] args) {
int[] nums = {1,2,3,4,5};
for(int num : nums){
System.out.println(num);
}
int[] reverse = reverse(nums);
printArray(nums);
int[][] arrays = {{1,2},{2,3},{3,4},{4,5}};
printTWOArray(arrays);
}
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
for(int i=0,j=arrays.length-1;i<arrays.length-1;i++,j--){
result[j] = arrays[i];
}
return result;
}
public static void printArray(int[] arrays){
for (int i=0;i<arrays.length;i++){
System.out.print(arrays[i]);
}
}
public static void printTWOArray(int[][] arrays){
for (int i=0;i<arrays.length;i++){
for(int j=0;j<arrays[i].length;j++){
System.out.print(arrays[i][j]);
}
}
}
}
数组的工具类java.util.Arrays类
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-38gqdcKt-1629094785809)(C:\Users\ZZ09J1672\AppData\Roaming\Typora\typora-user-images\image-20210812173207114.png)]](https://i-blog.csdnimg.cn/blog_migrate/44e10af64a4afe06a18c4b5cb0e046df.png)
稀疏数组
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5183d656b7b388ca90b9b64c2f6b7d0f.png)
public class SparseArray {
public static void main(String[] args) {
int[][] array1 = new int[11][11];
array1[1][2] = 1;
array1[2][3] = 2;
for (int[] ints : array1){
for (int inta : ints ){
System.out.print(inta+"\t");
}
System.out.println();
}
int sum = 0;
for(int i=0;i<11;i++){
for(int j=0;j<11;j++){
if(array1[i][j]!=0) sum++;
}
}
System.out.println("有效值的个数:"+sum);
int[][] array2 = new int[sum+1][3];
array2[0][0] = 11;
array2[0][1] = 11;
array2[0][2] = sum;
int count = 0;
for(int i=0;i<array1.length;i++){
for(int j=0;j<array1.length;j++){
if(array1[i][j]!=0){
count++;
array2[count][0] = i;
array2[count][1] = j;
array2[count][2] = array1[i][j];
}
}
}
System.out.println("稀疏数组");
for (int[] ints : array2) {
System.out.println(ints[0] + "\t" + ints[1] + "\t" + ints[2] + "\t");
}
System.out.println("==============");
System.out.println("还原");
int[][] array3 = new int[array2[0][0]][array2[0][1]];
for(int i=1;i<array2.length;i++){
array3[array2[i][0]][array2[i][1]] = array2[i][2];
}
System.out.println("输出还原的数组");
for (int[] ints : array3){
for (int inta : ints ){
System.out.print(inta+"\t");
}
System.out.println();
}
}
}