- 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) {
ArrayList al = new ArrayList();
什么时候给数组扩容,合适的时间,做合适的事情
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.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 {
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;
}
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){
if(index>=this.size()){
throw new IndexOutOfBoundsException("index:"+index+" size:"+size);
}
Object obj = this.obj[index];
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;
}
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) {
MyArrayList myl = new MyArrayList();
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);
}
}
利用ArrayList去除重复的自定义对象
import java.util.ArrayList;
import java.util.Iterator;
public class Demo2 {
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 {
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<>();
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;
}
}