Skip to content

Commit 659fc03

Browse files
authored
Merge pull request onlyliuxin#23 from Greastate/master
八组作业提交
2 parents 55f2072 + 3798466 commit 659fc03

40 files changed

+1784
-5
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## cpu 内存 硬盘 指令之间的关系
2+
> 计算机整体分为软件和硬件。
3+
> 软件是由指令和数据组成的程序,硬件包含主板、CPU、内存、硬盘等
4+
5+
6+
指令指的是计算机可以识别的机器语言,每个操作系统都有内置的指令集,计算机可根据不同的指令来驱动计算机工作或是相应的输出。指令是以二进制的形式存储在硬盘中,当CPU执行某个指令的时候,需要将指令先从硬盘加载到内存,CPU从内存中获取执行解释执行。
7+
8+
CPU是计算的核心部件,相当于人类的大脑。CPU执行指令的时候其实不关心也不知道指令的意思,只是按照指令执行。由于硬盘读写性能比较差,CPU从硬盘中直接读取数据的时候比较费时,就有的内存的存在。内存比硬盘读写速度快很多,CPU要执行程序时,先将程序加载到内存中在执行。
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.my.list;
2+
3+
/**
4+
* 实现ArrayList
5+
*
6+
*/
7+
public class ArrayList {
8+
//初始化容量
9+
private static final int INIT = 5;
10+
//增长因子
11+
private static final double GROWTH = 0.5;
12+
13+
//初始化数组
14+
Object[] baseArr = new Object[INIT];
15+
//当前下标
16+
int currentIndex = 0;
17+
//最大下标
18+
int maxIndex = INIT-1;
19+
20+
/**
21+
* 添加元素
22+
* @param obj
23+
* @return
24+
*/
25+
public Object add(Object obj){
26+
if(judgeCapacity()){
27+
baseArr = arrayDilatation(baseArr, GROWTH);
28+
maxIndex = baseArr.length-1;
29+
}
30+
baseArr[currentIndex] = obj;
31+
++currentIndex;
32+
return obj;
33+
}
34+
35+
/**
36+
* 指定下标添加元素
37+
* @param index
38+
* @param obj
39+
* @return
40+
*/
41+
public Object add(int index , Object obj){
42+
if (index < 0 || index > currentIndex) {
43+
throw new IndexOutOfBoundsException();
44+
}
45+
Object[] dest = new Object[currentIndex+1];
46+
System.arraycopy(baseArr, 0, dest, 0, index);
47+
dest[index] = obj;
48+
System.arraycopy(baseArr, index, dest, index+1, currentIndex-index);
49+
++currentIndex;
50+
baseArr = dest;
51+
return obj;
52+
}
53+
54+
/**
55+
* 指定下标删除元素
56+
* @param index
57+
* @return
58+
*/
59+
public Object remove(int index){
60+
if (index < 0 || index >= currentIndex) {
61+
throw new IndexOutOfBoundsException();
62+
}
63+
Object object = baseArr[index];
64+
Object[] dest = new Object[currentIndex];
65+
System.arraycopy(baseArr, 0, dest, 0, index);
66+
System.arraycopy(baseArr, index+1, dest, index, currentIndex-index-1);
67+
--currentIndex;
68+
baseArr = dest;
69+
return object;
70+
}
71+
72+
/**
73+
* 根据下标获取元素
74+
* @param index
75+
* @return
76+
*/
77+
public Object get(int index){
78+
79+
return baseArr[index];
80+
}
81+
82+
/**
83+
* 获取集合大小
84+
* @return
85+
*/
86+
public int size(){
87+
return currentIndex;
88+
}
89+
90+
/**
91+
* 判断容量是否需要增加
92+
* @return
93+
*/
94+
public boolean judgeCapacity(){
95+
if(currentIndex > maxIndex){
96+
return true;
97+
}
98+
return false;
99+
}
100+
101+
/**
102+
* 数组扩容
103+
* @param objArr
104+
* @param groth
105+
* @return
106+
*/
107+
public static Object[] arrayDilatation(Object[] objArr , double groth){
108+
int length = objArr.length;
109+
int newLength = (int) (length * groth + length);
110+
Object[] baseArr = new Object[newLength];
111+
System.arraycopy(objArr, 0, baseArr, 0, length);
112+
return baseArr;
113+
}
114+
115+
116+
}
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+
130+
131+
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package com.my.list;
2+
3+
/**
4+
* 实现 LinkedList
5+
*
6+
*/
7+
public class LinkedList {
8+
9+
//首节点
10+
Node first = new Node();
11+
//集合大小
12+
int size = 0;
13+
14+
/**
15+
* 添加元素
16+
* @param object
17+
* @return
18+
*/
19+
public Object add(Object object){
20+
if (size == 0) {
21+
first.setObject(object);
22+
}else{
23+
Node previous = first;
24+
Node temp = first.getNext();
25+
while (temp != null) {
26+
previous = temp;
27+
temp = temp.getNext();
28+
}
29+
Node node = new Node();
30+
node.setObject(object);
31+
previous.setNext(node);
32+
}
33+
++size;
34+
return object;
35+
}
36+
37+
/**
38+
* 根据下标添加元素
39+
* @param index
40+
* @param object
41+
* @return
42+
*/
43+
public Object add(int index , Object object){
44+
if (index < 0 || index > size) {
45+
throw new IndexOutOfBoundsException();
46+
}
47+
48+
if (size == 0) {
49+
first.setObject(object);
50+
}else{
51+
if (index == 0) {
52+
Node temp = new Node();
53+
temp.setObject(object);
54+
temp.setNext(first);
55+
first = temp;
56+
}else{
57+
int count = 1;
58+
Node temp = first;
59+
while (temp != null) {
60+
if (count++ == index) {
61+
Node next = temp.getNext();
62+
Node node = new Node();
63+
temp.setNext(node);
64+
node.setObject(object);
65+
node.setNext(next);
66+
break;
67+
}
68+
temp = temp.getNext();
69+
}
70+
}
71+
}
72+
++size;
73+
return object;
74+
}
75+
76+
/**
77+
* 根据下标删除元素
78+
* @param index
79+
* @return
80+
*/
81+
public Object remove(int index){
82+
if (index < 0 || index >= size) {
83+
throw new IndexOutOfBoundsException();
84+
}
85+
Node node = null;
86+
if (index == 0) {
87+
Node next = first.getNext();
88+
first = next;
89+
node = first;
90+
}else{
91+
int count = 1;
92+
Node temp = first;
93+
while (temp != null) {
94+
if (count++ == index) {
95+
node = temp.getNext();
96+
Node next = node.getNext();
97+
temp.setNext(next);
98+
break;
99+
}
100+
temp = temp.getNext();
101+
}
102+
}
103+
--size;
104+
return node.getObject();
105+
}
106+
107+
/**
108+
* 根据下标获取元素
109+
* @param index
110+
* @return
111+
*/
112+
public Object get(int index) {
113+
Node temp = first;
114+
int count = 0;
115+
while (temp != null) {
116+
if (count++ == index) {
117+
break;
118+
}
119+
temp = temp.getNext();
120+
}
121+
return temp.getObject();
122+
}
123+
124+
/**
125+
* 获取集合大小
126+
* @return
127+
*/
128+
public int size() {
129+
return size;
130+
}
131+
132+
133+
}
134+
135+
136+
/**
137+
* 节点元素
138+
*
139+
*/
140+
class Node{
141+
private Object object ;
142+
private Node next;
143+
public Object getObject() {
144+
return object;
145+
}
146+
public void setObject(Object object) {
147+
this.object = object;
148+
}
149+
public Node getNext() {
150+
return next;
151+
}
152+
public void setNext(Node next) {
153+
this.next = next;
154+
}
155+
156+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.my.list;
2+
3+
public class Test {
4+
5+
public static void main(String[] args) {
6+
7+
testArrayList();
8+
System.out.println("--------------------------------");
9+
testLinkedList();
10+
}
11+
12+
/**
13+
* ArrayList 测试
14+
*/
15+
public static void testArrayList(){
16+
System.out.println("ArrayList 测试 --开始");
17+
ArrayList list = new ArrayList();
18+
list.add("123");
19+
list.add("123");
20+
list.add("123");
21+
list.add("123");
22+
list.add("123");
23+
list.add("123");
24+
25+
list.add(1, 111);
26+
27+
list.remove(1);
28+
29+
System.out.println(list.baseArr.length);
30+
System.out.println(list.size());
31+
32+
for (int i = 0; i < list.size(); i++) {
33+
Object object = list.get(i);
34+
System.out.println(object);
35+
}
36+
System.out.println("ArrayList 测试 --结束");
37+
}
38+
39+
/**
40+
* LinkedList 测试
41+
*/
42+
public static void testLinkedList(){
43+
System.out.println("LinkedList 测试 --开始");
44+
LinkedList list = new LinkedList();
45+
list.add(1);
46+
list.add(2);
47+
list.add(3);
48+
list.add(4);
49+
list.add(5);
50+
list.add(6);
51+
52+
list.add(1, 111);
53+
54+
list.remove(1);
55+
56+
System.out.println(list.size());
57+
58+
for (int i = 0; i < list.size(); i++) {
59+
Object object = list.get(i);
60+
System.out.println(object);
61+
}
62+
System.out.println("LinkedList 测试 --结束");
63+
}
64+
65+
}

0 commit comments

Comments
 (0)