英文:
How to Delete elements from the bottom of Stack in Java?
问题
在Java中,如何从堆栈底部删除元素?我应该使用临时堆栈,将每个元素弹出到临时堆栈中以颠倒堆栈,然后弹出第一个元素,还是将其视为列表会更容易?我正尝试从堆栈底部删除前n/2个元素,其中n是堆栈中元素的数量。这里我只考虑整数值。
英文:
In Java, how to delete element from the bottom of the stack? Should I use a temporary stack and pop every element into it to reverse the stack and then pop the first element or would treating it as a list would be easier? I am trying to delete first n/2 elements from the bottom of the stack where n is the number of element in the stack. here I am taking only Integer values.
答案1
得分: 0
你可以在Java中使用Deque。它支持在两侧进行插入和删除操作。
例如:
Deque<Integer> deque = new ArrayDeque<>(Arrays.asList(1,2,3,4));
int count = deque.size() / 2;
while (count > 0)
{
deque.removeLast();
count--;
}
你也可以使用linkedList作为Deque的实现。LinkedList和ArrayDeque之间的区别:
https://stackoverflow.com/questions/6163166/why-is-arraydeque-better-than-linkedlist#:~:text=Key%20differences%3A,LinkedList%20but%20not%20in%20ArrayDeque&text=The%20LinkedList%20implementation%20consumes%20more%20memory%20than%20the%20ArrayDeque
更多详细信息请参见:
https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html
英文:
You can use Deque in java. It supports both insertion and deletion on both sides.
e.g.
Deque<Integer> deque = new ArrayDeque<>(Arrays.asList(1,2,3,4));
int count = deque.size() / 2;
while (count > 0)
{
deque.removeLast();
count--;
}
You can use linkedList as implementation of Deque instead. The difference between LinkedList and ArrayDeque:
https://stackoverflow.com/questions/6163166/why-is-arraydeque-better-than-linkedlist#:~:text=Key%20differences%3A,LinkedList%20but%20not%20in%20ArrayDeque&text=The%20LinkedList%20implementation%20consumes%20more%20memory%20than%20the%20ArrayDeque
More details at
https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html
答案2
得分: 0
根据您描述的用例,我认为您需要一种不同的数据结构。
我建议您查阅双端队列,或 Deque
,它自从 Java 1.6 版本以来就成为了 Java Collections API 的一部分。
这种数据结构有点像是栈和队列的混合体,允许您从任一端添加/移除元素。
您可以使用 addFirst
和 removeFirst
来执行传统的栈操作,即推入和弹出。
要从尾部 - 或者如您所说的 'Stack' 的 'bottom' - 移除元素,您可以使用 removeLast
。
size
方法会给您元素的数量。
英文:
Based on your described use case, I think you need a different data structure.
I would recommend you to review the double-ended queue, or Deque
, which has been part of the Java Collections API since Java 1.6.
This data structure is a bit of a hybrid between a Stack and a Queue, permitting you to add/remove elements to/from either end.
You can perform the traditional Stack operations of push and pop using addFirst
and removeFirst
, respectively.
To remove from the tail - or 'bottom' of the 'Stack', as you put it - you can use removeLast
.
The size
method will give you the number of elements.
答案3
得分: 0
我明白你的问题,最简单的解决方法是使用remove(int i)函数。
这个函数需要一个整数作为参数,
Stack<Integer> = new Stack<>();
//假设栈是[1,2,3,4,5,6,7,8]
首先使用size()来获取栈的大小。要删除前n/2个元素 -
int size = (int) Math.floor(stack.size()/2)
现在,从0到size运行一个循环
要小心,每次都删除第一个元素,因为栈会随着时间的推移而更新
for(int i = 0; i<size ; i++) {
stack.remove(0);
}
打印剩余的栈
System.out.println(stack)
//[5,6,7,8]
我希望这解决了你的疑问。
英文:
I understand your problem and easiest solution for this is to use remove(int i) function.
This takes int as a parameter,
Stack<Integer> = new Stack<>();
//suppose stack is [1,2,3,4,5,6,7,8]
First use size() to get stack size. To delete first n/2 elements -
int size = (int) Math.floor(stack.size()/2)
Now, run a loop from 0 to size
Be carefull always delete first element as every time stack gets updated
for(int i = 0; i<size ; i++) {
stack.remove(0);
}
Print the remaining stack
System.out.println(stack)
//[5,6,7,8]
I hope this solves your doubt.
专注分享java语言的经验与见解,让所有开发者获益!
评论