我想帮助创建一个对象数组

时间:2015-11-10 19:28:06

标签: java arrays object queue

我正在开发一个项目,我正在尝试使用数组实现队列。在队列中将列出附有一些信息的人员。我正在寻求帮助创建该类型的数组以及如何操作它。下面的三个程序是一个驱动程序,一个用于初始化人员的类,以及一个在从文件中读取时操纵队列的类。

package queue;

public class soldierQueue{

   private String Name, Branch, Duty;
   private int Commitment; 


   public String soldierQueue(String name, String branch, String duty, int commitment){
   Name = name;
   Branch = branch;
   Duty = duty;
   Commitment = commitment;

   String soldierInfo = Name  + ", " + Branch + ", " + Duty + ", " + Commitment;  



   return soldierInfo;

   }

 }


package queue;

public class QueueNGobernClass {
   public int front = 0, rear = 0,count = 0,size = 5;
   public soldierQueue[] soldiers = new soldierQueue[20];

    //adds to the rear of the queue
    public int[] addPerson(int[] queue,int addNumber){

        if(count == size){
            System.out.println("stack is full");
            }
        else if(rear < size){
            queue[rear] = addNumber;
            //System.out.println("front " + queue[front] + "rear" + queue[rear]);               
            //System.out.println(rear + "add " + queue[rear]);
            count++;
            rear++;
        }
        else{
            rear = 0;
            queue[rear] = addNumber;
            //System.out.println(rear + "add " + queue[rear]);
            //System.out.println("front " + queue[front] + "rear" + queue[rear]);   
            rear++;
            count++;
        }
    return queue; 
    }


    //removes from the queue
    public int[] removeQ(int[] queue){

        if(count == 0){
            System.out.println("stack is empty, cannot remove anything");
        }
        else if(front < (size)){
            //System.out.println("remove:front " + queue[front]+ " count " + count 
            //                  + " front " + front);           
            front++;
            count--;
            //System.out.println("count" + count);
            //System.out.println("remove:front " + queue[front]+ " count " + count);
        }
        else{
            front = 0;
            count--;
            //System.out.println("remove:front " + queue[front]);
            front++;

            //System.out.println("EMPTIED front " + queue[front] + " count " + count);
        }


        return queue;
    }

    public String printQ(int[] queue){
        String myQueue = "";
        int[] tempQ = new int[5];
        int tempCount = 0, tempFront = 0, tempRear = 0; 
        if(count == 0){
            System.out.println("Stack is empty");
        }
        else{

            while(count > 0){

                if(front < (size)){
                //System.out.println(" front " + front + " queue " + queue[front]);
                tempQ[tempRear] = queue[front];
                myQueue += queue[front] + " ";
                front++;
                tempRear++;
                tempCount++;
                count--;
                }
                else{
                    front = 0;
                    //System.out.println(" front " + front + " queue " + queue[front]);
                    tempQ[tempRear] = queue[front];
                    myQueue += queue[front] + " ";
                    tempRear++;
                    tempCount++;
                    front++;
                    count--;
                }
            }//ends while
            while(tempCount > 0){
                if(tempFront < size){
                    //System.out.println(tempQ[tempFront] + " " + tempCount); 
                     tempFront++;
                     tempCount--;
                }
                else{
                    tempFront = 0;
                    tempCount--;

                }
            }
        }//ends if/else
        return myQueue;
    }

    //reports first number in queue
    public int checkTopQ(int[] queue){

        return queue[rear];
    }

    //clears the queue
    public void clearQ(){
        front = 0;
        rear = 0;
        count = 0;  
    }


    //returns whether queue was empty or not
    public boolean epmtyQ(int[] queue){
        boolean empty = false;

        if(count > 0){
            empty = true;
        }
        return empty;
    }


    //tells whether queue is full or not
    public boolean fullQ(int[] queue){
        boolean full = false;

        if(count == size){
            full = true;
        }
        return full;
    }


    //fills a queue
    public int[] fillQ(int[] queue){
        //System.out.println(rear);

        if(count == size){
            System.out.println("Nothing can be added, its full");
        }
        else{
            while(count < size){
                if(rear < size){
                queue[rear] += (count * rear);
                count++;
                rear++;
                }
                else{
                rear = 0;
                queue[rear] += (count * rear);
                rear++;
                count++;
                }
            }
        }
        return queue;
    }

   //search for a number
    public boolean searchQ(int[] queue){
   int[] tempQ = new int[5];
    int tempCount = 0, tempFront = 0, tempRear = 0; 
   boolean found = false;

   if(count == 0){
            System.out.println("Stack is empty");
        }
        else{

            while(count > 0){

                if(front < (size)){
                //System.out.println(" front " + front + " queue " + queue[front]);
                tempQ[tempRear] = queue[front];

                front++;
                tempRear++;
                tempCount++;
                count--;
                }
                else{
                    front = 0;
                    //System.out.println(" front " + front + " queue " + queue[front]);
                    tempQ[tempRear] = queue[front];

                    tempRear++;
                    tempCount++;
                    front++;
                    count--;
                }
            }//ends while
            while(tempCount > 0){
                if(tempFront < size){
                    //System.out.println(tempQ[tempFront] + " " + tempCount); 
                    tempFront++;
                    tempCount--;
                }
                else{
                    tempFront = 0;
                    tempCount--;

                }
            }
        }//ends if/else


   return found;
   }

   public int[] manipulateQ(int[] queue){


   return queue;
   }
}//ends class



package queue;

import java.util.*;

public class QueueNGobernDemo {

    public static void main(String[] args) {

      //soldierQueue[] soldiers = new soldierQueue[40]; 

        int[] queue = new int [20];
        StringTokenizer st = new StringTokenizer("");
        QueueNGobernClass object = new QueueNGobernClass();

        object.addToQ(queue,5);
        object.addToQ(queue,8);

        object.removeQ(queue);
        object.addToQ(queue, 11);
        object.addToQ(queue, 22);
        object.addToQ(queue, 33);
        object.addToQ(queue, 44);
        object.addToQ(queue, 55);

     System.out.println("queue: " + object.printQ(queue));
     object.fillQ(queue);
    }
}

0 个答案:

没有答案