I have an String array called myArray (for the sake of argument, let's just say it contains words from a story). I want to pass this array to a method that will sort them alphabetically and analyze the words. I've looked this up on SO and many people have suggested for this scenario to use java.util.Arrays.sort(myArray). So I used this line in my method, passed in myArray, and performed calculations on it, etc etc.
However, it recently came to my attention that this will permanently sort myArray. That is, the array will still be sorted after I come out of the method. Is there a way for me to only sort the array within the scope of the method?
Example code:
public static double uniqueWords(String[] doc1) {
java.util.Arrays.sort(doc1)
... // count up the number of unique words in this array
return COUNT_OF_UNIQUE_WORDS;
}
public static void main(String[] args) {
String[] document;
... // put values in the array
System.out.println(uniqueWords(document));
System.out.println(java.util.Arrays.toString(document)); // here the array will still be sorted, which I DON'T want
}