-1

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? #noob

I posted my boolean the push method for @NoStupidQuestions :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

deHaar
  • 17,687
  • 10
  • 38
  • 51
Muriel
  • 27
  • 6
  • Please look at a basic tutorial, e.g. the [Java Tutorials on Methods by Oracle](https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html). – Turing85 Feb 09 '19 at 17:24
  • https://idownvotedbecau.se/noresearch/, Please see: [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Turing85 Feb 09 '19 at 17:28

1 Answers1

0

You have 2 options here:

  1. Return the array, you put in at first by changing your function toArray to this:
@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:
@Override
public void toArray(Object[] a) {
    for (int i = 0; i < a.length; i++) {
        System.out.println(a[i]);
    }
}
ThexXTURBOXx
  • 381
  • 1
  • 3
  • 16
  • Thank you! I did the same; returned the a, but wasnt sure if it was the right way :D – Muriel Feb 09 '19 at 17:17
  • @Muriel Well, you could do it, but if you don't use the returned value after that, it's superfluous, so you can choose ;) – ThexXTURBOXx Feb 09 '19 at 17:18
  • And cant use the length I think. Because its a indexed twostack ( left 0 right n-1), so numberOfElement keeps track – Muriel Feb 09 '19 at 17:19
  • Yeah, you can just change that out. I just thought, for generality purposes, I will write that there – ThexXTURBOXx Feb 09 '19 at 17:20