12

I know there are a lot of pages about this question, but I cannot understand it in my case.

I need to print the array of objects. For example, I have an array of objects that hold objects from the "shape" class. Do I call the toString method for each object in the array, or do I code the toString method in ObjectList to print out the instance variables? If so, how do I do that?

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 aiming for it to print this:

square has 4 sides
hexagon has 6 sides
2
  • If the array only holds Shapes, why not make it a Shape[] rather than Object[]? Also your add method should increase the value of numElement. Commented Apr 15, 2015 at 5:26
  • Oh yeah I forgot to add the numElement++, but I wanted to make it dynamic for all objects, and not just the Shape[] class. Commented Apr 15, 2015 at 5:39

4 Answers 4

16

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

Arrays.toString(myArray);

This will internally call the toString method of every element of your array.

So just override toString method in your Shape class and it should work fine.

To add further, override toString method in your class where you call Arrays.toString on your variable list :

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

    public String toString(){
         return Arrays.toString(list);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

The OP does not want to print all 10 objects, only the ones that have been added.
@pbabcdefp I guess that should be automatically handled by Arrays class
Downvoter, please comment, what was wrong with my answer
Thank you! This is exactly the answer.
I'm afraid it was me, sorry. The OP specified that only the 2 shapes added should be printed, whereas your answer prints the 2 shapes plus null 8 times.
4

You can do this with bellowed code, make for loop in toString method to print each shape object.

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);
    }
}

Comments

2

Write a for-each statement in toString() of Object List and create a large String with '\n' characters and return it as a String . Or may be name displayListElement() will be semantically more correct in which you can simple print all the Objects in the list .

Comments

0

Indeed, you should call toString method for each of objects that you want to print and join them together. You can use StringBuilder to hold the string-in-the-making as follows:

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 (e.g. numElements++) for each add operation as what pbabcdefp said in the comments. Also, you can use ArrayList class to manage "growing arrays".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.