Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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 如何将对象存储在我想要声明2个或更多对象的数组中,以及如何使用另一个类对象调用该数组_Java_Arrays - Fatal编程技术网

Java 如何将对象存储在我想要声明2个或更多对象的数组中,以及如何使用另一个类对象调用该数组

Java 如何将对象存储在我想要声明2个或更多对象的数组中,以及如何使用另一个类对象调用该数组,java,arrays,Java,Arrays,这是主要的方法 public static void main(String[] args) { Author[] authors=new Author[2]; for(int i=0;i<=authors.length;i++); authors[0]=new Author("suru","suru@qwe.com","m"); authors[1]=new Author("aksh","aksh@qwe.com","m

这是主要的方法

 public static void main(String[] args) {

        Author[] authors=new Author[2];
        for(int i=0;i<=authors.length;i++);
        authors[0]=new Author("suru","suru@qwe.com","m");
        authors[1]=new Author("aksh","aksh@qwe.com","m");
        for(int i=0;i<=authors.length;i++);
        System.out.println(authors);

        Book book=new Book("java",authors,200,2);
        System.out.println(book);
现在我创造了一本新的教科书

public class Book {
private String name;
private Author[] author;
private double price;
private int qty=0;

public Book(String name,Author[] author,double price, int qty) {
    this.name=name;
    this.author=author;
    this.price=price;
    this.qty=qty;
    } 

当我运行这个程序时,输出给出了内存地址,我可以打印这些详细信息吗?

您需要重写类
Author
中的
toString()
方法。 例如:

@Override
public String toString() {
    return "Author [name=" + name + ", email=" + email + ", gender=" + gender + "]";
}

您需要重写类
Author
中的
toString()
方法。 例如:

@Override
public String toString() {
    return "Author [name=" + name + ", email=" + email + ", gender=" + gender + "]";
}

当您作为变量名的方法
System.out.println()的参数传递时,将调用该变量类的方法
toString()
。如果在类
Author
Book
中不重写该方法,则调用这些类从
对象继承的
方法(Java中的所有类都从
对象继承)。默认情况下,此方法为未在其主体中定义的类打印内存中的地址。有一个简单的示例,您可以如何在
Author
方法中覆盖它:

class Author {
    @Override
    public String toString() {
        return "Author name: " + this.name + "\nAuthor email: " + this.email + "\nAuthor gender : " + this.gender;
}
要打印数组的内容(在您的示例中打印
Author[]authors
中包含的每个
Author
),您可能需要使用以下方法之一来实现这一点(因为
Author[]
Book[]
实际上是数组的一种类型,而不是
Book
Author
的一种类型,并且有自己的
toString)()
方法打印内存中的地址):

  • 创建一个循环,循环遍历
    authors
    数组的每个元素:

    for (Author author : authors) {
        System.out.println(author + "------"); // toString() method of each Author is called and added simple separator
    }
    
  • 调用
    Arrays.toString(authors)
    method.Class
    Arrays
    可让您以多种不同的方式操作数组。您可以在Java API文档中阅读有关此类的内容。这是文档中关于
    Arrays.toString()
    方法的一部分:

    class Author {
        @Override
        public String toString() {
            return "Author name: " + this.name + "\nAuthor email: " + this.email + "\nAuthor gender : " + this.gender;
    }
    
    返回指定数组内容的字符串表示形式。如果数组包含其他数组作为元素,则它们将由从Object继承的Object.toString()方法转换为字符串,该方法描述它们的标识,而不是它们的内容


  • 当您将变量名作为方法
    System.out.println()
    的参数传递时,将调用该变量类的方法
    toString()
    。如果未在类
    Author
    Book
    中重写该方法,
    toString()正在调用这些类从
    对象继承的
    方法(Java中的所有类都从
    对象继承)。默认情况下,此方法为具有
    toString()的类打印内存中的地址
    未在其主体中定义。有一个简单的示例,您可以如何在
    作者
    方法中覆盖它:

    class Author {
        @Override
        public String toString() {
            return "Author name: " + this.name + "\nAuthor email: " + this.email + "\nAuthor gender : " + this.gender;
    }
    
    要打印数组的内容(在您的示例中打印
    Author[]authors
    中包含的每个
    Author
    ),您可能需要使用以下方法之一来实现这一点(因为
    Author[]
    Book[]
    实际上是数组的一种类型,而不是
    Book
    Author
    的一种类型,并且有自己的
    toString)()
    方法打印内存中的地址):

  • 创建一个循环,循环遍历
    authors
    数组的每个元素:

    for (Author author : authors) {
        System.out.println(author + "------"); // toString() method of each Author is called and added simple separator
    }
    
  • 调用
    Arrays.toString(authors)
    method.Class
    Arrays
    可让您以多种不同的方式操作数组。您可以在Java API文档中阅读有关此类的内容。这是文档中关于
    Arrays.toString()
    方法的一部分:

    class Author {
        @Override
        public String toString() {
            return "Author name: " + this.name + "\nAuthor email: " + this.email + "\nAuthor gender : " + this.gender;
    }
    
    返回指定数组内容的字符串表示形式。如果数组包含其他数组作为元素,则它们将由从Object继承的Object.toString()方法转换为字符串,该方法描述它们的标识,而不是它们的内容

  • packageoops;
    公共班机{
    公共静态void main(字符串[]args){
    作者[]作者=新作者[2];
    对于(inti=0;i
    packageoops;
    公共班机{
    公共静态void main(字符串[]args){
    作者[]作者=新作者[2];
    对于(int i=0;i您可以重写toString(),也可以按如下方式直接打印对象的属性:

    Book b = new Book(/*args*/);
    System.out.println("Name: " + b.name);
    // continue printing all the attributes in the same way
    
    您可以尝试这样做,而不是重写对象类中的toString()方法。 希望这对您有帮助…

    您可以使用override toString()或直接打印对象的属性,如下所示:

    Book b = new Book(/*args*/);
    System.out.println("Name: " + b.name);
    // continue printing all the attributes in the same way
    
    您可以尝试这样做,而不是重写对象类中的toString()方法。
    希望这有帮助…

    我已经重写了toSting方法,但代码没有运行public String toString(){return name+“”+email+“”+gender+“”;我已经重写了toSting方法,但代码没有运行public String toString(){return name+“”+email+“”+gender+“”;@akshayshete这是什么意思?你会得到什么错误?它给java[Loops.Author;@1db9742 200.0 2这个output@akshayshete我编辑了我的答案,让你们知道你们应该以不同的方式打印数组:-)我忘了在我的第一篇文章中提到它attempt@akshayshete这是什么意思?你得到了什么错误?它给出了java[Loops.Author;@1db9742 200.0 2此output@akshayshete我编辑了我的答案,让您现在可以用不同的方式打印数组:-)我在第一次尝试时忘了提到它。不要在答案部分发布此代码,编辑您的问题,并在其中包含导致问题的部分(而不是全部代码,因为这是不必要的).在我阅读时,我强烈建议您花一些时间学习如何构建for循环、如何使用返回关键字以及如何使用数组索引,因为现在您的代码确实充满了错误…不要在“答案”部分发布此代码,编辑您的问题,并在其中包含导致问题的部分(而不是全部代码,因为这是不必要的).在我阅读时,我强烈建议您花一些时间学习如何构建for循环、如何使用return关键字以及如何使用ar