Временная сложность метода StringBuilder length () - PullRequest
1 голос
/ 19 июня 2020

Какова временная сложность метода length () из класса StringBuilder?

String str = "hello";
StringBuilder sb = new StringBuilder(str);

System.out.println(sb.length()); // should print 5

Мое предположение - O (n), где n - количество символов в строке, аналогично length () из String класс.

1 Ответ

1 голос
/ 19 июня 2020

Это переменная-член класса StringBuilder:

/**
 * The count is the number of characters used.
 */
int count;

А это код length():

/**
 * Returns the length (character count).
 *
 * @return  the length of the sequence of characters currently
 *          represented by this object
 */
@Override
public int length() {
    return count;
}

Как вы думаете?

...