title: How to use Java Arrays -- Translating From Ruby to Java
body: <p>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:</p>
  
  <pre><code>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 &lt;&lt; list.shuffle[5]
          end
          address &lt;&lt; ":"
      end
      return address[0..-2].to_sym
  end
  </code></pre>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <ol>
  <li><p>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...</p></li>
  <li><p>...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?</p></li>
  <li><p>etc.</p></li>
  </ol>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <pre><code>public ArrayList&lt;String&gt; shuffle(ArrayList&lt;String&gt; array) {
          Random generator = new Random();
          ArrayList&lt;String&gt; shuffled = new ArrayList&lt;String&gt;();
  
          for (int i = 0; i &gt; array.size(); i++) {
              int index = generator.nextInt(array.size());
  
              String popped = array.get(index);
              array.remove(index);
  
              shuffled.add(popped);
          }
  
  
          return shuffled;
      }
  </code></pre>