英文:
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 "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
答案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 < nodObj.length; index++) {
nodObj[index].show();
}
Or
for (Node node : nodObj) {
node.show();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论