随笔- 100  文章- 0  评论- 3  阅读- 30413 

  数组(Array)是一种线性表数据结构。它用一组连续的内存空间,来存储一组具有相同类型的数据。

 

一:有趣的开头

1.题目

  一个全国的年龄数据,统计每一个年龄有多少人·

 

2.分析

  我们可以进行一行一行的读取数据,年龄对应数组的下标,相同的年龄是相同的下标,可以针对这个下标里面的数据进行不断的累加。

  当数据读取完成,则数组已经统计好每个年龄的总数,即是某个年龄对应数组下标的数据。

 

  可以发现,通过简单的数组,可以解决复杂的问题,而不是考虑分布式问题。

 

3.代码

复制代码
package com.jun.algorithm.foundation.main;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * 一个文件中判断全国人民的年龄数据,每个年龄的人数
 * 主要是处理量很大的时候,只需要使用简单的数组就可以实现了
 * <p>
 * ps:
 * 为什么数组下标从0开始?
 * 连续的内存空间与相同的数据类型,才有一个重要的特性,随机访问
 * 数组可以使用cpu的缓存机制,预读数组中的数据,所以效率更高一些
 *
 * @author caojun
 */

public class CountAgeNumber {
    public static void main(String[] args) throws Exception {
        function();
    }

    /**
     * 具体的方法
     */
    private static void function() {
        // 将年龄累加值存放在数据中
        int[] data = new int[200];

        int total = 0;
        String str = "";
        String fileName = "E:\\mygitcode\\ageNumber.txt";
        InputStreamReader inputStreamReader = null;
        try {
            inputStreamReader = new InputStreamReader(new FileInputStream(fileName), StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            while ((str = bufferedReader.readLine()) != null) {
                int age = Integer.parseInt(str);
                data[age]++;
                total++;
            }
            System.out.println("数据总个数是" + total);
            for (int i = 0; i < 200; i++) {
                System.out.println(i + "岁的有" + data[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(inputStreamReader)) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
复制代码

 

 

二:数组的理解

1.随机访问

  数组是连续的内存空间与相同类型的数据结构,因为这两个限制,才可以形成随机访问。

  重要的应用就是查找。

  可以通过下标直接定位到某一个数据,o(1)。

 

2.缺点

  插入与删除

 

3.注意点

  数组的越界

 

4.数组为啥下标从0开始

  数组的特点决定,可以计算内存地址更加简单,节省运算

 

5.增删改查的代码实现

复制代码
package com.jun.algorithm.foundation.main;

/**
 * 数组的实现
 * 类似ArrayList
 */
public class ArrayImpl {

    private int size;
    private int[] data;
    // 已存的数据大小
    private int index;

    public ArrayImpl(int size) {
        this.size = size;
        data = new int[size];
        index = 0;
    }

    public void print() {
        System.out.print("index=" + index);
        for (int i = 0; i < index; i++) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }

    public void insert(int loc, int n) {
        if (index++ < size) {
            for (int i = size - 1; i > loc; i--) {
                data[i] = data[i - 1];
            }
            data[loc] = n;
        } else {
            // 扩容
        }
    }


    public void delete(int loc) {
        for (int i = loc; i < size; i++) {
            if (i != size - 1) {
                data[i] = data[i + 1];
            } else {
                data[i] = 0;
            }
        }
        index--;
    }

    public void update(int loc, int n) {
        data[loc] = n;
    }

    public int get(int loc) {
        return data[loc];
    }

}
复制代码

 

 posted on   曹军  阅读(52)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示