Implement Stack using Arrays and LinkedList
public class MyStackArray<E> {
private int size = 0;
private static final int DEFAULT_CAPACITY = 10;
private Object elements[];
public MyStackArray() {
elements = new Object[DEFAULT_CAPACITY];
}
public void push(E e) {
if (size == elements.length) {
ensureCapa();
}
elements[size++] = e;
}
@SuppressWarnings("unchecked")
public E pop() {
E e = (E) elements[--size];
elements[size] = null;
return e;
}
private void ensureCapa() {
int newSize = elements.length * 2;
elements = Arrays.copyOf(elements, newSize);
}
}
// Use LinkedList
import java.util.ArrayList;
public class MyStackList<E> extends ArrayList<E> {
private static final long serialVersionUID = 1L;
public E pop() {
E e = get(size() - 1);
remove(size() - 1);
return e;
}
public void push(E e) {
add(e);
}
}
https://gist.github.com/gcrfelix/736c75c6eee0372c6efc
从空间角度来看,如果存10000个元素,LinkedList和ArrayList哪个更节约空间?
答:ArrayList. 每存一个元素,LinkedList都需要占用一个指针和该元素的空间,所以他的空间一直是2n的占有量,但是ArrayList的长度是1,2,4,8....
增长,只有当element占满了array的长度arraylist才会扩容,所以他的空间占有量是<=2n。
import java.util.Arrays;
public class MyList<E> {
private int size = 0;
private static final int DEFAULT_CAPACITY = 10;
private Object elements[];
public MyList() {
elements = new Object[DEFAULT_CAPACITY];
}
public void add(E e) {
if (size == elements.length) {
ensureCapa();
}
elements[size++] = e;
}
private void ensureCapa() {
int newSize = elements.length * 2;
elements = Arrays.copyOf(elements, newSize);
}
@SuppressWarnings("unchecked")
public E get(int i) {
if (i>= size || i <0) {
throw new IndexOutOfBoundsException("Index: " + i + ", Size " + i);
}
return (E) elements[i];
}
}
public class MyStackArray<E> {
private int size = 0;
private static final int DEFAULT_CAPACITY = 10;
private Object elements[];
public MyStackArray() {
elements = new Object[DEFAULT_CAPACITY];
}
public void push(E e) {
if (size == elements.length) {
ensureCapa();
}
elements[size++] = e;
}
@SuppressWarnings("unchecked")
public E pop() {
E e = (E) elements[--size];
elements[size] = null;
return e;
}
private void ensureCapa() {
int newSize = elements.length * 2;
elements = Arrays.copyOf(elements, newSize);
}
}
// Use LinkedList
import java.util.ArrayList;
public class MyStackList<E> extends ArrayList<E> {
private static final long serialVersionUID = 1L;
public E pop() {
E e = get(size() - 1);
remove(size() - 1);
return e;
}
public void push(E e) {
add(e);
}
}
https://gist.github.com/gcrfelix/736c75c6eee0372c6efc
从空间角度来看,如果存10000个元素,LinkedList和ArrayList哪个更节约空间?
答:ArrayList. 每存一个元素,LinkedList都需要占用一个指针和该元素的空间,所以他的空间一直是2n的占有量,但是ArrayList的长度是1,2,4,8....
增长,只有当element占满了array的长度arraylist才会扩容,所以他的空间占有量是<=2n。
import java.util.Arrays;
public class MyList<E> {
private int size = 0;
private static final int DEFAULT_CAPACITY = 10;
private Object elements[];
public MyList() {
elements = new Object[DEFAULT_CAPACITY];
}
public void add(E e) {
if (size == elements.length) {
ensureCapa();
}
elements[size++] = e;
}
private void ensureCapa() {
int newSize = elements.length * 2;
elements = Arrays.copyOf(elements, newSize);
}
@SuppressWarnings("unchecked")
public E get(int i) {
if (i>= size || i <0) {
throw new IndexOutOfBoundsException("Index: " + i + ", Size " + i);
}
return (E) elements[i];
}
}