英文:
Any alternate way to implement the peek method?
问题
I understand your request, and I'll provide the translation of the code and your explanation without additional content. Here's the translated code and your explanation:
public class Stack {
int[] arr;
int top = -1;
int currSize = 10;
/* Constructor */
public Stack() {
arr = new int[10];
}
public void push(int data) {
if (top == arr.length - 1) {
resize();
}
top++;
arr[top] = data;
}
public int peek() {
try {
return arr[top];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Stack is empty");
}
return Integer.MIN_VALUE;
}
/* Resizes the array when space runs out */
public void resize() {
int[] newArr = new int[2 * currSize];
currSize = 2 * currSize;
for (int i = 0; i < newArr.length / 2; i++) {
newArr[i] = arr[i];
}
arr = newArr;
newArr = null;
}
/* Show elements in stack */
public void show() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + " -> ");
}
System.out.println();
}
/* Return the number of elements in stack */
public int getSize() {
return top + 1;
}
public static void main(String[] args) {
Stack st = new Stack();
System.out.println(st.peek());
}
}
I tried omitting the Integer.MIN_VALUE
return statement, but the program wouldn't compile.
I was expecting that after the message in the catch
block had been printed, the control would return from the peek
method to the main
method and resume executing the other statements in the main method (probably any push operations).
A solution that occurs to me is that I change the peek
method return type to void
, print the top-most value, and then return null
, but I don't want to take this path because I may want to store the return value and use it in some other parts of the program.
英文:
I am learning data structures on my own and have been trying to implement them using Java. Currently I am attempting to implement a array based stack. In the peek
method when the stack is empty I am currently catching ArrayIndexOutOfBoundsException
and then returning Integer.MIN_VALUE
to stop the program from quitting and getting it ready for further push operations. Is there any other way to implement the peek
method so that it just prints the message inside the catch
block without returning the Integer.MIN_VALUE.
public class Stack {
int[] arr;
int top = -1;
int currSize = 10;
/* Constructor */
public Stack() {
arr = new int[10];
}
public void push(int data) {
if (top == arr.length - 1) {
resize();
}
top++;
arr[top] = data;
}
/*
public void pop() {
arr[top] = 0;
top--;
}
*/
public int peek() {
try {
return arr[top];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Stack is empty");
}
return Integer.MIN_VALUE;
}
/* Resizes the array when space runs out */
public void resize() {
int[] newArr = new int[2 * currSize];
currSize = 2 * currSize;
for (int i = 0; i < newArr.length / 2; i++) {
newArr[i] = arr[i];
}
arr = newArr;
newArr = null;
}
/* Show elements in stack */
public void show() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + " -> ");
}
System.out.println();
}
/* Rturn the no of elements in stack */
public int getSize() {
return top + 1;
}
public static void main(String[] args) {
Stack st = new Stack();
System.out.println(st.peek());
}
}
I tried omitting the Integer.MIN_VALUE
return statement but the program wouldn't compile.
I was expecting that after the message in catch
block had been printed the control would return from the peek
method to the main
method and resume executing the other statements in main method (probably any push operations).
A solution which occurs to me is the I change the peek
method return type to void, print the top most value and the return null
but I don't want to take this path because I may want to store the return value and use it in some other parts of program.
答案1
得分: 4
Yes. Throw an Exception
. You could simply throw the one you are currently catching.
将其翻译为:是的。抛出一个异常
。您可以简单地抛出您当前正在捕获的异常。
英文:
Yes. Throw an Exception
. You could simply throw the one you are currently catching.
public int peek() throws ArrayIndexOutOfBoundsException {
return arr[top];
}
You could return an Optional<Integer>
like
public Optional<Integer> peek() {
try {
return Optional.of(arr[top]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Stack is empty");
}
return Optional.empty();
}
Or you could return an Integer
and make it null
. Warning, this is possibly a billion dollar mistake<sup>1</sup>. Like,
public Integer peek() {
try {
return arr[top];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Stack is empty");
}
return null;
}
<sup>1</sup><sub>Null References The Billion Dollar Mistake</sub>
答案2
得分: 1
我明白你的意思;只有在有值的情况下返回一个值,否则打印一条消息。
这就是异常实际上非常有用的地方。
让你的方法抛出一个异常,并在调用 peek
方法的地方捕获它。
public int peek() throws ArrayIndexOutOfBoundsException {
return arr[top];
}
void example() {
int value;
try {
value = stack.peek();
} catch (ArrayIndexOutOfBoundsException exception) {
System.out.println("stack is empty");
}
}
这涉及重新排序和重新思考你的控制流。对于这样的情况,通常是在基本层面上,很容易实现。
英文:
I see what you're saying; only return a value if there is one, otherwise print a message.
This is where exceptions are actually useful.
Have your method throw an exception, and catch it wherever you are calling the peek
method.
public int peek() throws ArrayIndexOutOfBoundsException {
return arr[top];
}
void example() {
int value;
try {
value = stack.peek();
} catch (ArrayIndexOutOfBoundsException exception) {
System.out.println("stack is empty");
}
}
It's a matter of re-ordering and re-thinking your control-flow.
Which for a situation like this, is typically at ground-level, and easy to implement.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论