Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我想帮助创建一个对象数组_Java_Arrays_Object_Queue - Fatal编程技术网

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

Java 我想帮助创建一个对象数组,java,arrays,object,queue,Java,Arrays,Object,Queue,我正在从事一个项目,在这个项目中,我试图使用数组实现一个队列。队列中会有一份附有一些信息的人员列表。我希望在创建这种类型的数组以及如何操作它方面得到帮助。下面的三个程序是一个驱动程序、一个初始化人员的类和一个在读取文件时操纵队列的类 package queue; public class soldierQueue{ private String Name, Branch, Duty; private int Commitment; public String sold

我正在从事一个项目,在这个项目中,我试图使用数组实现一个队列。队列中会有一份附有一些信息的人员列表。我希望在创建这种类型的数组以及如何操作它方面得到帮助。下面的三个程序是一个驱动程序、一个初始化人员的类和一个在读取文件时操纵队列的类

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);
    }
}
包队列;
公营士兵队{
私有字符串名称、分支、职责;
私人承诺;
公共字符串士兵队列(字符串名称、字符串分支、字符串职责、int承诺){
名称=名称;
分支=分支;
责任=责任;
承诺=承诺;
字符串soldierInfo=Name+“,“+分支机构+”,“+职责+”,“+承诺;
归国军人;
}
}
包队列;
公共类队列管理类{
公共整数前=0,后=0,计数=0,大小=5;
公共士兵队列[]士兵=新士兵队列[20];
//添加到队列的后部
公共int[]addPerson(int[]队列,int addNumber){
如果(计数==大小){
System.out.println(“堆栈已满”);
}
否则,如果(后部<尺寸){
队列[后部]=地址编号;
//System.out.println(“前”+队列[前]+“后”+队列[后]);
//System.out.println(后+添加+队列[后]);
计数++;
后++;
}
否则{
后部=0;
队列[后部]=地址编号;
//System.out.println(后+添加+队列[后]);
//System.out.println(“前”+队列[前]+“后”+队列[后]);
后++;
计数++;
}
返回队列;
}
//从队列中删除
公共int[]removeQ(int[]队列){
如果(计数=0){
System.out.println(“堆栈为空,无法删除任何内容”);
}
否则如果(正面<(尺寸)){
//System.out.println(“删除:前端”+队列[前端]+“计数”+计数
//+“前”+前);
前端++;
计数--;
//系统输出打印项次(“计数”+计数);
//System.out.println(“删除:前端”+队列[前端]+“计数”+计数);
}
否则{
正面=0;
计数--;
//System.out.println(“删除:前端”+队列[front]);
前端++;
//System.out.println(“清空的前端”+队列[前端]+“计数”+计数);
}
返回队列;
}
公共字符串printQ(int[]队列){
字符串myQueue=“”;
int[]tempQ=新int[5];
int tempCount=0,tempFront=0,tempRear=0;
如果(计数=0){
System.out.println(“堆栈为空”);
}
否则{
而(计数>0){
如果(正面<(尺寸)){
//System.out.println(“前端”+前端+“队列”+队列[前端]);
tempQ[tempRear]=队列[front];
myQueue+=队列[前端]+“”;
前端++;
tempRear++;
tempCount++;
计数--;
}
否则{
正面=0;
//System.out.println(“前端”+前端+“队列”+队列[前端]);
tempQ[tempRear]=队列[front];
myQueue+=队列[前端]+“”;
tempRear++;
tempCount++;
前端++;
计数--;
}
}//结束时
而(临时计数>0){
if(tempFront<大小){
//System.out.println(tempQ[tempFront]+“”+tempCount);
tempFront++;
温度计数--;
}
否则{
tempFront=0;
温度计数--;
}
}
}//如果/否则结束
返回myQueue;
}
//报告队列中的第一个数字
公共int checkTopQ(int[]队列){
返回队列[后方];
}
//清除队列
公共无效clearQ(){
正面=0;
后部=0;
计数=0;
}
//返回队列是否为空
公共布尔epmtyQ(int[]队列){
布尔空=假;
如果(计数>0){
空=真;
}
返回空;
}
//指示队列是否已满
公共布尔fullQ(int[]队列){
布尔满=假;
如果(计数==大小){
完整=正确;
}
全额退还;
}
//排队
公共int[]fillQ(int[]队列){
//系统输出打印LN(后);
如果(计数==大小){
System.out.println(“不能添加任何内容,它是完整的”);
}
否则{
while(计数<大小){
如果(后部<尺寸){
队列[后部]+=(计数*后部);
计数++;
后++;
}
否则{
后部=0;
队列[后部]+=(计数*后部);
后++;
计数++;
}
}
}
返回队列;
}
//搜索号码
公共布尔searchQ(int[]队列){
int[]tempQ=新int[5];
int tempCount=0,tempFront=0,tempRear=0;
布尔值=false;
如果(计数=0){
System.out.println(“堆栈为空”);
}
否则{
而(计数>0){
如果(正面<(尺寸)){
//System.out.println(“前端”+前端+“队列”+队列[前端]);
tempQ[tempRear]=队列[front];
前端++;
tempRear++;
tempCount++;
计数--;
}
否则{
正面=0;
//System.out.println(“前端”+前端+“队列”+队列[前端]);
坦普尔