简体   繁体   中英

Java: Time complexity getMethod()

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM