Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 访问数组中的数组内的元素_Java_Arrays - Fatal编程技术网

Java 访问数组中的数组内的元素

Java 访问数组中的数组内的元素,java,arrays,Java,Arrays,鉴于以下类别: public class Customer { protected String firstName; protected String lastName; protected String ID; protected float amountSpent; // Contructor // Accessors and mutators } public class Gold extends Customer { pro

鉴于以下类别:

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
}

和下面的代码

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;

如果John的信息位于
arr
数组中,如何使用
preferredArr[0].getFirstName()
访问该信息。我也不能按照教授的要求使用ArrayList。谢谢你的帮助

preferredArr[0]。getFirstName()
可以工作,因为对象位于preferredArr中。确切地说是对象的引用。
arr[0][0].getFirstName()
:还有,工作,因为在
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());   

    }
}

结果是:

preferredArr[0].getFirstName() = John                                                           
arr[0][0].getFirstName() = John
您可以检查:。


祝你好运。

我建议你
arr[0][0]。getFirstName()
但是你必须确保
preferedArr
在启动你的程序时会停留在索引0处。