在讲ArrayList和LinkedList之前,应该对他们的底层数据结构要有一些了解。所有先弄懂什么是数组,链表和队列。
1.数组数组是一组相同数据类型数据的容器。创建一个数组类似于在内存中分配了多个大小相同的格子。
public class Test {
// 创建数组
public static void main(String[] args) {
// 直接声明
String[] arr;
// 给定数组维但不赋值
String[] arr1 = new String[5];
// 给数组赋值
String[] arr2 = new String[] {"123", "456", "789"};
String[] arr3 = {"123", "456", "789"};
}
}
2.链表
链表在内存中的表现为离散的数据块,在逻辑上表现为线性的。
// 链表节点类 public class MyNode{ E val; MyNode next; public MyNode(E val, MyNode next) { this.val = val; this.next = next; } } // 链表类 public class MyList { MyNode root; MyNode tail; public void add(E obj) { if (root == null) { MyNode node = new MyNode (obj, null); root = node; tail = node; } else { MyNode node = new MyNode (obj, null); tail.next = node; tail = node; } } } public class Test { public static void main(String[] args) { MyList list = new MyList (); list.add(123); list.add(456); System.out.println(list.root.val); } }
E:泛型数据类型,类似于宏,对出现的数据设置规定数据类型。
3.队列 1)用数组实现public class ArrQueue {
Object[] DataQueue = new Object[0];
int size = 0;
public void add(Object data) {
size += 1;
if (size > DataQueue.length) {
Object[] arr = new Object[size + size / 2 + 1];
for (int i = 0; i < DataQueue.length; i++) {
arr[i] = DataQueue[i];
}
arr[size - 1] = data;
DataQueue = arr;
} else {
DataQueue[size - 1] = data;
}
}
}
public class main {
public static void main(String[] args) {
for (int i = 0; i < 10000; i++) {
qu.add("num"+i);
}
for (int i = 0; i < 10000; i++) {
System.out.println(qu.DataQueue[i]);
}
}
}
2)用链表实现
package DataStructure.Queue.LinkedQueue; public class MyList{ ListNode Head = new ListNode (null, null); ListNode head = Head, tail = Head; int size = 0; // ListNode[] ListArray; public ListNode create(int n) { if (n < 0) { System.out.println("ERROR"); return null; } if (n == 0) return head; if (n == 1) { ListNode node = new ListNode<>(null, null); head.next = node; tail = node; return head; } ListNode node1 = new ListNode (null, null); head.next = node1; tail = node1; for (int i = 1; i < n; i++) { ListNode node_ = new ListNode (null, null); tail.next = node_; tail = node_; } size = n; return head; } public void add(String value) { ListNode node = new ListNode<>(value, null); tail.next = node; tail = node; size += 1; } public void delete(int index) { if (size == 0 || index < 0 || index >= size) { System.out.println("ERROR"); return ; } ListNode p = head; if (index == 0) { p.next = p.next.next; size -= 1; return ; } for (int i = 0; i < index; i++) { p = p.next; } p.next = p.next.next; size -= 1; } public void change(String value, int index) { if (index < 0 || index >= size) return ; ListNode p = head; for (int i = 0; i <= index; i++) { p = p.next; } p.ob = value; } public void insert(String value, int index) { if (index < 0) return ; if (index >= size) { add(value); return ; } ListNode node = new ListNode<>(value, head.next); if (index == 0) { head.next = node; return ; } ListNode p = head; for (int i = 0; i < index; i++) { p = p.next; } p.next = node; } public void print(ListNode p) { if (p.next == null) return ; System.out.println(p.next.ob); print(p.next); } public int size() { return size; } }



