java List-----ArrayList类

  • ArrayList:List 接口的大小可变数组的实现。实现了所有可选列表操作,并允许包括 null 在内的所有元素
    * 父类:AbstractList
    * 父接口:List
    * 注意,此实现不是同步的。(不安全,效率高)
    * 从以下版本开始: 1.2
    *
    * Object[] obj = {};
    * 添加第一个元素的时候: obj = new Object[10];
    *
    * System.arraycopy(原数组,源数组中的起始位置, 目标数组,目标数据中的起始位置,要复制的数组元素的数量)
    * System.arraycopy(elementData,1, elementData,0,10);
    *
    * 数据结构
    * 数组
    * 堆
    * 栈
    * 队列
    * 链表
    * 树
    * 哈希结构

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Demo {
	
	public static void main(String[] args) {
		//1:创建一个ArrayList集合
		/*Collection coll = new ArrayList<>();
		List list = new ArrayList<>();*/
		//构造一个初始容量为 的空列表。
		ArrayList al = new ArrayList();
		//添加元素  底层给数组添加元素  添加第一个元素的时候扩容10  什么时候添加元素,
		什么时候给数组扩容,合适的时间,做合适的事情
		al.add("你");
		al.add("b");
		al.add("a");
		al.add("b");
		al.add("a");
		al.add("b");
		al.add("a");
		al.add("b");
		al.add("a");
		al.add("b");
		al.add("c");
		
		
		/* System.arraycopy(elementData, index+1, elementData, index,numMoved);
		                       原数组      1         目标数组    0       10 */
		System.out.println(al.remove(0));
		
		System.out.println(al.toString());
		System.out.println(al.size());
		
		
	}

}

模拟ArrayList底层的实现


import java.util.ArrayList;
import java.util.Arrays;

public class MyArrayList {
	
	/*
	 * 手动模拟ArrayList
	 * 添加元素
	 * 删除元素
	 * 修改元素
	 * 元素个数
	 * toString
	 * 
	 */
	
	private Object[] obj;
	
	private int size = 0;

	/**
	 * 构造器
	 */
	public MyArrayList() {
		super();
		this.obj = new Object[10];
	}

	/**
	 * 添加元素
	 */
	
	public boolean add(Object obj){
		//当元素的个数大于等于数组的长度的时候需要数组扩容
		if(size>=this.obj.length){
			int newCapacity =  this.obj.length+(this.obj.length>>1);
			this.obj = Arrays.copyOf(this.obj, newCapacity);
		}
		this.obj[size++] = obj;
		return true;
	}

	/**
	 * 获取元素的个数
	 * @return
	 */
	public int size(){
		
		return size;
	}
	
	/**
	 * 根据下标修改元素
	 * 
	 */
	
	public Object set(int index,Object object){
		
		Object oldValue = this.obj[index];
		this.obj[index] = object;
		return oldValue;
	}
	
	/**
	 * 根据下标删除元素
	 */
	public Object remove(int index){
		//1:先判断下表是否越界
		if(index>=this.size()){
			throw new IndexOutOfBoundsException("index:"+index+"  size:"+size);
		}
		//2 删除元素
		Object obj = this.obj[index];
		//3:移位
		int numMove = this.size()-index-1;
		if(numMove>0){
			System.arraycopy(this.obj, index+1, this.obj, index, numMove);
		}
		
	    this.obj[size--] = null;
		
		return obj;
	}
	
	/**
	 * 重写toString
	 */
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for(int i = 0;i<this.size();i++){
				if(i<size-1){
					sb.append(this.obj[i]);
					sb.append(",");
				}else{
					sb.append(this.obj[i]);
				}
		}
		sb.append("]");
		
		return sb.toString();
		
		
	}
	public static void main(String[] args) {
		//1:创建集合
		MyArrayList myl = new MyArrayList();
		//2:添加元素
		myl.add("a");
		myl.add("b");
		myl.add("c");
		myl.add("d");
		myl.add("e");
		myl.add("f");
		myl.add("g");
		myl.add("h");
		myl.add("i");
		myl.add("j");
		myl.add("o");
		
		
		System.out.println(myl.remove(0));
	//	System.out.println(myl.set(0, "大"));
		
		

		//System.out.println(myl.size());
		
		System.out.println(myl);
		
		
		
		
		
	}

}

利用ArrayList去除重复的自定义对象

import java.util.ArrayList;
import java.util.Iterator;

public class Demo2 {
	/*
	 * 什么样的对象是重复的对象?
	 *  1:对象的地址符相等,重复的对象
	 *  2:对象的属性值完全相同,重复的对象
	 *  
	 *  问题:为什么String可以去重,自定义对象不可以去重呢?
	 *      自定义对象去除重复,要重写equals方法
	 */
	
	public static void main(String[] args) {
		ArrayList al = new ArrayList<>();
		
		Student stu1 = new Student("张三",20,"男");
		Student stu2 = new Student("张三",20,"女");
		Student stu3 = new Student("张三",20,"男");
		Student stu4 = new Student("李四",30,"女");
		
		al.add(stu1);
		al.add(stu2);
		al.add(stu3);
		al.add(stu4);
		
		ArrayList al2 = new ArrayList<>();
		
		for(int i=0;i<al.size();i++){
			if(!(al2.contains(al.get(i)))){
				al2.add(al.get(i));
			}
		}
		//遍历新的集合
		Iterator it = al2.iterator();
		while(it.hasNext()){
			Student stu = (Student)it.next();
			System.out.println(stu.getName()+"\t"+stu.getAge()+"\t"+stu.getSex());
		}
		
		
		
		
	}
}

利用ArrayList去除重复字符串

import java.util.ArrayList;

public class Demo {
	
	/*
	 * F5:进方法
	 * F6:步进
	 * F7:退方法
	 */
	public static void main(String[] args) {
		
		ArrayList al = new ArrayList<>();
		al.add(10);
		al.add(20);
		al.add(10);
		al.add(20);
	
		ArrayList al2 = new ArrayList<>();
	
		//1:循环旧的集合
		for(int i = 0;i<al.size();i++){
			if(!(al2.contains(al.get(i)))){
				//如果不包含元素,就把这个元素添加到新的集合
				al2.add(al.get(i));
			}
		}
		
		
		System.out.println(al2);
		
	}

}


public class Student {
	
	private String name;
	private Integer age;
	private String sex;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Student(String name, Integer age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	public Student() {
		super();
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((age == null) ? 0 : age.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((sex == null) ? 0 : sex.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age == null) {
			if (other.age != null)
				return false;
		} else if (!age.equals(other.age))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (sex == null) {
			if (other.sex != null)
				return false;
		} else if (!sex.equals(other.sex))
			return false;
		return true;
	}
	
	

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值