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 8996831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:48:26+00:00 2026-06-15T23:48:26+00:00

I’m just starting with Java, so I’m transcribing some of my Ruby stuff. I

  • 0

I’m just starting with Java, so I’m transcribing some of my Ruby stuff. I have a Ruby function like this, which generates something that looks like an IPv6 address:

def generate_ip()
    list = ["a", "b", "c", "d", "e", "f", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
    address = ""
    8.times do
        4.times do
            address << list.shuffle[5]
        end
        address << ":"
    end
    return address[0..-2].to_sym
end

This method simply takes an array of strings and then repeatedly jumbles them up, choosing the 5th element each time to construct the returned string.

As far as I can tell, there’s no “shuffle()” method for String Arrays in Java, so I figured I’d write my own. But then I started to get completely mired in sub-problems, such as:

  1. Apparently there’s no built-in way to remove items from a String Array, which seems strange — is that correct? I saw several other SO posts where the responses for this features were a 10-line block of code. So I decided to use ArrayLists, since according to the doc, they have sane methods like size(), get(), and remove(), although…

  2. …I haven’t found a way to instantiate them using a literal, or to add multiple items with a single method call. Every tutorial I see (including the official Oracle Java Tutorial) uses multiple calls to ArrayList.add(). That can’t be the way to do it, can it?

  3. etc.

So before I go spiraling down into sub-problem land and asking 50 different questions, I thought I’d ask how to solve the most important problem. Is there a simple way to translate the above Ruby code into Java?

FWIW, my in-progress “shuffle” method is below. I’m just looping array.length times, copying/removing a random element from the old array to a new array each time.

public ArrayList<String> shuffle(ArrayList<String> array) {
        Random generator = new Random();
        ArrayList<String> shuffled = new ArrayList<String>();

        for (int i = 0; i > array.size(); i++) {
            int index = generator.nextInt(array.size());

            String popped = array.get(index);
            array.remove(index);

            shuffled.add(popped);
        }


        return shuffled;
    }
  • 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-15T23:48:27+00:00Added an answer on June 15, 2026 at 11:48 pm

    If you create a List (either ArrayList or LinkedList), you can use Collections.shuffle()

    ArrayList and LinkedList are both implementations of the List interface. I suspect either would be ok for your needs here, although the ArrayList would be preferable (being backed by an array). Here’s the Java List tutorial.

    Arrays are of a fixed length in Java, whereas a List is variable in size, and so the List is more appropriate in general for your above conversion. To create a list with a given set of starting elements, see Arrays.asList() e.g.

    List stooges = Arrays.asList("Larry", "Moe", "Curly");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have been unable to fix a problem with Java Unicode and encoding. The
I have two tables with like below codes: Table: Accounts id | username |

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.