繁体   English   中英

如何在Java中获得唯一字符串列表的随机排序?

[英]How can I get a random ordering of a list of unique Strings in Java?

我在ArrayList中有12个字符串。 我想在文本区域中随机显示这些字符串。 我已经到了字符串随机出现的位置,但有时会重复出现。 我不希望他们在所有12个字符串中都重复一遍。 到目前为止,这是我的代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * Layout of the application
 * 
 *  ###############################
 *  ##                           ##
 *  ##                           ##
 *  ##          #####            ##
 *  ##          #####            ##
 *  ##          #####            ##
 *  ##                           ##
 *  ###############################
 *  ##                           ##
 *  ##                           ##
 *  ##         Text Area         ##
 *  ##                           ##
 *  ##                           ##
 *  ##                           ##
 *  ###############################
 *  ##                           ##
 *  ##  #Fortune#       #Quit#   ##
 *  ##                           ##
 *  ###############################
 */

/**
 *
 * @author Kelle Schmitt
 */
public class FortuneTellerFrame extends JFrame
{
    public FortuneTellerFrame()
    {
    //set width and height of the window
    final int FRAME_HEIGHT = 400;
    final int FRAME_WIDTH = 400;

    //set width and height of the text area
    final int AREA_ROWS = 10;
    final int AREA_COLUMNS = 25;

    //declare the panels that are going to be used.
    final JPanel mainPnl, titlePnl, textPnl, controlPnl;
    final JLabel fortuneImage;

    //declare and find image for what your title/title image will be?
    ImageIcon icon = new ImageIcon("src/1804-1-bad-fortune-teller.jpg");
    fortuneImage = new JLabel("Fortune Teller!", icon, JLabel.CENTER);
    fortuneImage.setVerticalTextPosition(JLabel.TOP);
    fortuneImage.setHorizontalTextPosition(JLabel.CENTER);

    //declare the buttons that are going to be used.
    final JButton futureBtn, quitBtn;

    //create the main panel, and add the other panels to it
    mainPnl = new JPanel();
    mainPnl.setLayout(new BorderLayout());
    titlePnl = new JPanel();
    textPnl = new JPanel();
    controlPnl = new JPanel();

    mainPnl.add(titlePnl, BorderLayout.NORTH);
    mainPnl.add(textPnl, BorderLayout.CENTER);
    mainPnl.add(controlPnl, BorderLayout.SOUTH);

    //add the main panel to the parent frame
    add(mainPnl);
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //add other componets to the sub panel
    titlePnl.add(fortuneImage);

    final JTextArea fortuneArea;
    fortuneArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
    JScrollPane scrollPane = new JScrollPane(fortuneArea);
    fortuneArea.setText("I will tell the future...");
    textPnl.add(scrollPane);

    futureBtn = new JButton("Read my Fortune!");
    quitBtn = new JButton("Quit");

    controlPnl.add(futureBtn);
    controlPnl.add(quitBtn);

    //create actionlisteners for the buttons
    class QuitButtonListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent evt)
        {
            System.exit(0);
        }
    }
    ActionListener quitListener = new QuitButtonListener();
    quitBtn.addActionListener(quitListener);

        class ReadMyFortuneListener implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {            
            ArrayList<String> fortunes = new ArrayList<>();
            fortunes.add("The Browns will win the Super Bowl. Just Kidding the will never happen.");
            fortunes.add("You will get an A++ in Programming this semester!");
            fortunes.add("You will die in a very humorous way.");
            fortunes.add("You will fall in love with Kyle Hurt.");
            fortunes.add("You will be attacked by Aliens in the near future.");
            fortunes.add("You will be eaten by an alligator while walking to class today.");
            fortunes.add("You will go pro in football.");
            fortunes.add("Steve Jobs will come back from the dead to give you 1 billion dollars.");
            fortunes.add("You are going to disappear and never be heard from again.");
            fortunes.add("You are going to win a lifetime of moonshine.");
            fortunes.add("A bag of sugar will fall on you in the store today.");
            fortunes.add("You get to be the frountman of your favorite band.");


            Random rand = new Random();
            int pick = rand.nextInt(fortunes.size());
            fortuneArea.setText(fortunes.get(pick));

        }
    }
        ActionListener futureTeller = new ReadMyFortuneListener();
        futureBtn.addActionListener(futureTeller);
    }



}

使用“集合随机播放”静态方法可以随机播放数字:

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class ShuffleNumbers {
    static List<Integer> list = new ArrayList<Integer>();
    static {
        for (int i = 1; i <= 12; i++)
            list.add(new Integer(i));
    }

    public static void main(String[] argv) throws Exception {
        Collections.shuffle(list);
        System.out.println(list);
    }
}

首先! 您必须将arraylist作为字段放在FortuneTellerFrame类中:这是代码:

ArrayList<String> fortunes = new ArrayList<>();
public FortuneTellerFrame()
{            
        fortunes.add("The Browns will win the Super Bowl. Just Kidding the will never happen.");
        fortunes.add("You will get an A++ in Programming this semester!");
        fortunes.add("You will die in a very humorous way.");
        fortunes.add("You will fall in love with Kyle Hurt.");
        fortunes.add("You will be attacked by Aliens in the near future.");
        fortunes.add("You will be eaten by an alligator while walking to class today.");
        fortunes.add("You will go pro in football.");
        fortunes.add("Steve Jobs will come back from the dead to give you 1 billion dollars.");
        fortunes.add("You are going to disappear and never be heard from again.");
        fortunes.add("You are going to win a lifetime of moonshine.");
        fortunes.add("A bag of sugar will fall on you in the store today.");
        fortunes.add("You get to be the frountman of your favorite band.");

并且在actionPerform中,每次找到该元素都必须将其删除。 这是代码:

    public void actionPerformed(ActionEvent evt)
    {
        Random rand = new Random();
        int pick = rand.nextInt(fortunes.size());
        fortuneArea.setText(fortunes.get(pick));
        fortunes.remove(pick)
    }

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM