ArrayList实现但是arrayList.size()不会更新

时间:2018-03-03 01:48:56

标签: java arraylist

我很想解决这个问题Printer Queue,但我是新手使用Java而我似乎无法使其工作。

我的逻辑基础是从arrayList中删除元素,但即使在调用'.trimToSize()之后,大小也不会更新,问题是什么?

import java.util.Scanner;
import java.util.ArrayList;

public class PrinterQueue {

    static boolean isBiggest(ArrayList<Integer> list, int index) {
        int max_index = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) > list.get(max_index)) {
                max_index = i;
            }
        }
        return max_index == index;
    }

    public static void main(String[] args) {
        Scanner sca = new Scanner(System.in);
        int number_of_int = sca.nextInt();
        for (int i = 0; i < number_of_int; i++) {
            int number_of_jobs = sca.nextInt();
            int my_job_index = sca.nextInt();

            ArrayList<Integer> list = new ArrayList<Integer>(number_of_jobs);
            for (int j = 0; j < number_of_jobs; j++) {
                list.add(sca.nextInt());
            }

            int minutes_to_complete = 0;
            while(list.size() > 0) {
                System.out.println("current size: " + list.size());
                int current = list.get(0);
                if (isBiggest(list, current) && my_job_index == 0) {
                    System.out.println("1: ");
                    minutes_to_complete++;      
                    list.remove(0);
                } else {
                    if (isBiggest(list, current)) { 
                        System.out.println("2: ");
                        list.remove(0); 
                        minutes_to_complete++;      
                    } else {
                        int cache = list.get(0);
                        list.remove(0);
                        list.add(cache);
                        my_job_index--;
                        System.out.println("my_job_index:" + my_job_index);
                    }
                }
                list.trimToSize();

            }
        }
    }
}

0 个答案:

没有答案