Java/Collections Data Structure/Array SubArray

Материал из Java эксперт
Перейти к: навигация, поиск

Copies bytes from the source byte array to the destination array

 
/**********************************************************************************
 *
 * Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
 *                  Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
 *
 * Licensed under the Educational Community License Version 1.0 (the "License");
 * By obtaining, using and/or copying this Original Work, you agree that you have read,
 * understand, and will comply with the terms and conditions of the Educational Community License.
 * You may obtain a copy of the License at:
 *
 *      http://cvs.sakaiproject.org/licenses/license_1_0.html
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **********************************************************************************/

/**
 * Byte utilities
 */
public class ByteUtils {

  /**
   * Copies bytes from the source byte array to the destination array
   * 
   * @param source
   *          The source array
   * @param srcBegin
   *          Index of the first source byte to copy
   * @param srcEnd
   *          Index after the last source byte to copy
   * @param destination
   *          The destination array
   * @param dstBegin
   *          The starting offset in the destination array
   */
  public static void getBytes(byte[] source, int srcBegin, int srcEnd, byte[] destination,
      int dstBegin) {
    System.arraycopy(source, srcBegin, destination, dstBegin, srcEnd - srcBegin);
  }
  /**
   * Return a new byte array containing a sub-portion of the source array
   * 
   * @param srcBegin
   *          The beginning index (inclusive)
   * @param srcEnd
   *          The ending index (exclusive)
   * @return The new, populated byte array
   */
  public static byte[] subbytes(byte[] source, int srcBegin, int srcEnd) {
    byte destination[];
    destination = new byte[srcEnd - srcBegin];
    getBytes(source, srcBegin, srcEnd, destination, 0);
    return destination;
  }
  /**
   * Return a new byte array containing a sub-portion of the source array
   * 
   * @param srcBegin
   *          The beginning index (inclusive)
   * @return The new, populated byte array
   */
  public static byte[] subbytes(byte[] source, int srcBegin) {
    return subbytes(source, srcBegin, source.length);
  }
}





Exclude 2 arrays of ints

 
/*
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
 * 
 * Project: OpenSubsystems
 * 
 * $Id: ArrayUtils.java,v 1.9 2007/01/23 06:00:30 bastafidli Exp $
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

/**
 * Collection of useful utilities to work with arrays. 
 * 
 * @version $Id: ArrayUtils.java,v 1.9 2007/01/23 06:00:30 bastafidli Exp $
 * @author Peter Satury
 * @code.reviewer Miro Halas
 * @code.reviewed 1.5 2005/07/29 07:36:24 bastafidli
 */
public final class ArrayUtils
{
   /** 
    * Private constructor since this class cannot be instantiated
    */
   private ArrayUtils(
   )
   {
      // Do nothing
   }
   
   // Public methods ///////////////////////////////////////////////////////////
   /**
    * Method to exclude 2 arrays of ints so that the result contains all elements
    * from the first array, which are not in the second array.
    *  
    * @param arrBase - base array to exclude from 
    * @param arrExclude - array to exclude from the first one
    * @return int[] - array which contains all elements from the first array 
    *                 which are not in the second array or null
    */
   public static int[] exclude(
      int[] arrBase, 
      int[] arrExclude
   )
   {
      int[] arrReturn = null;
      if ((arrBase != null) && (arrBase.length > 0) && (arrExclude != null)
         && (arrExclude.length > 0))
      {
         int[] arrHelp;
         int   iCount1;
         int   iHelp;
         int   iLength = 0;
         
         arrHelp = new int[arrBase.length];
         for (iCount1 = 0; iCount1 < arrBase.length; iCount1++)
         {
            iHelp = arrBase[iCount1];
            if (ArrayUtils.contains(arrExclude, iHelp) == -1)
            {
               // If the element is not part of the second array then it should
               // be included in the result
               arrHelp[iLength++] = iHelp;
            }
         }
         
         // Shrink the array
         // TODO: Performance: Replace this with System.arraycopy
         arrReturn = new int[iLength];
         for (int iCount = 0; iCount < iLength; iCount++)
         {
            arrReturn[iCount] = arrHelp[iCount];
         }
      }
      else
      {
         arrReturn = arrBase;
      }
      
      return arrReturn;
   }
   
   /**
    * Test if specified array contains given element and if it does, find 
    * its position.
    * 
    * @param source - array to search, can be null
    * @param iTarget - element to find
    * @return int - -1 if it doesn"t exist there otherwise its position
    */
   public static int contains(
      int[] source,
      int   iTarget
   )
   {
      int iReturn = -1;
      
      if ((source != null) && (source.length > 0))
      {   
         int iIndex;
         
         for (iIndex = 0; iIndex < source.length; iIndex++)
         {
            if (source[iIndex] == iTarget)
            {
               iReturn = iIndex;
               break;
            }
         }
      }
      
      return iReturn;
   }
   /**
    * Sum all elements in the array.
    * 
    * @param source - array to sum elements of
    * @return long - sum of the elements in the array
    */
   public static long sum(
      int[] source
   )
   {
      int iReturn = 0;
      
      if ((source != null) && (source.length > 0))
      {   
         int iIndex;
         
         for (iIndex = 0; iIndex < source.length; iIndex++)
         {
            iReturn += source[iIndex];
         }
      }
      
      return iReturn;
   }
}





Gets the subarray from array that starts at offset.

 
/*
 * Copyright Aduna (http://www.aduna-software.ru/) (c) 1997-2006.
 *
 * Licensed under the Aduna BSD-style license.
 */

public class Utils {

  /**
   * Gets the subarray from <tt>array</tt> that starts at <tt>offset</tt>.
   */
  public static byte[] get(byte[] array, int offset) {
    return get(array, offset, array.length - offset);
  }
  /**
   * Gets the subarray of length <tt>length</tt> from <tt>array</tt>
   * that starts at <tt>offset</tt>.
   */
  public static byte[] get(byte[] array, int offset, int length) {
    byte[] result = new byte[length];
    System.arraycopy(array, offset, result, 0, length);
    return result;
  }

}





Gets the subarray of length length from array that starts at offset.

 
/*
 * Copyright Aduna (http://www.aduna-software.ru/) (c) 1997-2006.
 *
 * Licensed under the Aduna BSD-style license.
 */

public class Utils {

  /**
   * Gets the subarray of length <tt>length</tt> from <tt>array</tt>
   * that starts at <tt>offset</tt>.
   */
  public static byte[] get(byte[] array, int offset, int length) {
    byte[] result = new byte[length];
    System.arraycopy(array, offset, result, 0, length);
    return result;
  }

}





Puts the entire source array in the target array at offset offset.

 
/*
 * Copyright Aduna (http://www.aduna-software.ru/) (c) 1997-2006.
 *
 * Licensed under the Aduna BSD-style license.
 */

public class Utils {

  /**
   * Puts the entire <tt>source</tt> array in the <tt>target</tt>
   * array at offset <tt>offset</tt>.
   */
  public static void put(byte[] source, byte[] target, int offset) {
    System.arraycopy(source, 0, target, offset, source.length);
  }

}