• 扩展:对数组进行封装,实现增删改查功能


    
    public class ArrayDemo {
    	// 存储数据的数组
    	private Object[] array;
    	// 数组中存储元素的个数
    	private int size;
    	// 数组的容量
    	private int capacity;
    
    	// 无参构造器,数组默认初始容量为10
    	public ArrayDemo() {
    		this(10);
    	}
    
    	// 有参构造器
    	public ArrayDemo(int capacity) {
    		array = new Object[capacity];
    		this.capacity = capacity;
    	}
    
    	/**
    	 * 在数组后面添加元素
    	 * 
    	 * @param data
    	 * @return
    	 */
    	public boolean add(Object data) {
    		// 判断是否要扩容
    		ensureCapacity(size + 1);
    		array[size++] = data;
    		return true;
    	}
    
    	/**
    	 * 在数组指定位置添加元素
    	 * 
    	 * @param index
    	 * @param data
    	 * @return
    	 */
    	public boolean add(int index, Object data) {
    		// 判断是否要扩容
    		ensureCapacity(size + 1);
    		// 元素移动
    		System.arraycopy(array, index, array, index + 1, size - index);
    		array[index] = data;
    		size++;
    		return true;
    	}
    
    	/**
    	 * 扩容方法
    	 * 
    	 * @param needCapacity
    	 */
    	private void ensureCapacity(int needCapacity) {
    		// 如果元素的个数大于数组的容量时,需要扩容操作
    		if (needCapacity > capacity) {
    			capacity = capacity + (capacity >> 1);
    			Object[] newArray = new Object[capacity];
    			// 数组拷贝
    			System.arraycopy(array, 0, newArray, 0, array.length);
    			array = newArray;
    		}
    	}
    
    	/**
    	 * 重写toString方法
    	 */
    	public String toString() {
    		StringBuilder bulider = new StringBuilder();
    		if (size == 0) {
    			bulider.append("[]");
    		} else {
    			bulider.append("[");
    			for (int i = 0; i < size; i++) {
    				bulider.append(array[i] + ",");
    			}
    			bulider.setCharAt(bulider.length() - 1, ']');
    		}
    		return bulider.toString();
    	}
    
    	/**
    	 * 删除数组后面的元素
    	 * 
    	 * @return
    	 */
    	public Object remove() {
    		if (size > 0) {
    			return array[--size];
    		} else {
    			return null;
    		}
    	}
    
    	/**
    	 * 删除数组指定位置的元素
    	 * 
    	 * @param index
    	 * @return
    	 */
    	public Object remove(int index) {
    		// 判断下标是否合法
    		boolean flag = checkRange(index);
    		if (flag) {
    			// 拿到这个元素
    			Object res = array[index];
    			// 元素移动
    			System.arraycopy(array, index + 1, array, index, size - index - 1);
    			size--;
    			// 返回删除的元素
    			return res;
    		}
    		return null;
    	}
    
    	/**
    	 * 判断下标是否合法
    	 * 
    	 * @param index
    	 * @return
    	 */
    	private boolean checkRange(int index) {
    		return (index >= 0 && index <= size - 1);
    	}
    
    	/**
    	 * 修改指定位置的元素
    	 * 
    	 * @param index
    	 * @param data
    	 * @return
    	 */
    	public boolean set(int index, Object data) {
    		// 判断下标是否合法
    		boolean flag = checkRange(index);
    		if (flag) {
    			array[index] = data;
    			return true;
    		}
    		return false;
    	}
    
    	/**
    	 * 获取指定位置上的元素
    	 * 
    	 * @param index
    	 * @return
    	 */
    	public Object get(int index) {
    		// 判断下标是否合法
    		boolean flag = checkRange(index);
    		if (flag) {
    			return array[index];
    		}
    		return null;
    	}
    
    	/**
    	 * 获取数组中元素的个数
    	 * 
    	 * @return
    	 */
    	public int size() {
    		return size;
    	}
    
    	/**
    	 * 
    	 * @return
    	 */
    	public boolean isEmpty() {
    		return size == 0;
    	}
    
    	public static void main(String[] args) {
    		ArrayDemo arr1 = new ArrayDemo();
    		arr1.add("111");
    		arr1.add(2);
    		arr1.add(2, 222);
    		arr1.add('a');
    		arr1.remove(1);
    		System.out.println("arr1 --> size:" + arr1.size + "\tcapacity:" + arr1.capacity);
    		System.out.println(arr1);
    		arr1.set(0, 'a');
    		System.out.println(arr1.get(0));
    		ArrayDemo arr2 = new ArrayDemo(5);
    		System.out.println("arr2 --> size:" + arr2.size + "\tcapacity:" + arr2.capacity);
    		System.out.println(arr2);
    		System.out.println(arr2.isEmpty());
    	}
    }
    
    
    
  • 相关阅读:
    100 道 Linux 常见面试题
    借助Redis锁,完美解决高并发秒杀问题
    'cnpm'安装install
    Git常用命令及方法大全
    idea controller service impl mapper xml切换跳转快捷键
    idea创建springboot项目用阿里云镜像
    mybatis.type-aliases-package的作用和用法
    MyBatis Generator
    https://antdv.com/components/layout-cn/
    https://mvnrepository.com/search?q=mysql-connector-java //maven
  • 原文地址:https://www.cnblogs.com/m987/p/15816801.html
Copyright © 2020-2023  润新知