MyList接口
package collection;
/**
* 自定义接口Mylist,模仿List
* @param <E>
*/
public interface MyList<E> {
int size();
boolean isEmpty();
void add(E obj);
void add(int index, E obj);
void set(int index, E obj);
boolean contains(E obj);
Object[] toArray();
boolean remove(E obj);
void clear();
}
MyArrayList类
package collection;
import java.util.Arrays;
public class MyArrayList<E> implements MyList<E>{
private static final int DEFAULT_CAPACITY = 10;
// size是列表元素个数
private int size;
private Object[] elementData;
public MyArrayList() {
this(DEFAULT_CAPACITY);
}
public MyArrayList(int initialCapacity) {
if (initialCapacity < 0) {
try {
throw new Exception("大于零");
} catch (Exception e) {
e.printStackTrace();
}
}else{
this.elementData = new Object[initialCapacity];
}
}
public static void main(String[] args) {
MyArrayList<String> myList1 = new MyArrayList<>();
System.out.println(myList1);
myList1.add("aa");
myList1.add("bb");
myList1.add("cc");
myList1.add("dd");
myList1.add("ee");
myList1.add("ff");
myList1.add(2,"fuck");
System.out.println(myList1.set(0,"start"));
System.out.println(myList1.get(4));
System.out.println(myList1.contains("ee"));
myList1.remove(2);
myList1.remove("start");
System.out.println(myList1);
// myList1.clear();
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public void add(E obj) {
ensureCapacity();
elementData[size] = obj;
size++;
}
@Override
public void add(int index, E obj) {
rangeCheck(index);
ensureCapacity();
System.arraycopy(elementData,index,elementData,index+1,size-index);
elementData[index] = obj;
size++;
}
private void rangeCheck(int index){
if (index<0 || index>=size) {
try {
throw new Exception("index参数不合法");
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void ensureCapacity(){
if(size == elementData.length){
Object[] newElementsData = new Object[2*size+1];
System.arraycopy(elementData,0,newElementsData,0,size);
elementData = newElementsData;
}
}
public void remove(int index){
rangeCheck(index);
int numMoved = size - (index + 1);
if (numMoved > 0){
System.arraycopy(elementData,index+1,elementData,index,numMoved);
}
size--;
elementData[size] = null;
}
@Override
public E set(int index, E obj) {
rangeCheck(index);
Object oldValue = elementData[index];
elementData[index] = obj;
return (E) oldValue;
}
public E get(int index){
rangeCheck(index);
return (E) elementData[index];
}
@Override
public boolean contains(E obj) {
for (Object el : elementData) {
if (el.equals(obj)) {
return true;
}
}
return false;
}
@Override
public Object[] toArray() {
return elementData;
}
@Override
public boolean remove(E obj) {
for (int i = 0; i < size; i++) {
if (elementData[i].equals(obj)) {
remove(i);
return true;
}
}
return false;
}
@Override
public void clear() {
for (int i = 0; i < size; i++) {
elementData[i] = null;
}
size = 0;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < size; i++) {
sb.append(elementData[i]);
if (i !=size-1) {
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
}
MyLinkedList类
- 结构分析 每个节点存储了数据内容、前一个和后一个节点的内容
- 与arraylist相比,不便于查找,但是便于插入与删除
```java package collection;class Node {
Node prevoius; // 前一个节点
Object element; // 本节点保存的数据
Node next; // 后一个节点
}
public class MyLinkedList
public static void main(String[] args) {
MyLinkedList<String> mll = new MyLinkedList<>();
mll.add("aa");
mll.add("bb");
mll.add("cc");
mll.add("dd");
mll.add("ee");
mll.add("ff");
Node nn = mll.getNode(5);
mll.add(5,"fuck");
System.out.println(mll);
}
private void rangeCheck(int index){
if (index<0 || index>=size) {
try {
throw new Exception("index参数不合法");
} catch (Exception e) {
e.printStackTrace();
}
}
}
private Node getNode(int index){
rangeCheck(index);
Node n = null;
if (first != null) {
if ((size >> 1) > index) {
// 从前向后
n = first;
for (int i = 0; i < index; i++) {
n = n.getNext();
}
} else {
// 从后向前
n = last;
for (int i = size-1; i > index; i--) {
n = n.getPrevious();
}
}
}
return n;
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public void add(E obj) {
Node<E> n = new Node<>();
if (size == 0) {
n.setPrevious(null);
n.setNext(null);
n.setElement(obj);
first = n;
last = n;
}else{
n.setPrevious(last);
n.setNext(null);
n.setElement(obj);
last.setNext(n);
last = n;
}
size++;
}
@Override
public void add(int index, E obj) {
Node target = this.getNode(index);
Node previous = target.getPrevious();
Node<E> n = new Node<>();
n.setElement(obj);
n.setPrevious(previous);
n.setNext(target);
if (previous != null){
previous.setNext(n);
}
target.setPrevious(n);
// 处理一下链表
if (index == 0){
first = n;
}
if (index == size() -1){
last = n.getNext();
}
size++;
}
@Override
public Object set(int index, Object obj) {
return null;
}
@Override
public E get(int index) {
return (E) getNode(index).getElement();
}
@Override
public boolean contains(Object obj) {
return false;
}
@Override
public Object[] toArray() {
return new Object[0];
}
@Override
public boolean remove(Object obj) {
return false;
}
@Override
public void remove(int index) {
}
@Override
public void clear() {
}
}
class Node
public Node() {}
public Node getPrevious() {
return previous;
}
public void setPrevious(Node previous) {
this.previous = previous;
}
public Object getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
```