Reputation: 13
What is the run time (with regard to Big O) of the Arrays.toString() method in Java?
e.g.
String sort(String x){
char[] xArray = x.toCharArray();
Arrays.sort(xArray);
return Arrays.toString(xArray);
Upvotes: 0
Views: 1735
Reputation: 21
Array.toString() method, appends each of the array elements to a string builder using a for loop and returns the final string. So, if the size of the array is n, the new string will also be of size n.
So, it takes O(n) time to print the string representation of the array and O(n) space to create a new string of length n.
Time Complexity: O(n) Space Complexity: O(n)
Upvotes: 2