Проект Java - переопределить метод toString() и вернуть строку

Мой класс начал работать с очередями, и наши инструкции заключаются в том, чтобы переопределить метод toString() и вернуть строку. Без использования метода по умолчанию я не знаю, как преобразовать массив в строковую форму [элемент, элемент, элемент] для возврата. Вот мой код до сих пор. Моя очередь работает нормально, и мне не нужны какие-либо данные по этому поводу... просто возвращается метод toString().

package edu.ben.cis205;

public class MyQueue {

    //Create array
    public int[] array = new int[10];

    //Create variables to track number of elements and pointer positions
    private int first = 0;
    private int last = 0;
    private int numberOfElements = 0;

    public int peek() {
        //Returns first element in array
        if (numberOfElements > 0)
            return array[first];
        else {
            System.out.println("No integers in array.");
            return 0;
        }

    }

    public boolean add(int inputNumber) {
        //Adds input to array
        //Checks for room at back of array
        if (numberOfElements < array.length && last < array.length) {
            array[last] = inputNumber;
            last++;
            numberOfElements++;
            return true;
        //Checks for room at front of array
        } else if (numberOfElements < array.length && last == array.length) {
            last = 0;
            array[last] = inputNumber;
            last++;
            numberOfElements++;
            return true;
        } else {
            return false;
        }

    }

    public int getSize() {
        //Returns number of elements in array
        return numberOfElements;
    }

    public boolean isFull() {
        //Returns true if full
        if (numberOfElements == array.length)
            return true;
        else
            return false;
    }

    public boolean isEmpty() {
        //Returns true if array is empty
        if (numberOfElements == 0)
            return true;
        else
            return false;
    }

    public int remove() {
        //Removes element at front of array
        //Checks for elements and moves pointer to next array position
        if (numberOfElements > 0 && first < array.length) {
            int returnValue = array[first];
            first++;
            numberOfElements--;
            return returnValue;
        //Checks for elements and moves pointer to front if at final position
        } else if (numberOfElements > 0 && first == array.length) {
            first = 0;
            int returnValue = array[first];
            first++;
            numberOfElements--;
            return returnValue;
        //Returns an int value of 0 if array is empty
        } else {
            return 0;
        }
    }

    public int getCapacity() {
        //Returns array size
        return array.length;

    }

    public int getRemainingCapacity() {
        //Returns remaining spaces in array
        return array.length - numberOfElements;
    }

    //ERROR IN METHOD
    public String toString() {

        //Int establishes where to start in the array.
        int arrayPointer = first;

        //New array established to put queue in order.
        int stringArray[] = new int[numberOfElements];

        //This code reconstructs the queue in the correct order
        if (numberOfElements != 0) {
            for (int i = 0; i < numberOfElements; i++) {
                stringArray[i] = array[arrayPointer];

                if (arrayPointer < (array.length - 1))
                    arrayPointer++;
                else 
                    arrayPointer = 0;
            }
        } else
            return null;

        // ??? Do not know how to change the new array (stored now in the correct order) into a String return...
    }
}

person Juice Cagnasty    schedule 22.09.2013    source источник


Ответы (1)


Использовать

Arrays.toString (массив[])

чтобы получить ваш массив в представлении [item1, item2, item3]

person Srikanth Reddy Lingala    schedule 22.09.2013