Skip to content

Commit 7881334

Browse files
committed
Create Java程序设计题.md
1 parent 2f0a993 commit 7881334

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

docs/java/Java程序设计题.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
## 泛型的实际应用
2+
3+
### 实现最小值函数
4+
5+
自己设计一个泛型的获取数组最小值的函数.并且这个方法只能接受Number的子类并且实现了Comparable接口。
6+
7+
```java
8+
//注意:Number并没有实现Comparable
9+
private static <T extends Number & Comparable<? super T>> T min(T[] values) {
10+
if (values == null || values.length == 0) return null;
11+
T min = values[0];
12+
for (int i = 1; i < values.length; i++) {
13+
if (min.compareTo(values[i]) > 0) min = values[i];
14+
}
15+
return min;
16+
}
17+
```
18+
19+
测试:
20+
21+
```java
22+
int minInteger = min(new Integer[]{1, 2, 3});//result:1
23+
double minDouble = min(new Double[]{1.2, 2.2, -1d});//result:-1d
24+
String typeError = min(new String[]{"1","3"});//报错
25+
```
26+
27+
## 数据结构
28+
29+
### 使用数组实现栈
30+
31+
**自己实现一个栈,要求这个栈具有`push()``pop()`(返回栈顶元素并出栈)、`peek()` (返回栈顶元素不出栈)、`isEmpty()``size()`这些基本的方法。**
32+
33+
提示:每次入栈之前先判断栈的容量是否够用,如果不够用就用`Arrays.copyOf()`进行扩容;
34+
35+
```java
36+
public class MyStack {
37+
private int[] storage;//存放栈中元素的数组
38+
private int capacity;//栈的容量
39+
private int count;//栈中元素数量
40+
private static final int GROW_FACTOR = 2;
41+
42+
//TODO:不带初始容量的构造方法。默认容量为8
43+
public MyStack() {
44+
this.capacity = 8;
45+
this.storage=new int[8];
46+
this.count = 0;
47+
}
48+
49+
//TODO:带初始容量的构造方法
50+
public MyStack(int initialCapacity) {
51+
if (initialCapacity < 1)
52+
throw new IllegalArgumentException("Capacity too small.");
53+
54+
this.capacity = initialCapacity;
55+
this.storage = new int[initialCapacity];
56+
this.count = 0;
57+
}
58+
59+
//TODO:入栈
60+
public void push(int value) {
61+
if (count == capacity) {
62+
ensureCapacity();
63+
}
64+
storage[count++] = value;
65+
}
66+
67+
//TODO:确保容量大小
68+
private void ensureCapacity() {
69+
int newCapacity = capacity * GROW_FACTOR;
70+
storage = Arrays.copyOf(storage, newCapacity);
71+
capacity = newCapacity;
72+
}
73+
74+
//TODO:返回栈顶元素并出栈
75+
private int pop() {
76+
count--;
77+
if (count == -1)
78+
throw new IllegalArgumentException("Stack is empty.");
79+
80+
return storage[count];
81+
}
82+
83+
//TODO:返回栈顶元素不出栈
84+
private int peek() {
85+
if (count == 0){
86+
throw new IllegalArgumentException("Stack is empty.");
87+
}else {
88+
return storage[count-1];
89+
}
90+
}
91+
92+
//TODO:判断栈是否为空
93+
private boolean isEmpty() {
94+
return count == 0;
95+
}
96+
97+
//TODO:返回栈中元素的个数
98+
private int size() {
99+
return count;
100+
}
101+
102+
}
103+
104+
```
105+
106+
验证
107+
108+
```java
109+
MyStack myStack = new MyStack(3);
110+
myStack.push(1);
111+
myStack.push(2);
112+
myStack.push(3);
113+
myStack.push(4);
114+
myStack.push(5);
115+
myStack.push(6);
116+
myStack.push(7);
117+
myStack.push(8);
118+
System.out.println(myStack.peek());//8
119+
System.out.println(myStack.size());//8
120+
for (int i = 0; i < 8; i++) {
121+
System.out.println(myStack.pop());
122+
}
123+
System.out.println(myStack.isEmpty());//true
124+
myStack.pop();//报错:java.lang.IllegalArgumentException: Stack is empty.
125+
```

0 commit comments

Comments
 (0)