0

This is my code. Trying to create a method call getVehicles that will convert the arrayList of vehicles to an array and then returns the array.

public class GSM{
private ArrayList <Vehicles> vehicleList = new ArrayList <Vehicles>;
private String garageName;

public GSM (String garageName)
{
    this.garageName = garageName;
}
public void addVehicle(Vehicles A)
{
    vehicleList.add(A)
}

public Vehicles[] v getVehicles()
{

}

I am new to ArrayList and Arrays, so I am having a bit of problem with this one.

  • In the olden days before `Stream`s, we used [`Arrays.asList`](https://docs.oracle.com/javase/10/docs/api/java/util/Arrays.html#asList(T...)) – MadProgrammer Mar 02 '20 at 00:33
  • 1
    Does this answer your question? [Converting 'ArrayList to 'String\[\]' in Java](https://stackoverflow.com/questions/4042434/converting-arrayliststring-to-string-in-java) – andrewJames Mar 02 '20 at 01:10

1 Answers1

2
Vehicles[] arr = vehicleList.toArray(new Vehicles[0]);

See this answer for explanation: Converting 'ArrayList<String> to 'String[]' in Java

Domin0
  • 197
  • 2
  • 11