简体   繁体   English

具有来自另一个类的变量的对象数组

[英]Object array with variable from another class

Worksheet Question:工作表问题:

The question said to declare an array of 5 objects of the class Node in the main Class - I managed to this as shown below该问题说要在主类中声明类 Node 的 5 个对象的数组- 我设法做到了这一点,如下所示

But then the question continues populate the array by objects with seqNo values assigned to 1,2,3,4,5 respectively.但是随后问题继续通过将 seqNo 值分别分配给 1,2,3,4,5 的对象填充数组。 Then traverse the array and print the list of its object using method show()然后遍历数组并使用方法 show() 打印其对象的列表

I have an error when I am trying to show the array to the user.当我尝试向用户显示数组时出现错误。 I am trying to display the array by this line of code:我试图通过这行代码显示数组:

nodObj[].show();

Below I have all the code except the Person class.下面我有除 Person 类之外的所有代码。 Does someone have any idea if I should do a loop.有人知道我是否应该做一个循环。 When i tried if loop I got an error too.当我尝试 if 循环时,我也遇到了错误。 I have the display part code wrong I can't figure out what to change我的显示部分代码错误 我不知道要更改什么

My AnyClass我的 AnyClass

import java.util.Scanner;
public class AnyClass
{
public int seqNo;

/* -----------------Constructor------------*/
public  AnyClass(int num) 
{
     seqNo = num; //initializing 
}
//empty constructor
public  AnyClass() 
{
     
}
//intialized    
public void initializeseqNo(int seqNum)
{
    seqNum = seqNo;
}
/*-----Mehtods*/
public String getData()
{return "Sequence number " +seqNo+".";
}

public String getKey()
{
    return String.valueOf(seqNo); //for search by seqNo
}

public void editData() //empty method to be overriden by future subcclasses
{
    
}

public void edit(){
    Scanner sc = new Scanner(System.in);
    seqNo = sc.nextInt();//next line for String
}

} //end of AnyClass

My Node class我的节点类

public class Node
{
public AnyClass obj;

public Node(AnyClass newObj)
{ 
    obj = newObj;
}

public void show()
{
    System.out.println(obj.getData());
}
}

MainProg主程序

class MainProg{
    public static void main (String[] args) {

        //-----------Construction of objects---------
        Person head = new Person ("Gatt", 21445667);
        Person clerk = new Person();
        clerk.name = "Delia";

        System.out.println ("Data of a new Head: " +head.getData());

        AnyClass ac1 = new AnyClass(51);
        AnyClass ac2 = new AnyClass(52);
        AnyClass ac3 = new AnyClass(53);

        ac1.getData();
        ac2.getData();
        ac3.getData();
        
        
        //edit value of ac1
        ac1.edit();
        
        //print all values again
        ac1.getData();
        ac2.getData();
        ac3.getData();

        Node n = new Node(new AnyClass(3));

        //print values
        n.show();

        Node nodObj[] = new Node[5]; //allocating memory to array
        
        //populate array
        nodObj[0] = new Node(new AnyClass(1));
        nodObj[1] = new Node(new AnyClass(2));
        nodObj[2] = new Node(new AnyClass(3));
        nodObj[3] = new Node(new AnyClass(4));
        nodObj[4] = new Node(new AnyClass(5));

        //printing array
        
        nodObj[].show(); //ERROR THIS IS WRONG!

        
    }//end of Main()
}//end of program class 



           

Below I have all the code except the Person class.下面我有除 Person 类之外的所有代码。 Does someone have any idea if I should do a loop.有人知道我是否应该做一个循环。 When i tried if loop I got an error too.当我尝试 if 循环时,我也遇到了错误。 I have the display part code wrong I can't figure out what to change我的显示部分代码错误 我不知道要更改什么

Yes, you need to loop over the array.是的,您需要遍历数组。 At this level of instruction you should use a for or foreach loop.在此级别的指令中,您应该使用 for 或 foreach 循环。

for (int index = 0; index < nodObj.length; index++) {
  nodObj[index].show();
}

Or或者

for (Node node : nodObj) {
  node.show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM