英文:
Accessing an element inside an Array of an Array
问题
在arr
数组中,如果要访问John的信息,可以使用以下方式:arr[0][0].getFirstName()
。
英文:
Given the following class:
public class Customer {
protected String firstName;
protected String lastName;
protected String ID;
protected float amountSpent;
// Contructor
// Accessors and mutators
}
public class Gold extends Customer {
protected discount;
// overloaded contructor
// Accessors and mutator
}
and the following code
Customer[][] arr = new Customer[2][1];
Customer[] preferredArr = new Gold[3];
Customer[] regularArr = new Customer[3];
preferredArr[0] = new Gold("John", "Doe", "1234", 45, .12)
regularArr[0] = new Customer("Caroline", "Merritt", "5678", 60)
arr[0] = preferredArr;
arr[1] = regularArr;
How would I access John's information using preferredArr[0].getFirstName()
if it is inside of the arr
array. Also I can't use ArrayList as specified by my professor. Thanks for the help!
答案1
得分: 2
`preferredArr[0].getFirstName()`,可以工作是因为对象位于`preferredArr`中,确切地说是对象的引用。
`arr[0][0].getFirstName()`:同样也可以工作,因为在`arr[0][0]`中存储了相同对象的引用。
class Customer {
// ...(略去属性和构造函数部分)...
// 访问器方法
public String getFirstName()
{
return this.firstName;
}
}
class Gold extends Customer {
// ...(略去属性和构造函数部分)...
}
public class Main
{
public static void main(String[] args)
{
Customer[][] arr = new Customer[2][1];
Customer[] preferredArr = new Gold[3];
Customer[] regularArr = new Customer[3];
preferredArr[0] = new Gold("John", "Doe", "1234", 45, 0.12f);
regularArr[0] = new Customer("Caroline", "Merritt", "5678", 60);
arr[0] = preferredArr;
arr[1] = regularArr;
System.out.println("preferredArr[0].getFirstName() = " + preferredArr[0].getFirstName());
System.out.println("arr[0][0].getFirstName() = " + arr[0][0].getFirstName());
}
}
结果:
preferredArr[0].getFirstName() = John
arr[0][0].getFirstName() = John
你可以参考这个:两个引用指向同一对象。
以及 Java 中对象存储的位置。
祝你好运!
<details>
<summary>英文:</summary>
`preferredArr[0].getFirstName()`, work because the object is in preferredArr. to be exact the reference of the object.
`arr[0][0].getFirstName()`: also, work, because also, only the reference to the same object is stored in `arr[0][0]`.
class Customer {
protected String firstName;
protected String lastName;
protected String ID;
protected float amountSpent;
// Contructor
Customer(String firstName, String lastName, String ID, float amountSpent)
{
this.firstName = firstName;
this.lastName = lastName;
this.ID = ID;
this.amountSpent = amountSpent;
}
// Accessors and mutators
public String getFirstName()
{
return this.firstName;
}
}
class Gold extends Customer {
protected int discount;
// overloaded contructor
Gold(String firstName, String lastName, String ID, int discount, float amountSpent)
{
super(firstName, lastName, ID, amountSpent);
this.discount = discount;
}
// Accessors and mutator
}
public class Main
{
public static void main(String[] args)
{
Customer[][] arr = new Customer[2][1];
Customer[] preferredArr = new Gold[3];
Customer[] regularArr = new Customer[3];
preferredArr[0] = new Gold("John", "Doe", "1234", 45, 0.12f);
regularArr[0] = new Customer("Caroline", "Merritt", "5678", 60);
arr[0] = preferredArr;
arr[1] = regularArr;
System.out.println("preferredArr[0].getFirstName() = "+preferredArr[0].getFirstName());
System.out.println("arr[0][0].getFirstName() = "+arr[0][0].getFirstName());
}
}
The result :
preferredArr[0].getFirstName() = John
arr[0][0].getFirstName() = John
You can check this : [two references to the same object][1].
And [where the object in java are stored][2]
Good Luck.
[1]: https://stackoverflow.com/questions/8829139/java-creating-two-references-to-the-same-object
[2]: https://stackoverflow.com/questions/13624462/where-does-class-object-reference-variable-get-stored-in-java-in-heap-or-in-s#:~:text=All%20objects%20in%20Java%20are,Classes%20are%20also%20heap%20objects.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论