簡體   English   中英

使用Java Loop for ArrayList每次打印

[英]using Java Loop for ArrayList to print each time

我有一個方法,我基本上想要先模擬填充隊列,然后刪除第一個人並在每次我的public void mySimulation()方法中添加一個新人:

import java.util.*;

public class People {

    private final int DEFAULT_CAPACITY = 100;
    private int front, rear, count;
    private ArrayList<thePeople> people;
    private int theMaxCapacity;

    //-----------------------------------------------------------------
    //  Creates an empty queue using the specified capacity.
    //-----------------------------------------------------------------
    public People(int initialCapacity) {
        front = rear = count = 0;
        people = new ArrayList<thePeople>(Collections.nCopies(5, (thePeople) null));

    }

    //-----------------------------------------------------------------
    //  Adds the specified element to the rear of the queue, expanding
    //  the capacity of the queue array if necessary.
    //-----------------------------------------------------------------
    public void enque(thePeople element) {
        if (this.isFull()) {
            System.out.println("Queue Full");

            System.exit(1);
        } else {
            people.set(rear, element);
            rear = rear + 1;
            if (rear == people.size()) {
                rear = 0;
            }
            count++;
        }
    }

    //-----------------------------------------------------------------
    //  Removes the element at the front of the queue and returns a
    //  reference to it. Throws an EmptyCollectionException if the
    //  queue is empty.
    //-----------------------------------------------------------------
    public thePeople dequeue() {
        if (isEmpty()) {
            System.out.println("Empty Queue");
        }

        thePeople result = people.get(front);
        people.set(front, null);

        front = (front + 1) % people.size();

        count--;

        return result;
    }

    //-----------------------------------------------------------------
    //  Returns true if this queue is empty and false otherwise. 
    //-----------------------------------------------------------------
    public boolean isEmpty() {
        return (count == 0);
    }

    //-----------------------------------------------------------------
    //  Returns the number of elements currently in this queue.
    //-----------------------------------------------------------------
    public int size() {
        return count;
    }

    public boolean isFull() {

        return count == people.size();
    }

    public void mySimulation() {
        Random rand1 = new Random();
        thePeople theM = null;

        if (this.isFull()) {
            this.people.remove(0);
            System.out.println("Enqueueing...");
            this.enque(people.get(rand1.nextInt(people.size())));
            thePeople r1 = people.get(rear - 1);
            System.out.println(people.toString());
            System.out.println(r1);
            for (int e = 0; e < people.size(); e++) {
                if (people.get(e) instanceof thePeople) {
                    System.out.println("G");
                } else {
                    System.out.println("D");
                }
            }

        }

    }

    //-----------------------------------------------------------------
    //  Returns a string representation of this queue. 
    //-----------------------------------------------------------------
    @Override
    public String toString() {
        String result = "";
        int scan = 0;

        while (scan < count) {
            if (people.get(scan) != null) {
                result += people.get(scan).toString() + "\n";
            }
            scan++;
        }

        return result;

    }

    public static void main(String[] args) {
        People Q1 = new People(25);
        thePeople call1 = new thePeople("John King", "001 456 789");
        thePeople call2 = new thePeople("Michael Fish", "789 654 321");

        Q1.enque(call1);
        Q1.enque(call2);

        System.out.println(Q1.toString());
        ArrayList<thePeople> callerDetails = new ArrayList<>(Arrays.asList(call1, call2));
        Random rand = new Random();
        for (int z = 0; z <= 4; z++) {
            Q1.enque(callerDetails.get(rand.nextInt(callerDetails.size())));

        }
        System.out.println(Q1.toString());

    }

}

關於如何重復此過程的任何建議,以便我首先檢查隊列是否已滿,

如果是這樣的話,刪除第一個項目並使用我的arrayList添加一個新人,每次我的my public mySimulation()方法:因為我現在無法理解這一點?

您的代碼中充滿了錯誤:首先請確保刪除您在代碼的多行中意外放置的“the”。 然后將一些方法調整為正確的參數並返回類型。 至於你的問題:這很簡單

public void MySimulation(){
if(Queue.isFull()){
Queue.dequeue;}

Queue.enqueue;}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM