Skip to content

Commit

Permalink
Merge pull request #10 from brucejiao1/master
Browse files Browse the repository at this point in the history
1510_739253131作业2.26
  • Loading branch information
jodie-zss authored Feb 26, 2017
2 parents d3e23b7 + b1a5861 commit bd52d20
Show file tree
Hide file tree
Showing 6 changed files with 812 additions and 0 deletions.
177 changes: 177 additions & 0 deletions group15/1510_739253131/src/com/bruce/homework0226/ArrayListV00.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package com.bruce.homework0226;

import com.bruce.utils.MyException;

import java.io.Serializable;
import java.util.Arrays;

/**
* 用数组实现ArrayList基本功能:add,remove,size,contains,toArray方法
* @Version: 0.0
* Created by Bruce.Jiao on 17-2-23.
*/
public class ArrayListV00 implements Serializable {

/**
* 存放集合元素的数组
*/
private transient Object[] elementData;
/**
* 集合中元素的个数
*/
private int size;

/**
* 空构造,默认数组长度为10
*/
public ArrayListV00() throws MyException {
this(10);
}

/**
* 有参构造
*
* @param initCapacity
* 用户传入的集合大小,底层数组的初始化大小
*/
public ArrayListV00(int initCapacity) throws MyException{
if(initCapacity < 0){
throw new MyException("集合大小不能小于0");
}
elementData = new Object[initCapacity];
}

/**
* 向集合中添加元素
*
* @param value
* 添加的元素,允许添加null
* @return true:添加成功 ; false:添加失败
*/
public boolean add(Object value) {
// 添加元素之前,对数组长度进行判断,此处需要传入当前元素个数+1,
ensureCapacity(size + 1);
elementData[size++] = value;
return true;
}

/**
* 返回指定位置的元素 数组和集合,下标从1开始
*
* @param index
* 用户指定的位置
* @return
*/
public Object get(int index) throws MyException {
// 判断是否越界,注意:此处判断依据是size,而不能是elementData.length,
// 集合元素个数size小于等于elementData.length
if (index >= size || index < 0) {
throw new MyException("给定数值超出集合范围");
}
return elementData[index];
}

/**
* 删除指定位置的元素
*
* @param index
* 用户指定位置,从0开始
* @return 返回删除掉的指定位置的元素
*/
public Object remove(int index) throws MyException {
if (index >= size || index < 0) {
throw new MyException("给定数值超出集合范围");
}
Object value = elementData[index];
// 数组中被删除元素后边的所有元素的个数,此处不能使用elementData.length
int length = size - 1 - index;
// 被删除位置后还有元素,将数组中被删除位置往后(不包含被删除位置)的所有元素往前移动一位
if (length > 0) {
System.arraycopy(elementData, index + 1, elementData, index, length);
}
elementData[--size] = null;
return value;
}

/**
* 判断集合中是否包含指定的元素
*
* @param value
* 用户制定的元素
* @return true:包含指定元素;false:不包含指定元素
*/
public boolean contains(Object value) {
for (int i = 0; i < elementData.length; i++) {
if (value == null) {
if (elementData[i] == null) {
return true;
}
} else {
if (value.equals(elementData[i])) {
return true;
}
}
}
return false;
}

/**
* 得到集合对应的静态数组
*
* @return 底层数组
*/
public Object[] toArray() {
//elementData可能会包含null元素,不能直接返回,需返回一个包含集合所有元素的新数组
// return elementData;
return Arrays.copyOf(elementData,size);
}

/**
* 返回集合大小
*
* @return
*/
public int size() {
return size;
}

/**
* 传入的数值与数组长度进行比较,长度小于传入数值,对数组进行扩容
*
* @param minCapacity
* 传入的数值
*/
public void ensureCapacity(int minCapacity) {
int oldCapacity = elementData.length;
// 如果传入数值大于现有数组长度,对现有数组进行扩容
if (minCapacity > oldCapacity) {
// 此处用新的局部变量引用指向原有数组的内存地址,仅为了避免复制数组元素到新数组时候,发生原有数组内存地址被覆盖的情况
Object[] oldArray = elementData;
// 先得到现有数组长度1.5倍的值
int newCapacity = oldCapacity + oldCapacity >> 1;
// 如果增加1.5倍后的数值仍然小于传入的数值,将传入的数值赋给新数组长度
if (minCapacity > newCapacity) {
newCapacity = minCapacity;
}
// 将elementData引用指向一个新的扩容后的数组,并且将原有数组的元素复制到新数组中
elementData = Arrays.copyOf(elementData, newCapacity);
}
}

/**
* 重写toString方法,查看集合具体内容
* @return
*/
@Override
public String toString() {
return Arrays.toString(elementData);
}

/**
* 仅仅作为自己查看底层数组长度的方法,
* @return
*/
int arrayLength() {
return elementData.length;
}
}
141 changes: 141 additions & 0 deletions group15/1510_739253131/src/com/bruce/homework0226/JuintTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.bruce.homework0226;

import com.bruce.utils.MyException;
import junit.framework.TestCase;
import org.junit.Test;
import java.util.Arrays;
import java.util.Random;

/**
* Created by Bruce.Jiao on 17-2-23.
*/
public class JuintTest extends TestCase{

@Test
public void testArrayList(){
try {
ArrayListV00 arrayList = new ArrayListV00(0);
arrayList.add("aaa");
arrayList.add("bbb");
arrayList.add("ccc");
arrayList.add("fff");
arrayList.add("ggg");
System.out.println("集合下标2处的元素:"+arrayList.get(2));
System.out.println("是否包含ccc这个元素:"+arrayList.contains("ccc"));
System.out.println("是否包含ddd这个元素:"+arrayList.contains("ddd"));
System.out.println("删除前集合大小为:"+arrayList.size());
System.out.println("删除下标2处元素前底层数组:"+arrayList);
arrayList.remove(2);
System.out.println("删除下标2处元素后底层数组:"+arrayList);
System.out.println("删除一个元素后集合大小为:"+arrayList.size());
arrayList.remove(2);
System.out.println("再删除下标2处元素后底层数组:"+arrayList);
System.out.println("集合为:"+ Arrays.toString(arrayList.toArray()));
System.out.println("集合底层数组长度:"+ arrayList.arrayLength());
// System.out.println("集合下标-1处的元素:"+arrayList.get(-1));
} catch (MyException e) {
System.out.println("发生异常>>>"+e);
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testLinkedList(){
try {
LinkedListV00<String> linkedList = new LinkedListV00<>();
linkedList.add("aaa");
linkedList.add("bbb");
linkedList.add("ccc");
linkedList.add("ddd");
System.out.println("删除index=2的元素前:"+linkedList);
System.out.println("链表尺寸"+linkedList.size());
System.out.println("拿到index=2的元素"+linkedList.get(2));
linkedList.remove(2);
System.out.println("删除index=2的元素后:"+linkedList);
} catch (MyException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testStack(){
try {
StackV00 stack = new StackV00();
stack.push("ccc");
stack.push(null);
stack.push("bbb");
stack.push("aaa");
System.out.println("栈的大小:"+stack.size());
System.out.println("栈是否为空:"+stack.isEmpty());
System.out.println("栈是否为空:"+stack);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
stack.clear();
System.out.println("清空后,栈大小:"+stack.size());
System.out.println("栈是否为空:"+stack.isEmpty());
} catch (MyException e) {
System.out.println(e);
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testQueue(){
try {
QueueV00 queue = new QueueV00();
System.out.println("队列是否为空:"+queue.isEmpty());
queue.add("aaa");
queue.add("bbb");
queue.add("ccc");
queue.add("ddd");
System.out.println(queue);
System.out.println("queue.peek结果:"+queue.peek());
System.out.println("peek后队列长度:"+queue.length());
System.out.println("queue.poll结果:"+queue.poll());
System.out.println("poll后队列长度:"+queue.length());
System.out.println("队列是否为空:"+queue.isEmpty());
} catch (MyException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testArrayLinked(){
try {
ArrayListV00 arrayList = new ArrayListV00();
LinkedListV00 linkedList = new LinkedListV00();
long start1 = System.currentTimeMillis();
for(int i = 0;i<10000;i++){
arrayList.add("abc"+i);
}
long end1 = System.currentTimeMillis();
for(int i = 0;i<10000;i++){
linkedList.add("abc"+i);
}
long end2 = System.currentTimeMillis();
System.out.println("ArrayList的时间:"+(end1-start1));
System.out.println("LinkedList的时间:"+(end2-end1));
} catch (MyException e) {
e.printStackTrace();
}
}

public String getRandomString(int length){
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for(int i = 0;i<length;i++){
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
}
Loading

0 comments on commit bd52d20

Please sign in to comment.