Skip to content

Commit d0341c6

Browse files
committed
[feat]程序设计题更新
1 parent 059c5b9 commit d0341c6

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

docs/java/Java程序设计题.md renamed to docs/java/java-programming-problem/Java程序设计题.md

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
## 泛型的实际应用
1+
<!-- TOC -->
22

3-
### 实现最小值函数
3+
- [0.0.1. 泛型的实际应用:实现最小值函数](#001-%e6%b3%9b%e5%9e%8b%e7%9a%84%e5%ae%9e%e9%99%85%e5%ba%94%e7%94%a8%e5%ae%9e%e7%8e%b0%e6%9c%80%e5%b0%8f%e5%80%bc%e5%87%bd%e6%95%b0)
4+
- [0.0.2. 使用数组实现栈](#002-%e4%bd%bf%e7%94%a8%e6%95%b0%e7%bb%84%e5%ae%9e%e7%8e%b0%e6%a0%88)
5+
- [0.0.3. 实现线程安全的 LRU 缓存](#003-%e5%ae%9e%e7%8e%b0%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e7%9a%84-lru-%e7%bc%93%e5%ad%98)
6+
7+
<!-- /TOC -->
8+
9+
### 0.0.1. 泛型的实际应用:实现最小值函数
410

511
自己设计一个泛型的获取数组最小值的函数.并且这个方法只能接受Number的子类并且实现了Comparable接口。
612

@@ -23,10 +29,7 @@ int minInteger = min(new Integer[]{1, 2, 3});//result:1
2329
double minDouble = min(new Double[]{1.2, 2.2, -1d});//result:-1d
2430
String typeError = min(new String[]{"1","3"});//报错
2531
```
26-
27-
## 数据结构
28-
29-
### 使用数组实现栈
32+
### 0.0.2. 使用数组实现栈
3033

3134
**自己实现一个栈,要求这个栈具有`push()``pop()`(返回栈顶元素并出栈)、`peek()` (返回栈顶元素不出栈)、`isEmpty()``size()`这些基本的方法。**
3235

@@ -39,14 +42,14 @@ public class MyStack {
3942
private int count;//栈中元素数量
4043
private static final int GROW_FACTOR = 2;
4144

42-
//TODO:不带初始容量的构造方法。默认容量为8
45+
//不带初始容量的构造方法。默认容量为8
4346
public MyStack() {
4447
this.capacity = 8;
4548
this.storage=new int[8];
4649
this.count = 0;
4750
}
4851

49-
//TODO:带初始容量的构造方法
52+
//带初始容量的构造方法
5053
public MyStack(int initialCapacity) {
5154
if (initialCapacity < 1)
5255
throw new IllegalArgumentException("Capacity too small.");
@@ -56,30 +59,30 @@ public class MyStack {
5659
this.count = 0;
5760
}
5861

59-
//TODO:入栈
62+
//入栈
6063
public void push(int value) {
6164
if (count == capacity) {
6265
ensureCapacity();
6366
}
6467
storage[count++] = value;
6568
}
6669

67-
//TODO:确保容量大小
70+
//确保容量大小
6871
private void ensureCapacity() {
6972
int newCapacity = capacity * GROW_FACTOR;
7073
storage = Arrays.copyOf(storage, newCapacity);
7174
capacity = newCapacity;
7275
}
7376

74-
//TODO:返回栈顶元素并出栈
77+
//返回栈顶元素并出栈
7578
private int pop() {
7679
if (count == 0)
7780
throw new IllegalArgumentException("Stack is empty.");
7881
count--;
7982
return storage[count];
8083
}
8184

82-
//TODO:返回栈顶元素不出栈
85+
//返回栈顶元素不出栈
8386
private int peek() {
8487
if (count == 0){
8588
throw new IllegalArgumentException("Stack is empty.");
@@ -88,12 +91,12 @@ public class MyStack {
8891
}
8992
}
9093

91-
//TODO:判断栈是否为空
94+
//判断栈是否为空
9295
private boolean isEmpty() {
9396
return count == 0;
9497
}
9598

96-
//TODO:返回栈中元素的个数
99+
//返回栈中元素的个数
97100
private int size() {
98101
return count;
99102
}
@@ -121,4 +124,7 @@ for (int i = 0; i < 8; i++) {
121124
}
122125
System.out.println(myStack.isEmpty());//true
123126
myStack.pop();//报错:java.lang.IllegalArgumentException: Stack is empty.
124-
```
127+
```
128+
129+
130+
Loading
Loading

0 commit comments

Comments
 (0)