Reputation: 31
This is driving me crazy - How can I just get the application to print out exactly:
auto1
auto2
auto3
These are the objects in the array list I want to show. In my results all I get are the addresses of the three objects memory addresses.
Here is my code:
//Datenfelder
private ArrayList<Car> auto;
//End-Datenfelder
//Konstruktoren
CarCreator(){
auto = new ArrayList<Car>();
Car auto1 = new Car("Ferrari", "458 Italia", "Luxusklasse");
auto.add(auto1);
Car auto2 = new Car("Honda", "Civic", "Standardklasse");
auto.add(auto2);
Car auto3 = new Car("Mercedes-Benz", "C-Klasse", "Mittelklasse" );
auto.add(auto3);
}
//Ende-Konstruktoren
This is where the Objects were built. By default the constructor of the class will do so. The following method should now list all the objects by name (auto1 - auto3). I've created following method in order to get the objects dispalyed.
//Methoden
//generiere Liste mit der Anzahl Objekte
public void getCar(){
Iterator<Car> scrawler = auto.iterator();
while(scrawler.hasNext()){
Car carObject = scrawler.next();
System.out.println(carObject);
}
}
As mentioned, all I get are the addresses of the place in memory. I know it must work somehow the way I want I want it to, because yesterday I got the object names successfully shown.
cheers
Sunny
Upvotes: 0
Views: 67
Reputation: 3157
when you write Car auto1 you are telling the compiler "I need a variable of type car and I call this variable auto1"... but the compiler transform all of this "text" into bytecode so you will have no access to this information when the code is being executed.
When you call System.out.println(carObject);, the method "toString()" is being called. Java by default implements this method showing the pointer address (which actually is what you are seeing).
Try to add this on your Car class and let me know if helps you:
public class Car {
private String brand;
private String model;
public Car(String brand, String model) {
super();
this.brand = brand;
this.model = model;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", model=" + model + "]";
}
}
The last thing you should know is that the + operator on Strings and calling System.out has a cost on performance so, do this kind of things just for testing but if you want to leave traces on your code then user loggers and a better implementation for toString.
For last, one tip, if you are using eclipse you can click right button on your class --> Source --> generate toString()... which can save some time on this.
Upvotes: 0
Reputation: 1317
You expect that Java will print the variable names. This is wrong in so many ways...
A Car is an object. Your variable is a reference to that object. Many variables can point to that object, so there is no way you could ever know which variable you want to print (unless there is only one variable pointing to that object) and I don't think there is a way to get this information from the JVM.
Every object has a method toString(). The default implementation will print some strange hash. You can override this method to return a String that makes more sense for your object.
If you want the output you want, I could suggest doing something like
int i = 1;
for(Car car : auto) {
System.out.println("auto " + i);
}
Upvotes: 1
Reputation: 60818
Override toString()
in your derived class. The Object class implements a toString()
method that by default returns its memory address. You can override it to include more useful information.
Upvotes: 1
Reputation: 6560
You could write a method getName in the Object's class, then you could call carObject.getName();
That kind of method would look like this:
public String getName()
{
return name;
}
You could also create a toString method for the class and call that in the System.out.println when trying to print the info from the Object.
Upvotes: 0