[英]Custom Java Generic ArrayList Initialization out of bounds
I'm working on a custom implementation of a generic ArrayList in Java.我正在对 Java 中的通用 ArrayList 进行自定义实现。 I'm not allowed to tamper with the main function.
我不允许篡改主 function。 How could I fix the initialization issue without messing with the main?
我怎样才能在不弄乱主程序的情况下解决初始化问题?
import java.util.ArrayList;
public class MyStack {
private ArrayList<Object> list;
MyStack() {
list = new ArrayList<Object>();
}
MyStack(ArrayList<Object> list) {
this.list = list;
}
MyStack(Object[] array) {
list = new ArrayList<Object>();
for (int i = 0; i < array.length; i++) {
list.add(array[i]);
}
}
MyStack(MyStack stack) {
list = new ArrayList<Object>();
for (int i = 0; i < stack.getSize(); i++) {
list.add(stack.get(i));
}
}
MyStack(int capacity) {
list = new ArrayList<Object>(capacity);
}
public Object get(int index) {
return list.get(index);
}
public boolean isEmpty() {
if(list.get(0) == null){
return true;
}
return false;
}
public void push(Object o) {
list.add(o);
}
public Object pop() {
int size = getSize();
Object a = this.list;
if(!isEmpty()){
Object o = list.get(list.size() - 1);
return o;
}
return a;
}
public Object peek() {
int size = getSize();
if(size <= 0){ return null; }
Object o = list.get(size-1);
return o;
}
public int getSize() {
return list.size();
}
@Override /** Override the toString in the Object class */
public String toString() {
return "stack: " + list.toString();
}
public static void main(String[] args) {
MyStack stack = new MyStack();
// get args from command line
for (int i = 0; i < args.length; i++) {
stack.push(args[i]);
}
System.out.println(stack);
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
}
}
It's a generic implementation of the ArrayList.它是 ArrayList 的通用实现。 When compiling I get this error:
编译时出现此错误:
stack: []
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:459)
at MyStack.isEmpty(MyStack.java:52)
at MyStack.main(MyStack.java:104)
I'm confused as to how index 0 would be out of bounds?我对索引 0 将如何越界感到困惑? What could I do to account for an ArrayList with 0 dimensions?
我能做些什么来解释一个尺寸为 0 的 ArrayList?
You seem to expect your ArrayList
to return null
if you ask for a nonexistent element ( get(0)
), but that's not the behavior;如果您要求不存在的元素(
get(0)
),您似乎希望您的ArrayList
返回null
,但这不是行为; it throws instead . 它会抛出。 To check whether a list is empty, you can use
list.isEmpty()
or list.size() == 0
.要检查列表是否为空,可以使用
list.isEmpty()
或list.size() == 0
。
public boolean isEmpty() {
if(list.get(0) == null){
return true;
}
return false;
}
If the list is empty, list.get(0) will throws IndexOutOfBound exception.如果列表为空,list.get(0) 将抛出 IndexOutOfBound 异常。
Why not just return list.isEmpty()?为什么不直接返回 list.isEmpty()?
public boolean isEmpty() {
return list.isEmpty();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.