首页 > 解决方案 > 如何将 LinkedList 作为值传递?

问题描述

当我调用 shortestJobFirst 方法时,它不会打印任何内容,所以我认为我将它作为对 firstComeFirstServe 方法的引用而不是值传递,因此它会删除所有节点。我想为 4 个任务保留我的 LinkedList jobOrder。我曾尝试使用 subList() 但它不允许我删除任何节点,所以我认为这只是为了查看。有什么简单的方法可以解决这个问题吗?也许我应该只创建 4 个 LinkedList?请给我一个提示,非常感谢。

txt文件:

Job1
5
Job2
2
Job3
7
Job4
4
Job5
3
Job6
8

下面是我的代码

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Scanner;


public class Main {

    public static String filePath;


    public static void main(String[] args) throws Exception {

        LinkedList<Job> jobOrder = new LinkedList<Job>();

        System.out.println("Please enter input txt file path");
        System.out.println("Example: C:/Users/d/OneDrive/Desktop/Job.txt \n" );
        @SuppressWarnings("resource")

        //Create scanner to scan path for txt file
        Scanner input = new Scanner(System.in);
        //For user input
        System.out.print(">> " );
        filePath = input.nextLine();

        //Read input txt file
        fileReader(jobOrder,  filePath );

        //Call first come first serve algorithm
        firstComeFirstServe(jobOrder );
        //Shortest job first algorithm
        shortestJobFirst (jobOrder);

    }

    //My FCFS method
    public static void firstComeFirstServe(LinkedList<Job> jList )
    {
        int startTime = 0;
        int endTime = 0;

        System.out.println("(a) First Come First Service ");
        System.out.println("Job#" + " | " + " Start time" + " | " + " End Time" +" \t   |  " + "Job Completion ");

        while( !jList.isEmpty())
        {

            endTime = endTime + jList.getFirst().getId();

            System.out.println( jList.getFirst().getName() + " | \t" + startTime + " \t   |  " 
                    + endTime +" \t   |  "  + jList.getFirst().getName() + " completed @ " + endTime);

            startTime = endTime;

            jList.remove();
        }
        System.out.println(" ");
    }

    //My SJF method
    public static void shortestJobFirst (LinkedList<Job> jList )
    {

        Collections.sort(jList, new ascendingComparator());

        int startTime = 0;
        int endTime = 0;

        System.out.println("(b) Shortest Job First ");
        System.out.println("Job#" + " | " + " Start time" + " | " + " End Time" +" \t   |  " + "Job Completion ");

        while( !jList.isEmpty())
        {       
            endTime = endTime + jList.getFirst().getId();

            System.out.println( jList.getFirst().getName() + " | \t" + startTime + " \t   |  " 
                    + endTime +" \t   |  "  + jList.getFirst().getName() + " completed @ " + endTime);

            startTime = endTime;

            jList.remove();
        }
        System.out.println(" ");
    }


    //This method will read txt file and copy it to LinkedList
    public static void fileReader(LinkedList<Job> jList, String path ) throws Exception 
    {         
        //Scan txt file 
        Scanner sc = new Scanner(new BufferedReader(new FileReader(path)));
        String tempName = "";
        int tempLength = 0;
        int lineCount = 0;

        //Read while the scan still has next integers 
        while(sc.hasNextLine()) 
        {       
            lineCount = lineCount + 1;

            if (lineCount % 2 != 0)
            {
                tempName = sc.next();
            }
            else
            {
                tempLength = sc.nextInt();
                jList.add( new Job(tempName, tempLength) );
            }

        }
        sc.close();
    }

}


////

import java.util.Comparator;

class Job{

    private String jobNum;
    private int jobLength;

    public Job(String jobNum, int jobLength){
        this.jobLength = jobLength;
        this.jobNum = jobNum;
    }

    //Change this for display later
    public String toString(){
        return "[" + this.jobNum + "=>" + this.jobLength + "]";
    }

    public int getId(){
        return this.jobLength;
    }

    public String getName(){
        return this.jobNum;
    }
}


class ascendingComparator implements Comparator<Job>{

  public int compare(Job Job1, Job Job2) {        
      return Job1.getId() - Job2.getId();
  }    
}

标签: javalinked-list

解决方案


我想你已经习惯了 C++ 或 C。在 Java 中,如果它是原始类型,它总是按值传递,如果它是一个对象,它总是按引用传递。但是,您可以使用Object'clone方法。请注意,有些对象没有实现clone,但LinkedList这样做你很好。

firstComeFirstServe((LinkedList<Job>) jobOrder.clone());

推荐阅读