为队列创建toString

时间:2017-12-01 22:17:48

标签: java queue nodes tostring

我正在尝试为队列类创建一个toString方法,该队列类以数组的格式返回队列的字符串,队列中的最后一个值是字符串中的第一个。例如,如果按顺序将1,2,3,4,5添加到队列中,则toString方法将返回[5,4,3,2,1]。

我已经尝试过这样做,但我似乎无法获得打印的最后一个值。

这是我目前拥有的toString方法

    public String toString( ) {
    if ( isEmpty() ) {
        return "[]";
    } else {

        String build = "";

        Node current = first;
        while ( current.getNext() != null ) {
            build = current.getElement() + ", " + build;
            current = current.getNext();
        }

        return "[" + build + "]";
    }
}

}

当我按如下方式进行测试时:

public class test {

public static void main( String[] args ){

    Queue q = new Queue(5);

    try {
        q.enqueue(1);
        q.enqueue(2);
        q.enqueue(3);
        q.enqueue(4);
        q.enqueue(5);

        System.out.println(q.toString());

    } catch (QueueFullException e ) {
        e.printStackTrace();
    }
}

}

我一直在:

[4, 3, 2, 1, ]

非常感谢任何帮助。感谢

3 个答案:

答案 0 :(得分:0)

请尝试执行以下操作

// Add the custom field "favorite_color"
add_action( 'woocommerce_edit_account_form', 'add_favorite_color_to_edit_account_form' );
function add_favorite_color_to_edit_account_form() {
    $user = wp_get_current_user();
    ?>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="favorite_color"><?php _e( 'Favorite color', 'woocommerce' ); ?>
        <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="favorite_color" id="favorite_color" value="<?php echo esc_attr( $user->favorite_color ); ?>" />
    </p>
    <?php
}

// Save the custom field 'favorite_color' 
add_action( 'woocommerce_save_account_details', 'save_favorite_color_account_details', 12, 1 );
function save_favorite_color_account_details( $user_id ) {
    // For Favorite color
    if( isset( $_POST['favorite_color'] ) )
        update_user_meta( $user_id, 'favorite_color', sanitize_text_field( $_POST['favorite_color'] ) );

    // For Billing email (added related to your comment)
    if( isset( $_POST['account_email'] ) )
        update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['account_email'] ) );
}

因为在最后一个元素中没有下一个,但是你应该打印最后一个值

答案 1 :(得分:0)

这种方法可行:

@Override
public String toString ()
{
    String build = "";

    // I didn't want to make an assumption here - I don't know your implementation
    // If first is null when the queue is empty, then simplify this line
    Node current = (isEmpty()) ? null : first;

    while ( current != null )
    {
        String currElem = String.valueOf(current.getElement());

        // don't add the comma after the first element
        build = (build.length() == 0) ? currElem : currElem + ", " + build;

        current = current.getNext();
    }

    return "[" + build + "]";
}

注意:您不需要空队列的特殊情况 - 因为构建字符串将为空。

答案 2 :(得分:0)

您正在使用current.getNext()检查来遍历Queue元素。您可以使用

public String toString( ) {
if ( isEmpty() ) {
    return "[]";
} else {

    String build = "";

    Node current = first;
    while ( current != null ) {
        build = current.getElement() + ", " + build;
        current = current.getNext();
    }

    return "[" + build + "]";
}

这将解决您的问题。谢谢。