java - 如何将 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();
}
}
解决方案
我想你已经习惯了 C++ 或 C。在 Java 中,如果它是原始类型,它总是按值传递,如果它是一个对象,它总是按引用传递。但是,您可以使用Object
'clone
方法。请注意,有些对象没有实现clone
,但LinkedList
这样做你很好。
firstComeFirstServe((LinkedList<Job>) jobOrder.clone());
推荐阅读
- angular - 从文本框中的自动完成建议中检索选定的选项
- angular - 在 Angular 中,如果担心新数据到来时缓存无效,那么在处理缓存时,shareReplay 和拦截器之间哪种技术更好?
- asynchronous - 在 Dart 中将方法引用作为异步调用传递
- c# - Pdfsharp失去了文本块的不透明度
- typescript - 没有函数的接口和类型的类型参数推断
- postgresql - 比较两个表中的列并在 PostgreSQL 中合并值
- python - 使用python从图像中提取文本信息(城市名称)
- mysql - 我有一个 MySQL 表,我想在其中创建 67 列。有什么办法可以做到吗?我使用 XAMPP 作为服务器
- arrays - 数组 [1,2,3....n]。该系列中缺少一个数字。找出该数字的最佳方法是什么?
- php - 我正在创建一个连接到数据库的 php 文件。执行后抛出错误