数据结构---数组(java)

数组的基本功能:

1 、数组基础
<1> 用来存储一组类型相同的数据
<2> 在内存中,分配连续的空间,数组创建时要指定容量(大小)
<3> 数据类型[] 数组名 int[] arr = new int[10] int[] arr2 = {1,2,3,4}
<4> 索引---访问数组时通过索引进行操作
<5> 索引从0开始,最大为 arr.length -1
<6> 常见的错误: NullPointException ArrayIndexOutOfBoundsException
<7> 常见的数组: 字符串, 对象数组,哈希表
2 、演示数组的使用
3 、使用数组时,最重要的就是数组的 索引 ,通过索引可以对数组进行改和查操作。

1.数组的组成:

这里使用了泛型,数组可以是任何类型
 private int size;//当前元素个数
    private int capacity;//容量
    private T[]data;//数组保存数据

2.数组的构造方法:

 public MyArray(int capacity) {
        this.capacity = capacity;
        this.size=0;
        this.data=(T[])(new Object[this.capacity]);
    }

3.获取数组中元素个数

 //获取数组元素个数
    public int getSize(){
        return this.size;
    }

4.数组的容积:

 //获取数组容积
    public int getCapacity(){
        return this.capacity;
    }

5.判断数组是否为空:

 //判断数组是否为空
    public boolean isEmity(){
        return this.size==0;
    }

6.数组容积的变化:

 private void resize(int newcapacity){
        T[]newdata=(T[])(new Object[newcapacity]);
        for(int i=0;i<this.size;i++){
            newdata[i]=this.data[i];
        }
        this.capacity=newcapacity;
        this.data=newdata;
    }

7.像数组里添加元素:并扩容:

 public void add(T item,int index){
        if(index<0||index>this.size){
            throw new IllegalArgumentException("index is invalid");
        }
        //this.size表示待插入元素的位置
        if(this.size==this.capacity){
            resize(this.capacity*2);
        }
        for(int i=size-1;i>=index;i--){
            this.data[i+1]=this.data[i];
        }
        this.data[index]=item;
        this.size++;

    }

8.修改指定位置的值:

//修改指定位置值
    public void modify(int index,T value){
        if(index<0||index>this.size){
            throw new IllegalArgumentException("index is invalid");
        }
        this.data[index]=value;
    }

9.获取索引位置元素:

  //获取指定索引位置元素
    public T getValue(int index){
        if(index<0||index>this.size){
            throw new IllegalArgumentException("index is invalid");
        }
        return this.data[index];
    }

10.查询指定位置的值在数组中是否存在并返回索引

  //查询指定位置的值在数组中是否存在,返回索引
    public int containsValue(T val){
        for(int i=0;i<this.size;i++){
            if(val.equals(this.data[i])){
                return i;
            }
        }
        return -1;
    }

11.删除索引位置数组元素:以及数组的缩容

缩容是/4来判断,/2来缩容

是为了防止数组的震荡:复杂度的震荡 ,当我们同时进行addLastremoveLast的操作

 //根据索引删除数组元素
    public void removeValue(int index){
        if (index < 0 || index > this.size) {
            throw new IllegalArgumentException("index is invalid");
        }
        for (int i = index + 1; i < this.size; i++) {
            this.data[i - 1] = this.data[i];
        }
        if(this.size<this.capacity/4&&this.capacity/2>0){
            resize((this.capacity)/2);
        }

        this.size--;

    }

12.tostring方法重写:

 @Override
    public String toString() {
       StringBuilder sb=new StringBuilder("[");
       for(int i=0;i<this.size;i++){
           sb.append(this.data[i]);
           if(i!=this.size-1){
               sb.append(",");
           }
       }
       sb.append("]");
       return sb.toString();
    }

13.主函数及部分方法应用:

 public static void main(String[] args) {
        MyArray<Integer> myArray=new MyArray<>(3);
        Random random=new Random();
        myArray.add(10,0);
        System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
        for(int i=0;i<3;i++){
            myArray.add(random.nextInt(20),myArray.size);
        }
        myArray.add(40,myArray.size);
        System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
        System.out.println(myArray.toString()+myArray.capacity);
        while(!myArray.isEmity()){
            myArray.removeValue(0);
            System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
        }
    }

完整代码

package com.ffyc.learn;

import java.util.Arrays;
import java.util.Random;

public class MyArray<T> {
    private int size;//当前元素个数
    private int capacity;//容量
    private T[]data;//数组保存数据

    //构造方法
    public MyArray(int capacity) {
        this.capacity = capacity;
        this.size=0;
        this.data=(T[])(new Object[this.capacity]);
    }
    //获取数组元素个数
    public int getSize(){
        return this.size;
    }
    //获取数组容积
    public int getCapacity(){
        return this.capacity;
    }
    //判断数组是否为空
    public boolean isEmity(){
        return this.size==0;
    }
    private void resize(int newcapacity){
        T[]newdata=(T[])(new Object[newcapacity]);
        for(int i=0;i<this.size;i++){
            newdata[i]=this.data[i];
        }
        this.capacity=newcapacity;
        this.data=newdata;
    }
    //数组添加元素
    public void add(T item,int index){
        if(index<0||index>this.size){
            throw new IllegalArgumentException("index is invalid");
        }
        //this.size表示待插入元素的位置
        if(this.size==this.capacity){
            resize(this.capacity*2);
        }
        for(int i=size-1;i>=index;i--){
            this.data[i+1]=this.data[i];
        }
        this.data[index]=item;
        this.size++;

    }
    //修改指定位置值
    public void modify(int index,T value){
        if(index<0||index>this.size){
            throw new IllegalArgumentException("index is invalid");
        }
        this.data[index]=value;
    }
    //获取指定索引位置元素
    public T getValue(int index){
        if(index<0||index>this.size){
            throw new IllegalArgumentException("index is invalid");
        }
        return this.data[index];
    }
    //查询指定位置的值在数组中是否存在,返回索引
    public int containsValue(T val){
        for(int i=0;i<this.size;i++){
            if(val.equals(this.data[i])){
                return i;
            }
        }
        return -1;
    }
    //根据索引删除数组元素
    public void removeValue(int index){
        if (index < 0 || index > this.size) {
            throw new IllegalArgumentException("index is invalid");
        }
        for (int i = index + 1; i < this.size; i++) {
            this.data[i - 1] = this.data[i];
        }
        if(this.size<this.capacity/4&&this.capacity/2>0){
            resize((this.capacity)/2);
        }

        this.size--;

    }

    @Override
    public String toString() {
       StringBuilder sb=new StringBuilder("[");
       for(int i=0;i<this.size;i++){
           sb.append(this.data[i]);
           if(i!=this.size-1){
               sb.append(",");
           }
       }
       sb.append("]");
       return sb.toString();
    }

    public static void main(String[] args) {
        MyArray<Integer> myArray=new MyArray<>(3);
        Random random=new Random();
        myArray.add(10,0);
        System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
        for(int i=0;i<3;i++){
            myArray.add(random.nextInt(20),myArray.size);
        }
        myArray.add(40,myArray.size);
        System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
        System.out.println(myArray.toString()+myArray.capacity);
        while(!myArray.isEmity()){
            myArray.removeValue(0);
            System.out.println("容积:"+myArray.capacity+"元素个数:"+myArray.size);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值