第二章数组(无泛型)

package com.company;

public class Main {

    public static void main(String[] args) {
	// write your code here
        Array arr=new Array(20);
        for (int i=0;i<10;i++){
            arr.addLast(i);
        }
        System.out.println(arr);
    }
}




/**
 * @author admin
 * @version 1.0.0
 * @ClassName Array.java
 * @Description TODO
 * @createTime 2021年08月21日 08:11:00
 * 数组的代码实现
 */
public class Array {
    //private私有,封装性好,避免用户直接修改。
    private int[] data;
    private int size;
    //构造器,用传入的容量capacity构造Array。
    public Array(int capacity){
        data=new int[capacity];
        size=0;
    }
    //无参数构造器,默认容量capacity=10.
    public Array(){
        this(10);
    }
    //获取数组中元素个数
    public int getSize(){
        return size;
    }
    //获取数组容量
    public int getCapacity(){
        return data.length;
    }
    //数组是否为空。
    public boolean isEmpty(){
        return size==0;
    }
    //向所有元素后添加一个新元素
    public void addLast(int element){
        if (size==data.length)
            throw new IllegalArgumentException("添加失败,数组满了");
        data[size]=element;
        size++;
        //即可以调用下面的方法,add(size,e);
    }
    //在index位置插入一个新元素e
    public void add(int index,int e){
        if (size==data.length)
            throw new IllegalArgumentException("添加失败,数组满了");
        if (index<0||index>size){
            throw new IllegalArgumentException("添加失败,index必须在0到size之间");
            }
        for (int i=size-1;i>index;i--){
            data[++i]=data[i];
        }
        data[index]=e;
        size++;
    }
    //获取index索引位置处的元素。
    int get(int index){
        if (index<0||index>=size)
            throw new IllegalArgumentException("超了");
        return data[index];
    }
    //修改指定位置的元素。
    void set(int index,int e){
        if (index<0||index>=size)
            throw new IllegalArgumentException("超了");
        data[index]=e;
    }

    //查找数组中是否有指定元素
    public boolean contains(int e){
        for (int i=0;i<size;i++){
            if (data[i]==e)
                return true;
        }
        return false;
    }
    //返回数组中指定元素的索引
    public int find(int e){
        for (int i=0;i<size;i++){
            if (data[i]==e){
                return i;
            }
        }
        return -1;
    }
    //删除指定索引处的元素
    public int remove(int index){
        if (index<0||index>=size)
            throw new IllegalArgumentException("超了");
        int t=data[index];
        for (int i=index+1;i<size;i++){
            data[i-1]=data[i];
        }
        size--;
        return t;
    }
    @Override//重写toString方法。
   public String toString(){
        StringBuffer res=new StringBuffer();
        res.append(String.format("Array:size=%d,capacity=%d\n",size,data.length));
        res.append('[');
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i!=size-1)
                res.append(", ");
        }
        res.append(']');
        return res.toString();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值