[英]how to pass a int array value to another class in java
我有三个类,一个是链接列表类,一个LinkLLiat1类和一个LinkList2类。任何一个都可以帮助我将getStudentID值传递给链接列表类。
这是LinkList1类
public class LinkList1
{
public LinkList1 next;
private int EmployeeID;
private String EmployeeName;
private String EmpContactNo;
private String DOB;
private String DateofJoined;
private String DepatmentName;
public LinkList1 (int Employeeid, String Employeename, String EmpContactno, String dob, String Dateofjoined, String Depatmentname)
{
EmployeeID=Employeeid; // initialized class variable
EmployeeName=Employeename; // initialized class variable
EmpContactNo=EmpContactno; // initialized class variable
DOB=dob; // initialized class variable
DateofJoined=Dateofjoined; // initialized class variable
DepatmentName=Depatmentname; // initialized class variable
}
public void DisplayLink()
{
System.out.println("("+EmployeeID+","+EmployeeName+","+EmpContactNo+","+DOB
+","+DateofJoined+","+DepatmentName.toUpperCase()+")");
}
public int getStudentID()
{
return EmployeeID;
}
}
这是在LinkList2中。
public class LinkList2
{
private LinkList1 first; // refer tp first link on the list
public LinkList2 () // constructor
{
first = null; // no items on list yet
}
public boolean empty()
{
return (first==null);
}
public void insert(int Employeeid, String Employeename, String EmpContactno, String dob, String Dateofjoined, String Depatmentname)
{
LinkList1 newLinkList1= new LinkList1(Employeeid, Employeename, EmpContactno, dob, Dateofjoined, Depatmentname);
newLinkList1.next= first; // newlink --> old first
first = newLinkList1; // first --> new link
}
public LinkList1 delete() // elete first item
{
LinkList1 tem=first; //save reference to link
first = first.next; //selete it: first--> old next
return tem; // return deleted link
}
public int[] myListStudent(LinkList2 thelist)
{
int myStudentNo[]=new int[5];
LinkList1 current = first;
int i=0;
while (thelist!=null) // until end of list
{
int a=current.getStudentID();
myStudentNo[i]=current.getStudentID();
current = current.next; //move to next link
}
return myStudentNo;
}
public void displaylist()
{
System.out.println(" ID, Name, Contact No" +
"Date of Birth. Date of Joined, Work Department ID");
LinkList1 current = first; // start at beginning of list
while (current!=null) // until end of list
{
current.DisplayLink(); //print data
current = current.next; //move to next link
}
System.out.println("");
}
}
我需要将上述类的getStudentID值获取到位于mainlinklist类中的myArray。
public class LinkList
{
public static void main(String[] args)
{
LinkList2 thelist = new LinkList2(); // make new list
thelist.insert(1, "ssd", "071123456", "9.10.1993", "10.11.2010", "HR");
thelist.insert(12, "dcfs", "071123456", "9.10.1993", "10.11.2010", "ACC");
thelist.displaylist();
LinkList2 myStudent = new LinkList2();
int myArray[];
myArray=myStudent.myListStudent(thelist);
while(!thelist.empty())
{
LinkList1 aLink = thelist.delete();
System.out.println("Deleted");
aLink.DisplayLink();
System.out.println();
System.out.println("");
}
}
}
我想做的是将LinkList1中的EmployeeID vlues链接到LinkList2和Linklist ..我想收集值EmployeeID ..我不知道我在做什么,完全是错误的.. :)这意味着下面的myStudentNo到链接列表中的MyArray。
public int[] myListStudent(LinkList2 thelist)
{
int myStudentNo[]=new int[5];
LinkList1 current = first;
int i=0;
while (thelist!=null) // until end of list
{
int a=current.getStudentID();
myStudentNo[i]=current.getStudentID();
current = current.next; //move to next link
}
return myStudentNo;
}
您可以通过构造函数传递它。
例如。 当您创建类的实例时
CLASS name = new CLASS(variableToPass);
更新:
创建要传递给该类的实例时,请将该变量用作该类的构造函数的参数。 我希望现在更容易理解。
创建类的新实例时,将调用构造函数。
这是构造函数的示例:
public ClassName() {
}
该构造函数不带参数,因此创建该类的实例如下:
ClassName class = new ClassName();
构造函数还可以接受参数并将其设置为值:
int i = 0;
public ClassName(int i){
this.i = i;
}
ClassName class = new ClassName(1);
这会将i的实例变量设置为给定i的值。 这个。 引用实例变量。
您应该以这种方式添加要通过构造函数传递的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.