Skip to content

Commit

Permalink
创建
Browse files Browse the repository at this point in the history
  • Loading branch information
LiErSan123 committed Nov 30, 2018
0 parents commit 7bf4f46
Show file tree
Hide file tree
Showing 7 changed files with 664 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*.iml
/.idea
253 changes: 253 additions & 0 deletions src/Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
/**
* @author Li Ersan
*/
public class Array<E> {

/**
* 数据数组
*/
private E[] data;

/**
* 数组元素的个数
*/
private int size;

/**
* 构造函数,传入数组的容量capacity构造Array
*
* @param capacity
*/
public Array(int capacity) {
data = (E[]) new Object[capacity];
}

/**
* 无参构造函数,默认数组容量capacity为10
*/
public Array() {
this(10);
}

/**
* 获取数组中元素的个数
*
* @return
*/
public int getSize() {
return size;
}

/**
* 获取数组的容量
*
* @return
*/
public int getCapacity() {
return data.length;
}

/**
* 判断数组是否为空
*
* @return
*/
public boolean isEmpty() {
return size == 0;
}

/**
* 在数组后面添加一个新元素
*
* @param e
*/
public void addLast(E e) {
add(size, e);
}

/**
* 在数组最前面添加一个新元素
*
* @param e
*/
public void addFirst(E e) {
add(0, e);
}

/**
* 在index个位置插入一个新元素e
*
* @param index
* @param e
*/
public void add(int index, E e) {

if (index < 0 || index > size) {
throw new IllegalArgumentException("index范围错误");
}

if (size == data.length) {
resize(2 * data.length);
}

for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = e;
size++;
}

/**
* 获取int索引位置的元素
*
* @param index
* @return
*/
public E get(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("index范围错误");
}

return data[index];
}

public E getLast() {
return get(size - 1);
}

public E getFirst() {
return get(0);
}

/**
* 修改index索引位置的元素e
*
* @param index
* @param e
*/
public void set(int index, E e) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("index范围错误");
}

data[index] = e;
}

/**
* 查找数组中是否包含元素e
*
* @param e
* @return
*/
public boolean contains(E e) {

for (int i = 0; i < size; i++) {
if (data[i].equals(e)) {
return true;
}
}

return false;
}

/**
* 查找数组中元素e所在的索引,如果不存在元素e,则返回-1
*
* @param e
* @return
*/
public int find(E e) {
for (int i = 0; i < size; i++) {
if (data[i] == e) {
return i;
}
}

return -1;
}

/**
* 从数组中删除index位置的元素,返回删除的元素
*
* @param index
* @return
*/
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("index范围错误");
}

E ret = data[index];
for (int i = index + 1; i < size; i++) {
data[i - 1] = data[i];
}

size--;
data[size] = null; //loitering objects != memory leak

if (size == data.length / 4 && data.length / 2 != 0) {
resize(data.length / 2);
}

return ret;
}

/**
* 从数组中删除第一个元素,返回删除的元素
*
* @return
*/
public E removeFirst() {
return remove(0);
}

/**
* 从数组中删除最后一个元素,返回删除的元素
*
* @return
*/
public E removeLast() {
return remove(size - 1);
}

/**
* 从数组中删除元素e
*
* @param e
*/
public void removeElement(E e) {
int index = find(e);

if (index != -1) {
remove(index);
}
}

@Override
public String toString() {

StringBuilder res = new StringBuilder();
res.append(String.format("Array: size = %d, capacity = %d\n", size, data.length));
res.append("[");

for (int i = 0; i < size; i++) {
res.append(data[i]);
if (i != size - 1) {
res.append(", ");
}
}
res.append("]");

return res.toString();
}

private void resize(int newCapacity) {
E[] newData = (E[]) new Object[newCapacity];

for (int i = 0; i < size; i++) {
newData[i] = data[i];
}

data = newData;
}
}
62 changes: 62 additions & 0 deletions src/ArrayStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @author Li Ersan
*/
public class ArrayStack<E> implements Stack<E> {

Array<E> array;

public ArrayStack(int capacity) {
array = new Array<>(capacity);
}

public ArrayStack() {
array = new Array<>();
}

@Override
public int getSize() {
return array.getSize();
}

@Override
public boolean isEmpty() {
return array.isEmpty();
}

public int getCapacity() {
return array.getCapacity();
}

@Override
public void push(E e) {
array.addLast(e);
}

@Override
public E pop() {
return array.removeLast();
}

@Override
public E peek() {
return array.getLast();
}

@Override
public String toString() {

StringBuilder res = new StringBuilder();
res.append("Stack: ");
res.append("[");

for (int i = 0; i < array.getSize(); i++) {
res.append(array.get(i));
if (i != array.getSize() - 1) {
res.append(", ");
}
}
res.append("] top");

return res.toString();
}
}
Loading

0 comments on commit 7bf4f46

Please sign in to comment.