Array List样例:
/**
* 增加泛型
* 自动增加数组容量
* 增加set、get方法;增加数组边界的检查
* 增加remove方法
*/
package cn.study.lu.four;
public class MyArrayList
private Object[] elementData;
private int size;
private static final int DEFALT_CAPACITY = 10;
public MyArrayList() {
elementData = new Object[DEFALT_CAPACITY];
}
public MyArrayList(int capacity) {
elementData = new Object[capacity];
}
public void add(E e) {
if (size==elementData.length) {
Object[] newArray = new Object[elementData.length+(elementData.length>>1)];
System.arraycopy(elementData, 0, newArray, 0, elementData.length);
elementData = newArray;
}
elementData[size++] = e;
}
public Object get(int index) {
Cheak(index);
return (E)elementData[index];
}
public void set(E e,int index) {
Cheak(index);
elementData[index] = e;
}
public void Cheak(int index) {
if (index<0 ||index>size ) {
throw new RuntimeException("索引不合法"+index);
}
}
public void remove(E e) {
//将e和所有元素挨个比较,获得第一个为true的,返回
for (int i = 0; i < size; i++) {
if (e.equals(get(i))) {
remove(i);
}
}
}
public void remove(int index) {
int a = elementData.length-index-1;
if (a>0) {
System.arraycopy(elementData, index+1, elementData, index,a );
}
elementData[--size] = null;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size==0?true:false;
}
public String toString() {
StringBuilder str = new StringBuilder();
str.append("[");
for (int i = 0; i
}
str.setCharAt(str.length()-1, ']');
return str.toString();
}
public static void main(String[] args) {
MyArrayList s1 = new MyArrayList(20);
// s1.add("aa");
// s1.add("bb");
// System.out.println(s1);
for (int i = 0; i < 40; i++) {
s1.add("aaa"+i);
}
System.out.println(s1);
s1.set("啦啦啦", 39);
System.out.println(s1.get(39));
s1.remove(2);
System.out.println(s1);
System.out.println(s1.size());
System.out.println(s1.isEmpty());
}
}
Linked List样例:
package cn.study.lu.four;
/**
* 自定义一个链表并实现打印
*增加泛型
* 增加get方法
* 增加remove的两个方法
* 增加set方法
*
*/
public class MyLinkedList
private Node first;
private Node last;
private int size;
public void add(E e) {
Node node = new Node(e);
if(first == null) {
first = node;
last = node;
}else {
node.previous=last;
node.next = null;
last.next = node;
last = node;
}
size++;
}
public String toString() {
StringBuilder str = new StringBuilder("[");
Node temp = first;
while (temp!=null) {
str.append(temp.element+",");
temp = temp.next;
}
str.setCharAt(str.length()-1, ']');
return str.toString();
}
public E get(int index) {
Node temp = null;
if (index<0||index>size-1) {
throw new RuntimeException("索引不合法:"+index);
}
if (index<=((size-1)/2)) {
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.previous;
}
}
return (E)temp.element;
}
public void remove(E e) {
Node temp = first;
while (temp.element != e) {
temp = temp.next;
}
if (temp==null) {
throw new RuntimeException("不存在这个对象!");
}else {
temp.previous.next = temp.next;
temp.next.previous= temp.previous;
size--;
}
}
public void remove(int index) {
Node temp = null;
if (index<0||index>size-1) {
throw new RuntimeException("索引不合法:"+index);
}
if (index<=((size-1)/2)) {
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.previous;
}
}
if (index==0) {
temp.next.previous = null;
first = temp.next;
}else if (index == (size-1)) {
temp.previous.next = null;
last = temp.previous;
} else {
temp.previous.next = temp.next;
temp.next.previous = temp.previous;
}
size--;
}
public void set(int index,E e) {
Node a = new Node(e);
Node temp = null;
if (index<0||index>size) {
throw new RuntimeException("索引不合法:"+index);
}
if (index<=((size)/2)) {
temp = first;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
}else if(index!=size) {
temp = last;
for (int i = size-1; i >index; i--) {
temp = temp.previous;
}
}else {
temp=last;
}
if (index == 0) {
a.next = temp;
temp.previous=a;
first = a;
}else if (index==size) {
a.previous=temp;
temp.next = a;
last =a;
}else {
a.previous=temp.previous;
temp.previous.next = a;
a.next = temp;
temp.previous = a;
}
size++;
}
public static void main(String[] args) {
MyLinkedList
a.add("aaa");
a.add("bbb");
a.add("ccc");
a.add("ddd");
a.add("eee");
a.add("fff");
a.add("ggg");
System.out.println(a);
System.out.println(a.get(5));
a.remove(0);
System.out.println(a);
System.out.println(a.get(5));
a.set(0, "aaa");
System.out.println(a);
System.out.println(a.getSize());
System.out.println(a.get(5));
}
}
class Node{
Node previous;
Node next;
Object element;
public Node(Node previous, Node next, Object element) {
super();
this.previous = previous;
this.next = next;
this.element = element;
}
public Node(Object element) {
super();
this.element = element;
}
}