Java、对链表中的数字进行排序

 

package collection;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

public class Exercise20_02 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        TextField number = new TextField();
        HBox topHBox = new HBox(10, new Label("Enter a number:"), number);  //顶部
        topHBox.setAlignment(Pos.CENTER);

        TextArea textArea = new TextArea(); //中部
        textArea.setPrefRowCount(6);
        textArea.setPrefColumnCount(31);
        textArea.setEditable(false);

        Button sort = new Button("Sort");
        Button shuffle = new Button("Shuffle");
        Button reverse = new Button("Reverse");
        HBox bottomHBox = new HBox(10, sort, shuffle, reverse); //底部
        bottomHBox.setAlignment(Pos.CENTER);

        LinkedList<Integer> linkedList = new LinkedList<>();
        number.setOnAction(event -> {   //按钮注册动作事件
            try {
                add(linkedList, Integer.parseInt(number.getText()));
                addText(linkedList, textArea);
                number.setText("");
            } catch (NumberFormatException ex) {
            }
        });
        sort.setOnAction(event -> {
            Collections.sort(linkedList);
            addText(linkedList, textArea);
        });
        shuffle.setOnAction(event -> {
            Collections.shuffle(linkedList);
            addText(linkedList, textArea);
        });
        reverse.setOnAction(event -> {
            Collections.reverse(linkedList);
            addText(linkedList, textArea);
        });

        BorderPane pane = new BorderPane(new ScrollPane(textArea)); //总面板
        pane.setTop(topHBox);
        pane.setBottom(bottomHBox);

        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Exercise20_02");
        primaryStage.show();
    }

    /** 不重复的值添加进链表 */
    private void add(LinkedList<Integer> linkedList, int value) {
        boolean isTheSame = false;
        for (Integer i : linkedList)    //遍历链表
            if (i.equals(value))    //如果值重复,变量设置为true
                isTheSame = true;
        if (!isTheSame) //值不重复时添加进链表
            linkedList.addLast(value);
    }

    /** 添加链表数据到文本区域 */
    private void addText(LinkedList<Integer> linkedList, TextArea textArea) {
        textArea.clear();   //清除原有文本区域数据
        Iterator<Integer> iterator = linkedList.iterator(); //返回链表迭代器
        while (iterator.hasNext())  //遍历迭代器,追加数据
            textArea.appendText(iterator.next() + " ");
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值