要打印对象的内容?

时间:2017-05-26 07:10:59

标签: java println

我应该在代码中执行哪些更改,以便打印整个系列 试过toString,我只是变空了。这只是一个非常简单的代码soo plss help。

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by Alpit on 26-05-2017.
 */
public class Fam {
    String father;
    String mother;
    String sister;
    String brother;
String r;

    public Fam(String father, String sister, String brother, String mother) {
        this.father = father;
        this.sister = sister;
        this.brother = brother;
        this.mother = mother;
    }

    public String getFather() {
        return father;
    }

    public void setFather(String father) {
        this.father = father;
    }


}
class add {
    public static void main(String args[]) throws IOException {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<Object> arrayList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            String f = obj.readLine();
            String s = obj.readLine();
            String b = obj.readLine();
            String m = obj.readLine();
            Fam fam = new Fam(f, s, b, m);
            arrayList.add(fam);
        }
        for (Object x : arrayList) {

            System.out.println(String.valueOf(x));

        }
    }
}

我只获取了Object的地址,这个问题可以被认为是this question的副本,但我无法通过那里提供的解决方案来理解。

这是我再试一次的

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by Alpit on 26-05-2017.
 */
public class Fam {
    String father;
    String mother;
    String sister;
    String brother;
String r;

    public Fam(String father, String sister, String brother, String mother) {
        this.father = father;
        this.sister = sister;
        this.brother = brother;
        this.mother = mother;
    }

    public String getFather() {
        return father;
    }

    public void setFather(String father) {
        this.father = father;
    }

    public String toString()
    {
        return r;
    }


}
class add {
    public static void main(String args[]) throws IOException {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<Object> arrayList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            String f = obj.readLine();
            String s = obj.readLine();
            String b = obj.readLine();
            String m = obj.readLine();
            Fam fam = new Fam(f, s, b, m);
            arrayList.add(fam);
        }
        for (Object x : arrayList) {

            System.out.println(x.toString());

        }
    }
}

然后返回null。

4 个答案:

答案 0 :(得分:2)

您可以在Class Fam中覆盖Object中的toString()。

答案 1 :(得分:2)

String.valueOf(object)的作用是它调用Object类的toString()方法(在你的情况下是Fam)。

 public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();

}

所以你需要覆盖Fam类中的toString()方法,如下所示:

public class Fam {
String father;
String mother;
String sister;
String brother;
String r;
public Fam(String father, String sister, String brother, String mother) {
    this.father = father;
    this.sister = sister;
    this.brother = brother;
    this.mother = mother;
}

.
.
.

@Override
public String toString() {
    return this.father +" "+this.mother +" "+ this.sister +" "+ this.brother;
}

此外,您需要在主方法中进行此更改。

        ArrayList<Fam> arrayList = new ArrayList<Fam>();

这样您就可以打印对象了。 (PS:您可以在toString()方法中更改返回格式。)

答案 2 :(得分:2)

实现自己的toString()方法。做这样的事情:

class Fam {
    String father;
    String mother;
    String sister;
    String brother;
    String r;

    public Fam(String father, String sister, String brother, String mother) {
        this.father = father;
        this.sister = sister;
        this.brother = brother;
        this.mother = mother;
    }

    public String getFather() {
        return father;
    }

    public void setFather(String father) {
        this.father = father;
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(this.father);
        builder.append(" ");
        builder.append(this.sister);
        builder.append(" ");
        builder.append(this.brother);
        builder.append(" ");
        builder.append(this.mother);

        return builder.toString();
    }


} 

class add {
    public static void main(String args[]) throws IOException {
        Fam family = new Fam("father", "sister", "brother", "mother");

        System.out.println(family.toString());
    }
}

我已经在这里实现了toString()。我使用了StringBuilder来演示如何创建自己的方法,在父亲,姐妹,兄弟,母亲之间插入一个空格。

跑步你得到:

father sister brother mother

答案 3 :(得分:1)

您的ArrayList的类型参数为Object。这很好,但是当您尝试在for循环中打印数据时,您需要将x转换为Fam。将其声明为ArrayList<Fam> arrayList = new ArrayList<>()会更容易。此外,您不能以您尝试的方式打印对象。对象引用保存内存地址的值。您需要使用Fam类中的getter方法手动打印数据。如果需要,您也可以覆盖toString()方法来执行此操作。