Search code examples
javaarraysjgrasp

Returning String Arrays and Arrays operations in JAVA


I am writing this using JAVA. I am putting objects into my vehicleList array and I am having issues returning the array in my getVehicleList class. My vehicleList is an array of Vehicle[]. I keep getting "String cannot be converted to String[]. I hope I am making some sense. Here is my UseTaxList class. Any help would be much appreciated. Thank you.

This is what I have so far and I keep getting the error "String cannot be converted to String[]". I know what the error means, but I don't know how to resolve it.

public Vehicle[] getVehicleList() {

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

  return result;

}

Solution

  • If you simply want to convert all Elements from the vehicleList to String-Objects, you can write

    String[] result = new String[vehicleList.length];
    for (int i = 0; i < vehicleList.length; i++)
        result[i] = vehicleList[i].toString();
    
    return result;
    

    Simply write the code into your getVehicleList()-Method. You might have to add a custom toString()-Method in your Vehicle-Class!