简体   繁体   English

最后如何修复 toArray 方法?

[英]How can I fix the toArray method in the end?

So earlier I asked a question on how to use boolean values to access the different end of my stack.所以早些时候我问了一个关于如何使用布尔值访问堆栈的不同端的问题。 So I fixed this :)所以我解决了这个问题:)

Were I am stuck now is at the end;我现在被困在最后; I dont know how to implement the toArray method from the interface...ant comments or hints on how to fix it?我不知道如何从接口实现 toArray 方法...蚂蚁评论或提示如何修复它? #noob #小白

I posted my boolean the push method for @NoStupidQuestions :D我发布了我的布尔值 @NoStupidQuestions 的 push 方法:D

public class TwoStackArray<E> implements Twostack<E> {

    // lots of code emitted...

    @Override
    public void push(Boolean right, E element) throws TwostackFullException {
        if (numberOfElement == size - 1) {
            throw new TwostackFullException("Stack overflow");
        }

        if (right) {
            arr[rightIndex] = element;
            rightIndex--;
        } else {
            arr[leftIndex] = element;
            leftIndex++;
        }

        numberOfElement++;
    }

    // lots of code emitted......................

    @Override
    public <T> T[] toArray(T[] a) {
        for (int i = 0; i < numberOfElement; i++) {
            System.out.println(arr[i]);
        }
    }

}

IJ tells me the last method lacks return...but I just guessed a for loop/print method here...not sure how to solve this IJ 告诉我最后一个方法缺少 return ......但我只是在这里猜测了一个 for 循环/打印方法......不知道如何解决这个问题

You have 2 options here:您在这里有 2 个选择:

  1. Return the array, you put in at first by changing your function toArray to this:返回数组,您首先通过将函数 toArray 更改为:
@Override
public <T> T[] toArray(T[] a) {
    for (int i = 0; i < a.length; i++) {
        System.out.println(a[i]);
    }
    return a;
}
  1. You can just change the return type to void:您可以将返回类型更改为 void:
@Override
public void toArray(Object[] a) {
    for (int i = 0; i < a.length; i++) {
        System.out.println(a[i]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何解决这个问题呢? - How can i fix this method 如果未指定方法 toArray,如何使用 toArray() 将哈希集转换为数组? - How to convert hash Set into array using toArray() if the method toArray is not specified? 如何解决意外的文档结束异常 - How can I fix a unexpected End of Document Exception 如何使用 Lambda 参数为 ArrayList 实现 toArray() 方法? - How to implement toArray() method for ArrayList with Lambda parameter? 如何修复ObjectInputStream和ObjectOutputStream的“write end dead”和“read end dead”错误? - How can I fix my “write end dead” and “read end dead” errors for ObjectInputStream and ObjectOutputStream? 如何使该方法结束递归并达到最大值? - How can I make this method end its recursion and reach a maximum? 我该如何解决这个问题:“无法解析‘EmployeeRepository’中的方法‘getById’”? - How can I fix the issue: "Cannot resolve method 'getById' in 'EmployeeRepository'"? 我的联合方法不起作用,如何解决? - My Union Method is not working, How can I fix it? 如何修复我的 toString 方法中的输出? - How can I fix my output in my toString method? 如何使用BigInteger在多项式类中修复此“ plus”方法 - How can I fix this “plus” method in Polynomial class using BigInteger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM