java—如何在arraylist中的对象内的数组中找到最小值?该值与其他字符串联

pdsfdshx  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(263)

我正在用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}

无论输入了什么时间片,这些作业都需要进入cpu,然后返回到jobqueue。它们进入cpu的顺序需要由任何pcb的索引0处的最低值确定。在这种情况下,我需要找到最低的4,5,或3。
假设时间片是3。pcb-3到达cpu,用完它的突发,然后返回到jobqueue。同样,我需要找到发送到cpu的最低值,但现在我看到的是4、5和8。
我希望这更有意义。

pxiryf3j

pxiryf3j1#

声明一个数组以存储每个作业中每个cpu突发数组的所有最低值。将此数组的大小初始化为可用的作业数(arraylist大小)。
遍历arraylist以获取存储在其中的每个作业对象。从每个作业获取cpu突发数组,并按升序排序。这将为每个数组的开头带来最小的值。获取索引0处的值并存储在前面声明的数组中。
现在有了一个包含所有最低值的数组。
下面是一些示例代码:
工作类别:

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<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);
}

现在有一个名为jobslist的arraylist,它包含10个作业对象。每个作业对象包含一个名为programid的整数变量(其中包含一个唯一的程序id号)和一个名为cpubursts的整数数组,其中包含15个从1到100的随机突发值。
现在要获得每个作业的最低突发值:

/* 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));

相关问题