一 Collection是顶级接口 ,List(有序可重复)和Set(无序不可重复)也是接口 剩下的是实现类
还是别人总结的比较好https://blog.csdn.net/a23006239/article/details/47442867
二 简单实现ArrayList和LinkedList
public class MyArrayList {
private Object[] eleData;
private int size;
public MyArrayList(){
this(10);//调用构造方法this(参数1,参数2)
}
public MyArrayList(int initialCapacity) {
if(initialCapacity < 0){
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}
eleData =new Object[initialCapacity];
}
/**
*
* @param args
*/
public void add(Object o){
//数组扩容
if(size==eleData.length){
Object[] newArr=new Object[size*2+1];
System.arraycopy(eleData, 0, newArr, 0, eleData.length);
eleData = newArr;
}
eleData[size++] = o;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public void remove(int index){
rangeCheck(index);
int numMoved=size-index-1;
if(numMoved > 0){
System.arraycopy(eleData, index+1,eleData , index, numMoved);
}
eleData[--size]=null;
}
public void remove(Object o){
for(int i = 0; i < size; i++){
if(get(i).equals(o)){
remove(i);
}
}
}
public Object get(int i) {
rangeCheck(i);
return eleData[i];
}
public void rangeCheck(int index) {
if(index < 0 || index >= size){
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MyArrayList list=new MyArrayList();
list.add("111");
list.add("112");
list.add("113");
list.add("114");
list.add("115");
list.add("116");
list.remove("111");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
(¥)LinkedList实现是一个双向链表
public class Node {
Node pre;
Node next;
Object obj;
public Node(){
}
public Node(Node pre, Object obj, Node next){
super();//super调用父类的方法 父类是Object
this.pre=pre;
this.obj=obj;
this.next=next;
}
public Node getPre() {
return pre;
}
public void setPre(Node pre) {
this.pre = pre;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
}
public class MyLinkedList {
private Node first;
private Node last;
private int size;
public void add(Object obj){
Node n=new Node();
if(first == null){
n.setNext(null);
n.setObj(obj);
n.setPre(null);
first=n; //添加的第一个是空的,将 first last=n
last=n;
}else{//直接往last节点后面增加新的节点
n.setPre(last);
n.setNext(null);
n.setObj(obj);
last.setNext(n);
last=n;
}
size++;
}
public int size(){
return size;
}
private void rangeCheck(int index){
if(index<0 || index >=size){
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public Object get(int index){
rangeCheck(index);
Node temp=node(index);
if(temp != null){
return temp.obj;
}
return null;
}
public Node node(int index) {
Node temp = null;
if(first !=null){
if(index < (size>>1)){
temp=first;
for(int i=0; i<index;i++){
temp=temp.next;
}
}else{
temp=last;
for(int i=size-1; i > index; i--){
temp=temp.pre;
}
}
}
return temp;
}
public void remove(int index){
Node temp=node(index);
if(temp!=null){
temp.pre.next =temp.next;
temp.next.pre=temp.pre;
size--;
}
}
public static void main(String[] args) {
MyLinkedList list=new MyLinkedList();
list.add("aaa");
list.add("aab");
list.add("aac");
list.remove(1);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}