如何在Java中获得唯一字符串列表的随机顺序?
我在ArrayList中有12个字符串。我想在文本区域中随机显示这些字符串。我已经找到了随机出现字符串的地方,但它们有时会重复。我不希望他们重复,直到它通过所有12个字符串。以下是我目前的代码:如何在Java中获得唯一字符串列表的随机顺序?,java,string,user-interface,random,Java,String,User Interface,Random,我在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; impo
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);
}
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.List;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.Random;
导入javax.swing.Icon;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
导入javax.swing.JTextField;
/**
*应用程序的布局
*
* ###############################
* ## ##
* ## ##
* ## ##### ##
* ## ##### ##
* ## ##### ##
* ## ##
* ###############################
* ## ##
* ## ##
*##文本区##
* ## ##
* ## ##
* ## ##
* ###############################
* ## ##
*####财富###退出###
* ## ##
* ###############################
*/
/**
*
*@作者凯勒·施密特
*/
公共类算命框架扩展了JFrame
{
公共算命框架()
{
//设置窗口的宽度和高度
最终整型框架高度=400;
最终整数帧_宽度=400;
//设置文本区域的宽度和高度
最终int区域_行=10;
最终int区域_列=25;
//声明将要使用的面板。
最终JPanel mainPnl、titlePnl、textPnl、controlPnl;
最终JLabel图像;
//为您的标题/标题图像声明并查找图像?
ImageIcon=newImageIcon(“src/1804-1-bad-fortune-teller.jpg”);
fortuneImage=newjlabel(“算命先生!”,图标,JLabel.CENTER);
fortuneImage.setVerticalTextPosition(JLabel.TOP);
fortuneImage.setHorizontalTextPosition(JLabel.CENTER);
//声明将要使用的按钮。
最终JButton futureBtn,quitBtn;
//创建主面板,并将其他面板添加到其中
mainPnl=新的JPanel();
mainPnl.setLayout(新的BorderLayout());
titlePnl=新的JPanel();
textPnl=新的JPanel();
controlPnl=新的JPanel();
mainPnl.add(标题NL,BorderLayout.NORTH);
mainPnl.add(textPnl,BorderLayout.CENTER);
mainPnl.add(controlPnl,BorderLayout.SOUTH);
//将主面板添加到父框架
添加(mainPnl);
设置尺寸(框宽、框高);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//将其他组件添加到子面板
标题NL.add(图像);
最终的JTextArea-fortuneArea;
fortuneArea=新的JTextArea(AREA_行、AREA_列);
JScrollPane scrollPane=新的JScrollPane(区域);
setText(“我将告诉未来…”);
textPnl.add(滚动窗格);
futureBtn=newjbutton(“阅读我的财富!”);
quitBtn=新的JButton(“Quit”);
控制PNL.add(未来BTN);
控制pnl.add(quitBtn);
//为按钮创建actionlisteners
类QuitButtonListener实现ActionListener
{
已执行的公共无效操作(操作事件evt)
{
系统出口(0);
}
}
ActionListener quitListener=新建QuitButtonListener();
quitBtn.addActionListener(quitListener);
类ReadMyFortuneListener实现ActionListener
{
已执行的公共无效操作(操作事件evt)
{
ArrayList fortunes=新ArrayList();
财富。加上(“布朗一家将赢得超级碗。只是开个玩笑,这永远不会发生。”);
添加(“本学期编程你将获得A+);
加上(“你会以一种非常幽默的方式死去。”);
加上(“你会爱上凯尔·赫特。”);
添加(“你在不久的将来会被外星人攻击。”);
添加(“你今天走路去上课时会被鳄鱼吃掉。”);
财富。加上(“你将成为职业足球运动员。”);
《财富》.add(“史蒂夫·乔布斯将死而复生,给你10亿美元。”);
财富。加上(“你将消失,再也没有消息了。”);
财富。加上(“你将赢得一生的私酒。”);
添加(“今天商店里会有一袋糖掉在你身上。”);
财富。加上(“你会成为你最喜欢的乐队中的蛙人。”);
Random rand=新的Random();
int pick=rand.nextInt(fortunes.size());
fortuneArea.setText(fortunes.get(pick));
}
}
ActionListener futureTeller=新的ReadMyFortuneListener();
futureBtn.addActionListener(futureTeller);
}
}
使用Collection shuffle static方法洗牌您的号码:
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);
}
}
import java.util.List;
导入java.util.ArrayList;
导入java.util.Collections;
公共类随机数{
静态列表=新的ArrayList();
静止的{
对于(int i=1;i使用Collection shuffle static方法来洗牌您的数字:
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);
}
}
import java.util.List;
导入java.util.ArrayList;
导入java.util.Collections;
公共类随机数{
静态列表=新的ArrayList();
静止的{
首先,对于(inti=1;i)!
您必须将arraylist
作为字段放入算命框架类:
代码如下:
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.");
public void actionPerformed(ActionEvent evt)
{
Random rand = new Random();
int pick = rand.nextInt(fortunes.size());
fortuneArea.setText(fortunes.get(pick));
fortunes.remove(pick)
}
首先!
您必须将arraylist
作为字段放入算命框架类:
代码如下:
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.");
public void actionPerformed(ActionEvent evt)
{
Random rand = new Random();
int pick = rand.nextInt(fortunes.size());
fortuneArea.setText(fortunes.get(pick));
fortunes.remove(pick)
}
不使用随机索引,您可以简单地