Java:不可转换类型错误(使用数组队列)

时间:2016-03-17 13:56:38

标签: java arrays class queue

我正在开发一个模拟餐厅座位系统的程序。我无法将(将对象放入队列中)一个Party(一个对象)排队,而不会使Party类中的变量变为静态,因为如果我这样做,那么当我出队时,它会将队列中的所有对象写入同样的。

以下是主程序的代码:

public static void main (String [] args)
    throws IOException
{
    //get name of simulation file from user and "open" that file
    Scanner cin= new Scanner (System.in);

    System.out.println("--- Welcome to the La Food Restaurant Simulator! ---");
    System.out.println("Enter a data file name:");
    String filename= cin.next();

    Scanner fileread= new Scanner(new FileReader (filename));

    Queue Q= new QueueArray(100);

    boolean flag=true;

    while (flag==true)
    {
        char action= fileread.next().charAt(0);
        int seatedtime;
        System.out.println(action); //TESTING PURPOSES ONLY

        //if A read in party and put them in at the back of the queue (enqueue)
        if(action=='A')
        {
            Party p= (Party)Q.enqueue(new Party(fileread));

            System.out.println(p); //TESTING PURPOSES ONLY
            //System.out.println(p.size); // TESTING PURPOSES ONLY
            //System.out.println(p.name); // TESTING PURPOSES ONLY

            //System.out.println("Please wait at the bar, party "+p.name+" of "+p.size+" people.");
        }

        //if T put the party at the front in the queue and remove them (dequeue)
        if(action=='T')
        {
            seatedtime=fileread.nextInt();
            System.out.println(seatedtime); //TESTING PURPOSES ONLY

            Party p2=(Party) Q.dequeue();
            System.out.println(p2.name);

            //need a way to return the front object's (that was dequeued) info (size,name,arrival)
            System.out.println("Table for "+p2.name "!");
        }

        // if Q stop the simulation
        if(action=='Q')
        {
            flag=false;
        }
    }

    System.out.println("---Simulation Terminated---");
    System.out.println("The average waiting time was ");
    System.out.println("The following parties were never seated:");
    //print out info on the unseated party
}
}

这是我的派对类代码:

public class Party
{

int arrival;
int size;
String name;

//constructor of party object
public Party(Scanner file)
{
    arrival=file.nextInt();
    size= file.nextInt();
    name= file.next();
    name= name + file.nextLine();
}
}

以下是QueueArrays类中使用的入队方法:

Object [] items; 
int front; 
int back;   
int count; 

public void enqueue(Object x) 
{  
    if(isFull()) return; // do nothing 
    count++; 
    back++; 
    f(back>=items.length) back=0; 
    items[back]=x; 
}

当我尝试编译主程序时,它会出错:

  

我排队参加派对的不可转换类型。需要:   派对找到:无效

如何在不使用Q.enqueue()的情况下修复它,因为这需要我的Party Class中的变量静态导致覆盖问题?

1 个答案:

答案 0 :(得分:0)

不确定我的问题是否正确,但是Party p= (Party)Q.enqueue(new Party(fileread));行无法编译,因为enqueue方法返回void - 您无法将其分配给变量。将其更改为

Party p = new Party();
Q.enqueue(p);

尽管如此,我认为没有任何理由可以保持静态。

顺便说一句,你为什么要实现自己的Queue而不使用Queue<Party> q = new LinkedList<Party>();(来自java.util包)这样的东西 - 这会让你的生活变得更轻松。