-2

I am wondering if I can use for each loop to iterate through the object of a class. for instance, I created an object honda of type car. I append all the state of the car such as model, price, color, etc. to it. I want to print all the instance of the car. I don't want to use the print statement to print each instance . what is the best way to do it? Thanks.

Below is my java code

package classandobjects;

public class MainClass {

    public static void main(String[] args) {

        Classes friend1 = new Classes();
        friend1.name="yusuf";
        friend1.age=27;
        friend1.country="Nigeria";
        friend1.department="EE";
        friend1.gender="male";

        Classes friend2 = new Classes();
        friend1.name="mathew";
        friend1.age=30;
        friend1.country="Nigeria";
        friend1.department="EE";
        friend1.gender="male";

        FavPlayers player = new FavPlayers();
        player.pname="J.Terry";
        player.position="C.Back";
        player.gaols=38;
        player.awards="25-awards";

        FavPlayers player1 = new FavPlayers();
        player1.pname="F.Lampard";
        player.position="Midfield";
        player.gaols=50;
        player.awards="10-awards";

        Car model = new Car();
        model.modelName="Honda Civic";
        model.color="Ash-color";
        model.Doors="4-doors";
        model.price=900000;

        System.out.println("below is my friend information");
        System.out.println(friend1.name);
        System.out.println(friend1.age);
    }
}

Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27
Sirmee
  • 79
  • 4

2 Answers2

2

You had better override a toString method in the Car class and simply print it like:

System.out.println(model);

Indeed, you needn't print each instance variable separately. Your toString may have the following view:

public @Override String toString() {
    return modelName + " [" + color + ... + "]"; // also consider StringBuilder
}

If you came from languages where an object is an associative array (e.g. JavaScript) and asked how to print an instance using a foreach loop in Java, then the answer is that Java doesn't allow it (it is possible by reflection though) and you cannot iterate over object variables through a foreach.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
0

I don't want to use the print statement to print each instance

Well, you're going to have to build up a string and print it somewhere.

what is the best way to do it?

If I understand correctly, you want to print an object?

Then implement a toString() method.

public class Classes {
    // other code 
    public String toString() {
        // change how you want 
        return this.name + ", " + this.age; 
    } 
} 

Then you can do

System.out.println(friend1);

If you want to print a list of objects, then you need a list to loop over. You could use reflection to get a list of object fields and values, but that seems unnecessary.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245