一、集合框架接口图
在开始实现LinkedList类和ArrayList 类之前,首先我们来认识一下Java集合框架的接口图。
说明:
- Java的集合类主要由两个接口开始衍生,即Collection和Map,它们是Java集合框架的根接口。
- Collection是一个高度抽象出来的集合,它包含了List和Set两大分支。
- LIst是一个有序的队列,它的实现类有LinkedList,ArrayList,Vector,Stack;
Set是一个不允许有重复元素的集合。它的实现类有HastSet,TreeSet。 - ArrayList是一个动态数组,而LinkedList是一个双向链表。所以它除了有ArrayList的基本操作方法外还额外提供了get,remove,insert方法。
二、实现LinkedList
1.LinkedList定义
LinkedList是一个双向链表,建议在有频繁插入和删除时使用。 LinkedList不是线程安全的。
2.LinkedList方法
通过查阅jdk文档,我们可以看到它的方法。下面列举一二。
3.实现MyLinkedList
class ListNodeX{
public int data;
public ListNodeX prev;
public ListNodeX next;
public ListNodeX(int data){
this.data = data;
}
}
public class MyLinkedList2 {
public ListNodeX head;
public ListNodeX last;
public void addFirst(int data) {
ListNodeX node = new ListNodeX(data);
if(head == null){
this.head = node;
}else{
node.next = this.head;
this.head.prev = node;
this.head = node;
}
}
public void addLast(int data) {
ListNodeX node = new ListNodeX(data);
if(head == null){
this.head = node;
this.last = node;
}else{
this.last.next = node;
node.prev = this.last;
this.last = node;
}
}
public void addIndex(int index,int data) {
ListNodeX cur = this.head;
if(index < 0||index > size())return;
if(index == 0){
addFirst(data);
return;
}
if(index==size()){
addLast(data);
return;
}
while(index != 0){
cur = cur.next;
index--;
}
ListNodeX node = new ListNodeX(data);
node.next = cur;
cur.prev.next = node;
node.prev = cur.prev;
cur.prev = node;
}
public boolean contains(int key) {
ListNodeX cur = head;
while(cur!=null){
if(cur.data == key){
return true;
}
cur = cur.next;
}
return false;
}
public void remove(int key) {
ListNodeX cur = this.head;
while(cur!=null){
if(cur.data == key){
if(cur==this.head){
this.head = this.head.next;
if(this.head==null){
this.last = null;
}else{
this.head.prev = null;
}
}else{
cur.prev.next = cur.next;
if(cur.next == null){
this.last = cur.prev;
}else{
cur.next.prev = cur.prev;
}
}
return;
}else{
cur = cur.next;
}
}
}
public void removeAllKey(int key) {
ListNodeX cur = this.head;
while(cur!=null){
if(cur.data == key){
if(cur==this.head){
this.head = this.head.next;
if(this.head==null){
this.last = null;
}else{
this.head.prev = null;
}
}else{
cur.prev.next = cur.next;
if(cur.next == null){
this.last = cur.prev;
}else{
cur.next.prev = cur.prev;
}
}
cur = cur.next;
}else{
cur = cur.next;
}
}
}
public int size() {
int count = 0;
ListNodeX cur = head;
while(cur!=null){
count++;
cur = cur.next;
}
return count;
}
public void display() {
ListNodeX cur = head;
while(cur!=null){
System.out.print(cur.data+" ");
cur = cur.next;
}
System.out.println();
}
public void clear() {
ListNodeX cur = this.head;
while(cur!=null){
ListNodeX curNext = cur.next;
cur.prev = null;
cur.next = null;
cur = curNext;
}
this.head = null;
this.last = null;
}
}
三、实现ArrayList
1.ArrayList定义
ArrayList的底层结构通过数组实现。
2.ArrayList方法
3.实现MyArrayList
public class MyArrayList {
public int[] elem;
public int usedSize;
public static final int intCapacity = 10;
public MyArrayList(){
this.elem = new int[intCapacity];
this.usedSize = 0;
}
public boolean isFull(){
if(usedSize == this.elem.length){
return true;
}
return false;
}
public void add(int pos,int data){
if(isFull()){
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
if(pos<0||pos>this.usedSize)return;
int i = this.usedSize - 1;
while(i>=pos){
this.elem[i+1] = this.elem[i];
i--;
}
this.elem[pos] = data;
this.usedSize++;
}
public void show(){
for(int i = 0;i < this.usedSize;i++){
System.out.print(this.elem[i]+" ");
}
System.out.println();
}
public boolean contains(int n){
for(int i = 0;i < this.usedSize;i++){
if(this.elem[i] == n){
return true;
}
}
return false;
}
public int search(int val){
for(int i = 0;i < this.usedSize;i++){
if(val == this.elem[i]){
return i;
}
}
return -1;
}
public int getPos(int pos){
if(this.usedSize==0)return -1;
if(pos<0||pos>=this.usedSize) {
return -1;
}
return this.elem[pos];
}
public int Size(){
return usedSize;
}
public void deleteKey(int del){
int index = search(del);
for (int i = index; i < this.usedSize - 1;i++){
this.elem[i] = this.elem[i+1];
}
this.usedSize--;
}
public void clear(){
this.usedSize = 0;
}
}
|