java笔记_数组

数组声明创建

  • Java语言使用new操作符来创建数组
int[] temp = new int[10];
  • 获取数组长度
arrays.length

数组内存分析

数组内存分为

    • 存放new的对象和数组
    • 存放基本变量类型(会包含这个基本类型的具体数值)
    • 引用对象的变量(会存放这个引用在堆里面的具体地址)

数组内存分析

数组的三种初始化

  1. 静态初始化
int[] temp1 = {1, 2, 3};
  1. 动态初始化
int[] temp2 = new int[3];
temp2[0] = 1;
temp2[1] = 2;
temp2[2] = 3;
  1. 默认初始化

Arrays类

一些常用功能:

  • 打印数组元素
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零基础学习视频通俗易懂

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y_cen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值