I have a class with a getAvailableRobot
(ArrayList
) method and was wondering what the time complexity of the method is. I understand that if available was just an int it would be O(1), but since it is an ArrayList
, will it still be O(1), or will it return a new list with the help of an iterator making it O(n)?
public class Robot {
protected List<Robot> available;
public List<Robot> getAvailableRobots(){
return available;
}
}
What I'm basically asking for is the runtime for this line in another method:
List<Robot> newList = getAvailableRobots();
CodePudding user response:
When you return a collection like this it isn't copied, you just return a reference to the same object. In other words, the execution time of this method isn't affected by the size of the list, so ot will also have an O(1) time complexity.