001package com.hfg.util;
002
003
004import java.util.Random;
005
006//------------------------------------------------------------------------------
007/**
008 Functions to use with primitive arrays.
009
010 @author J. Alex Taylor, hairyfatguy.com
011 */
012//------------------------------------------------------------------------------
013// com.hfg XML/HTML Coding Library
014//
015// This library is free software; you can redistribute it and/or
016// modify it under the terms of the GNU Lesser General Public
017// License as published by the Free Software Foundation; either
018// version 2.1 of the License, or (at your option) any later version.
019//
020// This library is distributed in the hope that it will be useful,
021// but WITHOUT ANY WARRANTY; without even the implied warranty of
022// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
023// Lesser General Public License for more details.
024//
025// You should have received a copy of the GNU Lesser General Public
026// License along with this library; if not, write to the Free Software
027// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
028//
029// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
030// jataylor@hairyfatguy.com
031//------------------------------------------------------------------------------
032
033public class ArrayUtil
034{
035   //---------------------------------------------------------------------------
036   /**
037    Simple shuffle using the Fisher-Yates method.
038    * @param inArray primitive int array
039    */
040   public static void shuffle(int[] inArray)
041   {
042      int index;
043      int temp;
044      Random random = new Random();
045      for (int i = inArray.length - 1; i > 0; i--)
046      {
047         index = random.nextInt(i + 1);
048         // Swap values
049         temp         = inArray[index];
050         inArray[index] = inArray[i];
051         inArray[i]     = temp;
052      }
053   }
054
055   //---------------------------------------------------------------------------
056   /**
057    Simple shuffle using the Fisher-Yates method.
058    * @param inArray primitive float array
059    */
060   public static void shuffle(float[] inArray)
061   {
062      int   index;
063      float temp;
064      Random random = new Random();
065      for (int i = inArray.length - 1; i > 0; i--)
066      {
067         index = random.nextInt(i + 1);
068         // Swap values
069         temp         = inArray[index];
070         inArray[index] = inArray[i];
071         inArray[i]     = temp;
072      }
073   }
074
075   //---------------------------------------------------------------------------
076   /**
077    Simple shuffle using the Fisher-Yates method.
078    * @param inArray primitive char array
079    */
080   public static void shuffle(char[] inArray)
081   {
082      int   index;
083      char temp;
084      Random random = new Random();
085      for (int i = inArray.length - 1; i > 0; i--)
086      {
087         index = random.nextInt(i + 1);
088         // Swap values
089         temp         = inArray[index];
090         inArray[index] = inArray[i];
091         inArray[i]     = temp;
092      }
093   }
094}