资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java笔记-创新互联

java笔记_数组
  • 数组声明创建
  • 数组内存分析
  • 数组的三种初始化
  • Arrays类
  • 稀疏数组
  • 参考

创新互联是一家集网站设计制作、网站设计、网站页面设计、网站优化SEO优化为一体的专业网站设计公司,已为成都等多地近百家企业提供网站建设服务。追求良好的浏览体验,以探求精品塑造与理念升华,设计最适合用户的网站页面。 合作只是第一步,服务才是根本,我们始终坚持讲诚信,负责任的原则,为您进行细心、贴心、认真的服务,与众多客户在蓬勃发展的市场环境中,互促共生。数组声明创建
  • 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零基础学习视频通俗易懂

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站题目:java笔记-创新互联
网站地址:http://cdkjz.cn/article/goisd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220