将arraylist中的方法写一遍

Java集合框架


自己定义一个类,实现集合的增删改查

import java.util.Arrays;
//将arraylist中的方法写一遍
public class MyPrcatice {
	
	//存储数据的数组
	private Object[] data;
	//记录元素个数
	private int size;

	//初始容量
	public MyPrcatice() {
		data = new Object[0];
	}

	public MyPrcatice(int capcity) {
		if (capcity <= 0) {
			data = new Object[0];
		} else {
			data = new Object[capcity];
		}
	}

	// 扩容方法,当前类中其他方法的辅助,所以不需要为外界提供该功能
	private void grow() {
		/*
		 * 先需要扩容 容量最小需要size+1 
		 */
		if (data.length <= size + 1) {
			int newCapcity = data.length * 2 + 10; // 首次扩容防止为0
			if (newCapcity < size + 1) {
				newCapcity = size + 1;
			}
			// 将数组扩容为新容量
			data = Arrays.copyOf(data, newCapcity);
		}
	}

	// 向末尾添加元素
	public void add(Object element) {

		grow();
		data[size] = element;  // o- size-1 都有元素     size 空着
		size++;
	}

	// 向指定位置添加元素
	public void add(int index, Object element) {
		if (index < 0 || index > size) { // 判断下标是否合理
			// 抛出异常
			throw new IndexOutOfBoundsException("index的范围需要在[0 " + size + "]");
		}
		// 向末尾添加,直接调
		if (index == size) {
			add(element);
		}
		// 扩容
		grow();
		/*
		 * 将index位置挪出 size -1 (最后一个元素) size size -2 size -1
		 * 
		 * ... index index + 1
		 */
		for (int i = size - 1; i >= index; i--) {
			data[i + 1] = data[i];
		}

		// 将新元素放到index处
		data[index] = element;

		size++;
	}

	// 判断下标是否在[0 size-1]之间

	public void indexCheck(int index) {
		if (index < 0 || index > size - 1) {
			throw new IndexOutOfBoundsException("下标越界,index = " + index);
		}
	}

	// 根据下标获取元素
	public Object get(int index) {
		indexCheck(index);
		return data[index];
	}

	// 修改元素,返回值为修改之前的值
	public Object set(int index, Object newElement) {
		indexCheck(index);
		Object old = data[index];
		data[index] = newElement;
		return old;
	}

	// 按照下标删除元素,把旧值保存起来
	public Object remove(int index) {
		indexCheck(index);
		// 从index+1 到size-1集体向前挪动一格
		/*
		 * index + 1 size -1 1 2 1 9 10 20
		 * 
		 * 需要挪动的元素个数: (size - 1 ) - (index + 1) + 1 size - index - 1
		 * 
		 * 如果要删除的最后一个元素,算出来挪动的元素个数为0
		 * 
		 */
		Object old = data[index];
		int moved = size - index - 1;
		if (moved > 0) {
			System.arraycopy(data, index + 1, data, index, moved);
		}
		// 将最后一个元素赋值为null
		data[size - 1] = null;
		// 将元素个数减1
		size--;
		return old;
	}

	// 获取元素个数

	public int getSize() {

		return size;

	}

	// 根据元素获取第一次出现的位置,需要下标就用for循环

	public int indexOf(Object element) {
		if (element == null) {
			for (int i = 0; i < size; i++) {
				if (data[i] == element) {
					return i;
				}
			}
		}else {
			for (int i = 0; i < size; i++) {
				if (element.equals(data[i])) {
					return i;
				}
			}
		}

		return -1;
	}
	
	//找最后一次出现的位置
	public int lastIndexOf(Object element) {
		if (element == null) {
			for (int i = size -1; i >=0 ; i--) {
				if (data[i] == element) {
					return i;
				}
			}
		}else {
			for (int i = size -1; i >=0 ; i--) {
				if (element.equals(data[i])) {
					return i;
				}
			}
		}

		return -1;
	}
		
	

//将数组的长度变的和元素个数一样多

public void trimSize() {
	data = Arrays.copyOf(data, size);
	
	
	
	
	
 }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
非常感谢您的提问,我很乐意为您解答关于JAVAArrayList集合的问题。 ArrayListJAVA最常用的集合之一,它是一个动态数组,可以根据需要自动扩展大小。ArrayList可以存储任何类型的对象,包括基本数据类型和自定义对象。下面是一些关于ArrayList的详细讲解: 1. ArrayList的创建和初始化 在JAVA,创建ArrayList对象的语法如下: ArrayList<数据类型> arrayList = new ArrayList<数据类型>(); 其,数据类型可以是任何JAVA的数据类型,例如:Integer、String、Double等等。初始化ArrayList对象后,可以使用add()方法向其添加元素,例如: arrayList.add("Hello"); arrayList.add("World"); arrayList.add("!"); 2. ArrayList的常用方法 ArrayList提供了许多常用的方法,例如: - add():向ArrayList添加元素 - get():获取ArrayList指定位置的元素 - set():替换ArrayList指定位置的元素 - remove():从ArrayList删除指定元素 - size():获取ArrayList元素的数量 - clear():清空ArrayList的所有元素 下面是一些示例代码: // 添加元素 arrayList.add("Java"); arrayList.add("is"); arrayList.add("awesome"); // 获取元素 String firstElement = arrayList.get(0); System.out.println(firstElement); // 输出:Java // 替换元素 arrayList.set(1, "really"); System.out.println(arrayList); // 输出:[Java, really, awesome] // 删除元素 arrayList.remove("Java"); System.out.println(arrayList); // 输出:[really, awesome] // 获取元素数量 int size = arrayList.size(); System.out.println(size); // 输出:2 // 清空ArrayList arrayList.clear(); System.out.println(arrayList); // 输出:[] 3. ArrayList的遍历 遍历ArrayList可以使用for循环或者foreach循环,例如: // 使用for循环遍历ArrayList for (int i = 0; i < arrayList.size(); i++) { String element = arrayList.get(i); System.out.println(element); } // 使用foreach循环遍历ArrayList for (String element : arrayList) { System.out.println(element); } 4. ArrayList的性能 ArrayList的性能比较高,因为它是基于数组实现的,可以随机访问元素。但是,当需要频繁插入或删除元素时,ArrayList的性能会受到影响,因为需要移动数组的元素。在这种情况下,可以考虑使用LinkedList。 总之,ArrayListJAVA非常常用的集合之一,它提供了许多常用的方法,可以方便地操作元素。希望我的回答能够帮助您更好地理解ArrayList

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值