我正在创建一个应用程序,允许用户为8个赛车手分配一个车道号码。我已将runners名称设置为ArrayList,并将通道设置为Set。目前我正在调用列表并设置为类阵列。我将ArrayList和Set组合为Singleton Map。我无法让Set接受Integer变量raceLane作为Type。任何有关Set问题的帮助以及有关如何将ArrayList和Set转换为Singleton Map的建议都将不胜感激,谢谢。这是我到目前为止所拥有的。
package Race;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class LaneDesignation {
//create ArrayList and Set
private ArrayList runnersArray= new ArrayList();
private Set<raceLane> raceLanesSet = new HashSet<raceLane>();
//Declare Variables
private Integer raceLane;
String runnerName;
int numbRunner, i = 0;
//Call Scanner Instance
Scanner input = new Scanner(System.in);
public LaneDesignation()
{
this.raceLane = raceLane;
}
public void setNumb(int numb)
{
numbRunner= numb;
}
public void setName()
{
System.out.print("Please enter the name of runner " + numbRunner + ": ");
runnerName = input.next();
i = 0;
runnersArray.add(i, runnerName);
i++;
}
public String returnName()
{
return runnerName;
}
public void getLane()
{
raceLanesSet.add(0, raceLane);
do
{
System.out.print("Please enter the lane the runner is assigned to ");
while (input.hasNextInt())
{
//Test that only numeric characters were entered
System.out.println("Invalid entry. Please enter a numeric value");
System.out.print("Please enter a lane designation from 1-8: ");
input.next();
}
raceLane = input.nextInt();
}while (raceLane <=0);
raceLanesSet.add(i, raceLane);
i++;
}
public Integer returnLane()
{
return raceLane;
}
public void displayDesignation()
{
System.out.println("Runner | Desinated Lane");
while (i < runnersArray.size())
{
System.out.println(runnersArray.(i) + " will race in lane" + raceLanesSet.get(i));
i++;
}
}
public void setRaceLane(Integer raceLane)
{
this.raceLane = raceLane;
}
}
调用ArrayList和Set并将其组合为ClassArray //受到凯西的启发
package Race;
import java.util.Scanner;
public class Runners {
//creation array of classes of class LaneDesignation
LaneDesignation[] Assign = new LaneDesignation[8];//8 Runners and 8 lanes
Scanner input = new Scanner(System.in);
public Runners()
{
//initialization of the class
for (int i=0; i<Assign.length; i++){
Assign[i] = new LaneDesignation();
}
}
public void raceArray()
{
for(int i=0;i<Assign.length; i++)
{
Assign[i].setNumb((i+1));
Assign[i].setName();
Assign[i].getLane();
}
}
//Method to display the contents in the list
//I am receiving runnersArray cannot be resolved to variable error
public void outputRunners()
{
for(LaneDesignation currentRunner : runnersArray)
{
System.out.println(currentRunner.getRunner());
}
}
}
答案 0 :(得分:0)
集合的类型参数(set / HashSet / ArrayList ...)应该是Object类型。 raceLane不是对象类型,它只是Integer类型的变量。 你必须改变
Set<Integer> raceLanesSet = new HashSet<Integer>()
您必须定义一个新类“raceLane”