访问嵌套数组中的元素

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

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(&quot;John&quot;, &quot;Doe&quot;, &quot;1234&quot;, 45, 0.12f);
    regularArr[0] = new Customer(&quot;Caroline&quot;, &quot;Merritt&quot;, &quot;5678&quot;, 60);
    
    arr[0] = preferredArr;
    arr[1] = regularArr;
    
    System.out.println(&quot;preferredArr[0].getFirstName() = &quot;+preferredArr[0].getFirstName());
    
    System.out.println(&quot;arr[0][0].getFirstName() = &quot;+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>



huangapple
  • 本文由 发表于 2020年9月15日 22:01:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63903577.html
匿名

发表评论

匿名网友

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

确定