繁体   English   中英

如何在 ArrayList 内的 object 内的数组中找到最小值? 该值与其他字符连接

[英]How can I find the smallest value in an array that is within an object inside of an ArrayList? The value is concatenated with other characters

我正在为 Java 中的 SJF 编写 CPU 调度程序。我有一个包含作业对象的 ArrayList。 每个作业都包含一个程序 ID 以及一个 CPU 突发数组等。如何在所有作业中找到最小值,即从每个数组的最低索引中选择?


好吧,我会尽量说得更清楚。 这是我用来创建作业并将它们同时添加到 ArrayList 的代码。

//add PCB objects to the jobQueue ArrayList. listOfBursts is an array.
 jobQueue.add(new PCB(processID, listOfCPUBursts));

假设我在 jobQueue ArrayList 中有 3 个 PCB 对象。我需要按照它们在数组中出现的顺序运行 CPU 突发,但最低索引中的最小值必须首先运行。 感觉这个还不是很清楚,我试试看图吧。

          PCB-1's array = {4, 7, 2, 3}
          PCB-2's array = {5, 2, 1, 3}
          PCB-3's array = {3, 8, 4, 2}

这些作业需要 go 到 CPU,无论输入什么时间片,然后返回到 jobQueue。 它们 go 到 CPU 的顺序需要由任何 PCB 的索引 0 处的最低值确定。 在这种情况下,我需要找到 4、5 或 3 中的最低值。

假设时间片是 3。PCB-3 进入 CPU,用完它的突发,然后返回到 jobQueue。 我再次需要找到发送到 CPU 的最低值,但现在我正在查看 4、5 和 8。

我希望这更有意义。

声明一个数组以存储来自每个作业的每个 CPU Burst 数组的所有最低值。 将此数组的大小初始化为可用的作业数(ArrayList 大小)。

遍历 ArrayList 以获取存储在那里的每个作业 object。 从每个作业中获取 CPU Bursts 数组并按升序排序。 这会将最小值带到每个数组的开头。 获取索引 0 处的值并将其存储在您之前声明的数组中。

现在你有了一个包含所有最低值的数组。

这是一些示例代码:

工作 Class:

public class Job {

    private int programID;
    private int[] cpuBursts;

    public Job() {}

    public Job (int programid,  int[] cpubursts) {
        this.programID = programid;
        this.cpuBursts = cpubursts;
    }

    public int getProgramID() {
        return programID;
    }

    public void setProgramID(int programID) {
        this.programID = programID;
    }

    public int[] getCpuBursts() {
        return cpuBursts;
    }

    public void setCpuBursts(int[] cpuBursts) {
        this.cpuBursts = cpuBursts;
    }
}

填写工作和 ArrayList:

ArrayList<Job> jobsList = new ArrayList<>();
Job job;

// Fill jobsList ArrayList with 10 different job objects...
Random randomGenerator = new Random();
for (int i = 0; i < 10; i++) {
    // Create a new Program ID
    int id = 2230 + (i + 1);

    // Fill a CPU Burst array with 15 random integer values...
    int[] bursts = new int[15];
    for (int j = 0; j < 15; j++) {
        int randomBurst = randomGenerator.nextInt(100) + 1;
        bursts[j] = randomBurst;
    }

    // declare a job object
    job = new Job(id, bursts);

    // Add the job to the Jobs List
    jobsList.add(job);
}

现在你有一个名为ArrayList的 jobsList,它包含 10 个Job对象。 每个 Job object 包含一个名为programID的 integer 变量(其中包含一个唯一的程序 ID 号)和一个名为cpuBursts的 integer 数组,其中包含从 1 到 100 的 15 个随机突发值。

现在从每个作业中获取最低的突发值:

/* From the 10 Job objects stored within the ArrayList,
   display the all the jobs and at the end, display the
   lowest burst detected in each Job cpuBurst array.  */
int[] lowestBursts = new int[jobsList.size()];  // To hold the lowest bursts.

// Iterate through the Jobs within the ArrayList...
for (int i = 0; i < jobsList.size(); i++) {
    // Display information about the Job...
    System.out.println("Job #" + (i + 1) + ":");
    System.out.println("Program ID: --> " + jobsList.get(i).getProgramID());
    System.out.println("CPU Bursts: --> " + Arrays.toString(jobsList.get(i).getCpuBursts()));
    System.out.println();

    // Sort the cpuBurst array in ascending order.
    Arrays.sort(jobsList.get(i).getCpuBursts());

    // Grab the first burst within the array, it will be the lowest.
    lowestBursts[i] = jobsList.get(i).getCpuBursts()[0];
}

// Now, Display all the lowest bursts from each array..
System.out.println("Lowest Burst In Each Job:");
System.out.println(Arrays.toString(lowestBursts));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM