ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-01-18 11:01:53  阅读:103  来源: 互联网

标签:index capacity 封装 Object 改查 增删 return array size



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());
	}
}


标签:index,capacity,封装,Object,改查,增删,return,array,size
来源: https://www.cnblogs.com/m987/p/15816801.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有