0

I am currently using Java and I am trying to get my getVehicleList method to print out its contents from Vehicle[] vehicleList. However, when I call my getVehicleList, it returns LVehicle;@61443d8f instead.

I think this is the memory address if I am correct. I am not sure if this is relevant, but here is an example of what it should print out. Thank you.

Jones, Jo: Car 2014 Honda Accord (Alternative Fuel)
Value: $22,000.00 Use Tax: $110.00
with Tax Rate: 0.005

Jones, Sam: Car 2014 Honda Accord
Value: $22,000.00 Use Tax: $220.00
with Tax Rate: 0.01

Here is the code that returns the memory address instead.

public Vehicle[] getVehicleList() {

  Vehicle[] result = new Vehicle[vehicleList.length];
  for (int i = 0; i < vehicleList.length; i++) {
     result[i] = vehicleList[i];
  }

  return result;

}

This is what it prints instead.

LVehicle;@61443d8f

Here is my toString()

public String toString() {

  String result = "";
  for (int i = 0; i < vehicleList.length; i++) {
     result += vehicleList[i] + "\n\n";
  }

     return result;

 }
  • 1) Why don't you just return `vehicleList`? 2) You need to implement a `toString()` method in your `Vehicle` class then iterate through the array printing each one or use `Arrays.toString()`. – takendarkk Dec 06 '14 at 03:59
  • related [Java: Syntax and meaning behind “[B@1ef9157”? Binary/Address?](http://stackoverflow.com/questions/1040868/java-syntax-and-meaning-behind-b1ef9157-binary-address) – zapl Dec 06 '14 at 04:00

1 Answers1

2

You can implement

public String toString(){
}

in the Vehicle class

Since JVM does not know how you want to present the information inside your Vehicle class, you have to tell it by implementing the toString() method

Patrick Chan
  • 1,019
  • 10
  • 14