-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from Greastate/master
八组作业提交
- Loading branch information
Showing
40 changed files
with
1,784 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## cpu 内存 硬盘 指令之间的关系 | ||
> 计算机整体分为软件和硬件。 | ||
> 软件是由指令和数据组成的程序,硬件包含主板、CPU、内存、硬盘等 | ||
|
||
指令指的是计算机可以识别的机器语言,每个操作系统都有内置的指令集,计算机可根据不同的指令来驱动计算机工作或是相应的输出。指令是以二进制的形式存储在硬盘中,当CPU执行某个指令的时候,需要将指令先从硬盘加载到内存,CPU从内存中获取执行解释执行。 | ||
|
||
CPU是计算的核心部件,相当于人类的大脑。CPU执行指令的时候其实不关心也不知道指令的意思,只是按照指令执行。由于硬盘读写性能比较差,CPU从硬盘中直接读取数据的时候比较费时,就有的内存的存在。内存比硬盘读写速度快很多,CPU要执行程序时,先将程序加载到内存中在执行。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package com.my.list; | ||
|
||
/** | ||
* 实现ArrayList | ||
* | ||
*/ | ||
public class ArrayList { | ||
//初始化容量 | ||
private static final int INIT = 5; | ||
//增长因子 | ||
private static final double GROWTH = 0.5; | ||
|
||
//初始化数组 | ||
Object[] baseArr = new Object[INIT]; | ||
//当前下标 | ||
int currentIndex = 0; | ||
//最大下标 | ||
int maxIndex = INIT-1; | ||
|
||
/** | ||
* 添加元素 | ||
* @param obj | ||
* @return | ||
*/ | ||
public Object add(Object obj){ | ||
if(judgeCapacity()){ | ||
baseArr = arrayDilatation(baseArr, GROWTH); | ||
maxIndex = baseArr.length-1; | ||
} | ||
baseArr[currentIndex] = obj; | ||
++currentIndex; | ||
return obj; | ||
} | ||
|
||
/** | ||
* 指定下标添加元素 | ||
* @param index | ||
* @param obj | ||
* @return | ||
*/ | ||
public Object add(int index , Object obj){ | ||
if (index < 0 || index > currentIndex) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Object[] dest = new Object[currentIndex+1]; | ||
System.arraycopy(baseArr, 0, dest, 0, index); | ||
dest[index] = obj; | ||
System.arraycopy(baseArr, index, dest, index+1, currentIndex-index); | ||
++currentIndex; | ||
baseArr = dest; | ||
return obj; | ||
} | ||
|
||
/** | ||
* 指定下标删除元素 | ||
* @param index | ||
* @return | ||
*/ | ||
public Object remove(int index){ | ||
if (index < 0 || index >= currentIndex) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Object object = baseArr[index]; | ||
Object[] dest = new Object[currentIndex]; | ||
System.arraycopy(baseArr, 0, dest, 0, index); | ||
System.arraycopy(baseArr, index+1, dest, index, currentIndex-index-1); | ||
--currentIndex; | ||
baseArr = dest; | ||
return object; | ||
} | ||
|
||
/** | ||
* 根据下标获取元素 | ||
* @param index | ||
* @return | ||
*/ | ||
public Object get(int index){ | ||
|
||
return baseArr[index]; | ||
} | ||
|
||
/** | ||
* 获取集合大小 | ||
* @return | ||
*/ | ||
public int size(){ | ||
return currentIndex; | ||
} | ||
|
||
/** | ||
* 判断容量是否需要增加 | ||
* @return | ||
*/ | ||
public boolean judgeCapacity(){ | ||
if(currentIndex > maxIndex){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* 数组扩容 | ||
* @param objArr | ||
* @param groth | ||
* @return | ||
*/ | ||
public static Object[] arrayDilatation(Object[] objArr , double groth){ | ||
int length = objArr.length; | ||
int newLength = (int) (length * groth + length); | ||
Object[] baseArr = new Object[newLength]; | ||
System.arraycopy(objArr, 0, baseArr, 0, length); | ||
return baseArr; | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package com.my.list; | ||
|
||
/** | ||
* 实现 LinkedList | ||
* | ||
*/ | ||
public class LinkedList { | ||
|
||
//首节点 | ||
Node first = new Node(); | ||
//集合大小 | ||
int size = 0; | ||
|
||
/** | ||
* 添加元素 | ||
* @param object | ||
* @return | ||
*/ | ||
public Object add(Object object){ | ||
if (size == 0) { | ||
first.setObject(object); | ||
}else{ | ||
Node previous = first; | ||
Node temp = first.getNext(); | ||
while (temp != null) { | ||
previous = temp; | ||
temp = temp.getNext(); | ||
} | ||
Node node = new Node(); | ||
node.setObject(object); | ||
previous.setNext(node); | ||
} | ||
++size; | ||
return object; | ||
} | ||
|
||
/** | ||
* 根据下标添加元素 | ||
* @param index | ||
* @param object | ||
* @return | ||
*/ | ||
public Object add(int index , Object object){ | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
if (size == 0) { | ||
first.setObject(object); | ||
}else{ | ||
if (index == 0) { | ||
Node temp = new Node(); | ||
temp.setObject(object); | ||
temp.setNext(first); | ||
first = temp; | ||
}else{ | ||
int count = 1; | ||
Node temp = first; | ||
while (temp != null) { | ||
if (count++ == index) { | ||
Node next = temp.getNext(); | ||
Node node = new Node(); | ||
temp.setNext(node); | ||
node.setObject(object); | ||
node.setNext(next); | ||
break; | ||
} | ||
temp = temp.getNext(); | ||
} | ||
} | ||
} | ||
++size; | ||
return object; | ||
} | ||
|
||
/** | ||
* 根据下标删除元素 | ||
* @param index | ||
* @return | ||
*/ | ||
public Object remove(int index){ | ||
if (index < 0 || index >= size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Node node = null; | ||
if (index == 0) { | ||
Node next = first.getNext(); | ||
first = next; | ||
node = first; | ||
}else{ | ||
int count = 1; | ||
Node temp = first; | ||
while (temp != null) { | ||
if (count++ == index) { | ||
node = temp.getNext(); | ||
Node next = node.getNext(); | ||
temp.setNext(next); | ||
break; | ||
} | ||
temp = temp.getNext(); | ||
} | ||
} | ||
--size; | ||
return node.getObject(); | ||
} | ||
|
||
/** | ||
* 根据下标获取元素 | ||
* @param index | ||
* @return | ||
*/ | ||
public Object get(int index) { | ||
Node temp = first; | ||
int count = 0; | ||
while (temp != null) { | ||
if (count++ == index) { | ||
break; | ||
} | ||
temp = temp.getNext(); | ||
} | ||
return temp.getObject(); | ||
} | ||
|
||
/** | ||
* 获取集合大小 | ||
* @return | ||
*/ | ||
public int size() { | ||
return size; | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
/** | ||
* 节点元素 | ||
* | ||
*/ | ||
class Node{ | ||
private Object object ; | ||
private Node next; | ||
public Object getObject() { | ||
return object; | ||
} | ||
public void setObject(Object object) { | ||
this.object = object; | ||
} | ||
public Node getNext() { | ||
return next; | ||
} | ||
public void setNext(Node next) { | ||
this.next = next; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.my.list; | ||
|
||
public class Test { | ||
|
||
public static void main(String[] args) { | ||
|
||
testArrayList(); | ||
System.out.println("--------------------------------"); | ||
testLinkedList(); | ||
} | ||
|
||
/** | ||
* ArrayList 测试 | ||
*/ | ||
public static void testArrayList(){ | ||
System.out.println("ArrayList 测试 --开始"); | ||
ArrayList list = new ArrayList(); | ||
list.add("123"); | ||
list.add("123"); | ||
list.add("123"); | ||
list.add("123"); | ||
list.add("123"); | ||
list.add("123"); | ||
|
||
list.add(1, 111); | ||
|
||
list.remove(1); | ||
|
||
System.out.println(list.baseArr.length); | ||
System.out.println(list.size()); | ||
|
||
for (int i = 0; i < list.size(); i++) { | ||
Object object = list.get(i); | ||
System.out.println(object); | ||
} | ||
System.out.println("ArrayList 测试 --结束"); | ||
} | ||
|
||
/** | ||
* LinkedList 测试 | ||
*/ | ||
public static void testLinkedList(){ | ||
System.out.println("LinkedList 测试 --开始"); | ||
LinkedList list = new LinkedList(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.add(4); | ||
list.add(5); | ||
list.add(6); | ||
|
||
list.add(1, 111); | ||
|
||
list.remove(1); | ||
|
||
System.out.println(list.size()); | ||
|
||
for (int i = 0; i < list.size(); i++) { | ||
Object object = list.get(i); | ||
System.out.println(object); | ||
} | ||
System.out.println("LinkedList 测试 --结束"); | ||
} | ||
|
||
} |
Oops, something went wrong.