Skip to content

Commit

Permalink
Merge pull request #9 from SarahhLee/master
Browse files Browse the repository at this point in the history
Merge SarahhLee  Commit
  • Loading branch information
gqipan authored Feb 27, 2017
2 parents 452821d + 33f25dd commit f31f708
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
6 changes: 6 additions & 0 deletions group11/1059156023/dataStructure/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions group11/1059156023/dataStructure/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions group11/1059156023/dataStructure/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>dataStructure</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.coding.basic;

import java.util.List;

public class ArrayList implements List{
private int size;

//设置一个默认容量,当调用默认构造函数实例化数组后,需要扩容时可用
private static final int DEFAULT_CAPACITY=10;

//Integer.MAX_VALUE:2147483647,MAX_ARRAY_SIZE:2147483639
private static final int MAX_ARRAY_SIZE=Integer.MAX_VALUE-8;

private Object[] elementData;

//定义一个默认为空的数组,供默认构造函数使用
private static final Object[] EMPTY_ELEMENTDATA={};

//定义默认构造函数,实例化为空数组
public ArrayList(){
this.elementData=EMPTY_ELEMENTDATA;
}

//定义一个有参的构造函数
public ArrayList(int initialCapacity){
if(initialCapacity<0)
throw new IllegalArgumentException("Illegal Capacity:"+initialCapacity);
this.elementData = new Object[initialCapacity];
}

//定义add(Object o)方法,默认在数组末尾添加
public boolean add(Object o){
//要添加一个数,所以用ensureCapacityInternal()判断size+1个的数,数组是否放得下
ensureCapacityInternal(size+1);
elementData[size++]=o;
return true;
}

private void ensureCapacityInternal(int minCapacity) {
if(elementData == EMPTY_ELEMENTDATA)
minCapacity = DEFAULT_CAPACITY;

//如果需要扩容,则调用grow()
if(minCapacity-elementData.length>0)
grow(minCapacity);
}

private void grow(int minCapacity) {
int oldCapacity = elementData.length;
int newCapacity = oldCapacity+(oldCapacity>>1);

//原始长度是0时,即原来是空数组时
if(newCapacity-minCapacity<0)
newCapacity = minCapacity;

//如果新的容量超过了数组最大容量,就调用hugeCapacity()把能给的最大容量给它
if(newCapacity-MAX_ARRAY_SIZE>0)
newCapacity = hugeCapacity(minCapacity);


}

private static int hugeCapacity(int minCapacity) {
if (minCapacity<0) {
throw new OutOfMemoryError(); //抛出内存溢出异常
}
//如果minCapacity比MAX_ARRAY_SIZE大,则返回int类型所能表示的最大值,否则返回MAX_ARRAY_SIZE
return (minCapacity>MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.coding;
package com.coding;

import com.coding.basic.*;
public class Main {
public static void main(String[] args) {
ArrayList list = new ArrayList();
ArrayList list = new ArrayList();

list.add(0, "2xxx");
list.add(1, "we");
Expand All @@ -17,7 +17,7 @@ public static void main(String[] args) {
}
System.out.println(list.size());

LinkedList llist = new LinkedList();
LinkedList llist = new LinkedList();
llist.add("hu");
llist.add("zhao");
llist.add(2,"xing");
Expand All @@ -31,12 +31,12 @@ public static void main(String[] args) {
System.out.print(llist.get(i));
System.out.println(llist.size());

Iterator literator = llist.iterator();
Iterator literator = llist.iterator();
while(literator.hasNext()) {
System.out.println(literator.next());
}

Stack stack = new Stack();
Stack stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
Expand All @@ -45,7 +45,7 @@ public static void main(String[] args) {
while(!stack.isEmpty())
System.out.println(stack.pop());

Queue queue = new Queue();
Queue queue = new Queue();
queue.enQueue(1);
queue.enQueue(2);
queue.enQueue(3);
Expand Down

0 comments on commit f31f708

Please sign in to comment.