Java 如何将链表从一个方法传递或检索到另一个方法?
首先,我想从Java 如何将链表从一个方法传递或检索到另一个方法?,java,linked-list,Java,Linked List,首先,我想从AddPatient()方法中检索患者链接列表,并将其显示在ListPatient()方法中 我尝试通过更改public static void ListPatient()来检索;方法以公共静态作废ListPatient(ListInterface patient)但它不起作用 package dsa; import dsa.LList; import dsa.ListInterface; import java.sql.Time; import java.util.Date; im
AddPatient()
方法中检索患者链接列表,并将其显示在ListPatient()
方法中
我尝试通过更改public static void ListPatient()
来检索;方法以公共静态作废ListPatient(ListInterface patient)
但它不起作用
package dsa;
import dsa.LList;
import dsa.ListInterface;
import java.sql.Time;
import java.util.Date;
import java.util.Scanner;
public class EmergencyClinic {
public static void main(String[] args){
MainMenu();
}
public static void MainMenu(){
int n = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to Emergency Clinic!");
System.out.println("1. Add Patient");
System.out.println("2. Serve Patient");
System.out.println("3. List Patient");
System.out.print("Please choose your option :");
n = scan.nextInt();
switch(n){
case 1: AddPatient();
break;
case 2: ServePatient();
break;
case 3: ListPatient();
break;
default : System.out.println("Sorry! Invalid Input. Returning to main menu...\n"); MainMenu();
break;
}
}
public static void AddPatient(){
ListInterface<PatientDetails> patient = new LList<PatientDetails>();
Scanner scan = new Scanner(System.in);
int num=0;
System.out.print("Please Enter Name :");
String name = scan.nextLine();
System.out.print("Please Enter IC No :");
String ic = scan.nextLine();
System.out.print("Please Enter Contact Number :");
String contactNum = scan.nextLine();
System.out.print("Please Enter Gender :");
String gender = scan.nextLine();
Date date = new Date();
Long time = date.getTime();
System.out.print("Please Enter Reason :");
String reason = scan.nextLine();
System.out.print("Please Enter Seriousness :");
String seriousness = scan.nextLine();
if(patient.isEmpty())
{
patient.add(new PatientDetails(name, ic, contactNum, gender,date ,time ,reason,seriousness ));
}
MainMenu();
}
public static void ServePatient(){
}
public static void ListPatient(){
ListInterface<PatientDetails> patient = new LList<PatientDetails>();
System.out.println(patient.getLength());
if (!patient.isEmpty())
{
for(int i=0;i<patient.getLength();i++){
patient.getEntry(i);
}
}
else
{
System.out.println("Error in list patients!");
}
}
}
包dsa;
导入dsa.LList;
导入dsa.ListInterface;
导入java.sql.Time;
导入java.util.Date;
导入java.util.Scanner;
公共类紧急循环{
公共静态void main(字符串[]args){
主菜单();
}
公共静态void主菜单(){
int n=0;
扫描仪扫描=新扫描仪(System.in);
System.out.println(“欢迎来到急诊室!”);
System.out.println(“1.添加患者”);
系统输出打印(“2.服务患者”);
System.out.println(“3.列出患者”);
System.out.print(“请选择您的选项:”);
n=scan.nextInt();
开关(n){
案例1:AddPatient();
打破
案例2:ServePatient();
打破
案例3:ListPatient();
打破
默认值:System.out.println(“对不起!输入无效。返回主菜单…\n”);MainMenu();
打破
}
}
公共静态void AddPatient(){
ListInterface patient=new-LList();
扫描仪扫描=新扫描仪(System.in);
int num=0;
系统输出打印(“请输入名称:”);
字符串名称=scan.nextLine();
系统输出打印(“请输入IC编号”);
字符串ic=scan.nextLine();
系统输出打印(“请输入联系人号码:”);
String contactNum=scan.nextLine();
系统输出打印(“请输入性别:”;
字符串性别=scan.nextLine();
日期=新日期();
Long time=date.getTime();
系统输出打印(“请输入原因:”);
字符串原因=scan.nextLine();
系统输出打印(“请输入严重性:”);
String=scan.nextLine();
if(patient.isEmpty())
{
添加(新患者详细信息(姓名、ic、联系人编号、性别、日期、时间、原因、严重性));
}
主菜单();
}
publicstaticvoidservepatient(){
}
公共静态无效列表患者(){
ListInterface patient=new-LList();
System.out.println(patient.getLength());
如果(!patient.isEmpty())
{
对于(int i=0;i所有方法都标记为void。这意味着没有返回值。有人可能会说,它们是过程,而不是函数。
如果要返回列表,必须更改签名:
public static List AddPatient()
然后可以使用关键字return从方法返回列表
括号()中的参数都是输入参数
这是方法/函数的一个非常基本的概念。我建议读者阅读一本书,了解Java的基本原理
Java也有它自己的通用实现。如果对它的实现没有任何特殊要求,你应该使用它。似乎add、list和serve是三个函数。你所有的方法都是静态的,那么你需要一个静态的PatientList
变量。也就是说,当用户选择add
时,他当他选择列表中的dded元素时,将显示相同的列表对象
在类中的代码中声明:
private static ListInterface<PatientDetails> patient = new LList<PatientDetails>();
private static ListInterface patient=new-LList();
在add and list方法中,直接使用此变量。您知道方法签名中的void
是什么意思吗?您知道方法参数是什么吗?
private static ListInterface<PatientDetails> patient = new LList<PatientDetails>();