Java printing of an array of objects

I know there are many pages to this question, but I cannot figure it out in my case.

I need to print an array of objects. For example, I have an array of objects that contain objects from the shape class. Am I calling the toString method for each object in an array, or the toString method code in an ObjectList to print instance variables? If so, how to do it?

public class Shape{
    private String shapeName;
    private int numSides;

    public String toString(){
        return shapeName + " has " + numSides + " sides.";
    }
}

public class ObjectList{
    private Object[] list = new Object[10];
    private int numElement = 0;

    public void add(Object next){
        list[numElement] = next;
    }

    public String toString(){
        // prints out the array of objects 

        // do I call the toString() method from the object?

        // or do I print the instance variables? What am I printing?

        // I'm guessing I do a for loop here
    }
}

public class Driver{
    public static void main(String[] args){
        ObjectList list = new ObjectList();
        Shape square = new Shape("square", 4);
        Shape hex = new Shape("hexagon", 6);
        list.add(square);
        list.toString();  // prints out array of objects
}

I am going to print this:

square has 4 sides
hexagon has 6 sides
+4
source share
4 answers

The easiest way to do this is to use Arrays.toString:

Arrays.toString(myArray);

This will internally call a method toStringfor each element of your array.

toString Shape, .

, toString , Arrays.toString list:

public class ObjectList{
    private Object[] list = new Object[10];
    .............

    public String toString(){
         return Arrays.toString(list);
    }
}
+5

, toString .

class Shape{
    private String shapeName;
    private int numSides;

    Shape(String shapeName, int numSides){
        this.shapeName = shapeName;
        this.numSides = numSides;
    }
    public String toString(){
        return shapeName + " has " + numSides + " sides.";
    }
}

class ObjectList{
    private Object[] list = new Object[10];
    private int numElement = 0;

    public void add(Object next){
        list[numElement] = next;
        numElement++;
    }

    @Override
    public String toString(){
        String str="";
        int i=0;
        while(list[i] != null){
            str += list[i]+"\n";
            i++;
        }
        return str;
    }
}

public class Driver{
    public static void main(String[] args){
        ObjectList list = new ObjectList();
        Shape square = new Shape("square", 4);
        Shape hex = new Shape("hexagon", 6);
        list.add(hex);
        list.add(square);
        System.out.println(list);
    }
}
+4

inString() "\n" . displayListElement() , .

+1

, toString , , . StringBuilder :

public String toString() {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i <= numElements; i++) {
        result.append(list.toString() + "\n");
    }

    return result.toString();
}

Note that you need to increase numElements(for example numElements++) for each operation add, as noted in the pbabcdefp comments. In addition, you can use the class ArrayListto manage "growing arrays."

0
source

All Articles