SL8R.co.uk - Code Zone

Code, Code and Code

SL8R.co.uk - Code Zone header image 2

Java: Mix up items in array

May 20th, 2008 · No Comments

Quick routine to mix up the items in an array

public Object[] MixUpArray(Object inarray[])
{
  // get the size of the array
  int iSize = inarray.length;
  // create an array the same size as the input array
  Object outarray[] = new Object[iSize];
  // Convert the array to a Vector
  Vector<Object> temparray = new Vector<Object>();
  // Copy the array to the Vector
  for ( int i=0; i < iSize; i++ ) temp.add(a[i]);
  // perform the mix up
  for ( int i=0; i < iSize; i++ )
  {
  // Choose a random Vector item
  int iRandom = (int)(Math.random() * iSize);
  // store the chosen vector item in the array
  outarray[i] = temparray.getElementAt(iRandom);
  // remove the item from the vector
  temparray.remove(iRandom);
  }
  // return the new array
  return outarray;
} // method MixUpArray

Tags: Code

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment