Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8821161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:47:02+00:00 2026-06-14T05:47:02+00:00

I have an String array called myArray (for the sake of argument, let’s just

  • 0

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:47:04+00:00Added an answer on June 14, 2026 at 5:47 am
    String temp[] = java.util.Arrays.copyOf(doc1,doc1.length);
    java.util.Arrays.sort(temp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

lets say we have a string[] array = {telekinesis, laureate, Allequalsfive, Indulgence}; and we
I have a string array variable called 'myAttachmentArray[]' which holds different figures like this:
friends. I have an array and it contains some string values. ex: array name=All_array
I have a 2D array called results. Each row array in results contains both
I have got a 2d array called $myarray, and when I using var_dump($myarray), it
I have a custom class in Obj-C called RouteManager which contains an array of
I have an activity that pulls a String Array from xml and displays a
I have a ListAdapter that contains a String array. When I click on any
I have a struct called member . Within member I have a std::string array
I have an array called $featuresSEO that has a number of words in it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.