[英]Picking then removing random elements from and arraylist
A group of friends and I are doing a secret santa exchange and I decided to make a program to randomize and print a master list that I would have, for fun. 我和一群朋友正在做一次秘密的圣诞老人交流,所以我决定制作一个程序以随机化并打印一个我会拥有的主列表,以达到娱乐目的。 Here's what I have so far. 到目前为止,这就是我所拥有的。 I'm having a problem with the line String random1 = sList.get(myRandomizer.nextInt(sList.size()));
我在String random1 = sList.get(myRandomizer.nextInt(sList.size()));
It gives the error that I'm doing incompatible types but I'm pretty sure it's returning what it should? 它给出了我正在执行不兼容类型的错误,但是我很确定它正在返回应有的错误?
Here is my initial class 这是我的第一堂课
import java.io.Serializable;
public class SpecifiedData implements Serializable
{
private String name;
public SpecifiedData(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
Here is my test class where my errors are 这是我的错误所在的测试班
import java.util.*;
import java.io.*;
public class Randomizer
{
public static void main(String[] args) throws IOException, ClassNotFoundException
{
Random myRandomizer = new Random();
boolean done = false;
ArrayList<SpecifiedData> sList = new ArrayList<SpecifiedData>();
File sFile = new File("Participants.dat");
if(sFile.exists())
{
FileInputStream myFIS = new FileInputStream(sFile);
ObjectInputStream sIn = new ObjectInputStream(myFIS);
sList = (ArrayList<SpecifiedData>)sIn.readObject();
sIn.close();
}
do
{
Scanner myScanner = new Scanner(System.in);
while (!done)
{
System.out.println("1 - add a person");
System.out.println("2 - display all people");
System.out.println("3 - delete person");
System.out.println("4 - randomize");
System.out.println("5 - exit");
int choice = Integer.parseInt(myScanner.nextLine());
if (choice == 1)
{
System.out.print("Enter person's name: ");
String participantsName = myScanner.nextLine();
SpecifiedData mySpecifiedData = new SpecifiedData(participantsName);
sList.add(mySpecifiedData);
}
else if (choice == 2)
{
for(int i = 0; i < sList.size(); i++)
System.out.println(sList.get(i).getName());
}
else if (choice == 3)
{
System.out.println("Enter persons name: ");
String participantsName = myScanner.nextLine();
for(SpecifiedData mySpecifiedData : sList)
{
if(mySpecifiedData.getName().equals(participantsName))
{
sList.remove(mySpecifiedData);
break;
}
}
}
else if (choice == 4)
{
for(int i = 0; i < (sList.size()/2); i++)
{
for(SpecifiedData mySpecifiedData : sList)
{
String random1 = sList.get(myRandomizer.nextInt(sList.size()));
if(mySpecifiedData.getName().equals(random1))
{
System.out.println(random1 + " ");
sList.remove(random1);
break;
}
String random2 = sList.get(myRandomizer.nextInt(sList.size()));
if(mySpecifiedData.getName().equals(random2))
{
System.out.print(random2);
sList.remove(random2);
break;
}
}
}
}
else if(choice == 5)
{
done = true;
}
else
System.out.println("Invalid menu choice!");
}
System.out.println("Goodbye!");
}while(!done);
FileOutputStream myFOS = new FileOutputStream(sFile);
ObjectOutputStream sOut = new ObjectOutputStream(myFOS);
sOut.writeObject(sList);
sOut.close();
}
}
sList is declared as an ArrayList that contains objects of type SpecifiedData. sList声明为ArrayList,其中包含SpecifiedData类型的对象。 On that line you are getting an object from the list and attempting to assign it to a String. 在那一行上,您从列表中获取一个对象,并尝试将其分配给String。 You need to assign it to a SpecifiedData. 您需要将其分配给SpecifiedData。
SpecifiedData random1 = sList.get(myRandomizer.nextInt(sList.size()));
sList
is a list of type SpecifiedData
, not String
so this error is perfectly normal. sList
是类型SpecifiedData
的列表,而不是String
的列表,因此此错误完全正常。 By the way, you are removing elements from a list you are traversing... bad idea. 顺便说一句,您要从要遍历的列表中删除元素……不好的主意。 I did not look at your code carefully but one thing is sure : you must improve it. 我没有仔细查看您的代码,但是可以肯定的是:您必须对其进行改进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.