对象数组,其中的变量来自另一个类。

huangapple go评论71阅读模式
英文:

Object array with variable from another class

问题

以下是你提供的代码的翻译部分:

My AnyClass

import java.util.Scanner;

public class AnyClass {
    public int seqNo;

    /* -----------------构造函数------------*/
    public AnyClass(int num) {
        seqNo = num; // 初始化
    }

    // 空构造函数
    public AnyClass() {

    }

    // 初始化
    public void initializeseqNo(int seqNum) {
        seqNum = seqNo;
    }

    /*-----方法*/
    public String getData() {
        return "序列号 " + seqNo + "。";
    }

    public String getKey() {
        return String.valueOf(seqNo); // 根据序列号进行搜索
    }

    public void editData() // 空方法,将来会被子类重写
    {

    }

    public void edit() {
        Scanner sc = new Scanner(System.in);
        seqNo = sc.nextInt();// 下一行是用于字符串的
    }
}

My Node class

public class Node {
    public AnyClass obj;

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

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

MainProg

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

        //-----------构造对象---------
        Person head = new Person("Gatt", 21445667);
        Person clerk = new Person();
        clerk.name = "Delia";

        System.out.println("新头部的数据:" + head.getData());

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

        ac1.getData();
        ac2.getData();
        ac3.getData();

        // 编辑 ac1 的值
        ac1.edit();

        // 再次打印所有值
        ac1.getData();
        ac2.getData();
        ac3.getData();

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

        // 打印值
        n.show();

        Node nodObj[] = new Node[5]; // 为数组分配内存

        // 填充数组
        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));

        // 打印数组
        for (int i = 0; i < nodObj.length; i++) {
            nodObj[i].show();
        }
    }
}
英文:

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

But then the question continues populate the array by objects with seqNo values assigned to 1,2,3,4,5 respectively. Then traverse the array and print the list of its object using method 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. Does someone have any idea if I should do a loop. When i tried if loop I got an error too. I have the display part code wrong I can't figure out what to change

My 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 &quot;Sequence number &quot; +seqNo+&quot;.&quot;;
}
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 (&quot;Gatt&quot;, 21445667);
Person clerk = new Person();
clerk.name = &quot;Delia&quot;;
System.out.println (&quot;Data of a new Head: &quot; +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 

答案1

得分: 0

是的,你需要对数组进行循环。在这个指导水平上,你应该使用for循环或foreach循环。

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

或者

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

> Below I have all the code except the Person class. Does someone have
> any idea if I should do a loop. When i tried if loop I got an error
> too. 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 (int index = 0; index &lt; nodObj.length; index++) {
nodObj[index].show();
}

Or

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

huangapple
  • 本文由 发表于 2020年10月28日 03:25:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64561557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定