Ensures that a given array can hold up to minCapacity elements. : String Array « Data Types « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Data Types » String ArrayScreenshots 
Ensures that a given array can hold up to minCapacity elements.
    

/*
 Copyright 1999 CERN - European Organization for Nuclear Research.
 Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
 is hereby granted without fee, provided that the above copyright notice appear in all copies and 
 that both that copyright notice and this permission notice appear in supporting documentation. 
 CERN makes no representations about the suitability of this software for any purpose. 
 It is provided "as is" without expressed or implied warranty.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace DiscoveryLogic.Common.Numeric
{
    public class Arrays : System.Object
    {
        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static sbyte[] ensureCapacity(sbyte[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            sbyte[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 31;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new sbyte[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }    

        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static short[] ensureCapacity(short[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            short[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 31;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new short[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }
        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static bool[] ensureCapacity(bool[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            bool[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 31;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new bool[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }    
        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static char[] ensureCapacity(char[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            char[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 31;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new char[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }

        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static double[] ensureCapacity(double[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            double[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 31;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new double[newCapacity];
                //for (int i = oldCapacity; --i >= 0; ) newArray[i] = array[i];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }        

        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static float[] ensureCapacity(float[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            float[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 31;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new float[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }
        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static int[] ensureCapacity(int[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            int[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 31;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new int[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }        


        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static long[] ensureCapacity(long[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            long[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 31;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new long[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }            

        /// <summary> Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
        /// 
        /// Returns the identical array if it can hold at least the number of elements specified.
        /// Otherwise, returns a new array with increased capacity containing the same elements, ensuring  
        /// that it can hold at least the number of elements specified by 
        /// the minimum capacity argument.
        /// 
        /// </summary>
        /// <param name="minCapacity">  the desired minimum capacity.
        /// </param>
        public static System.Object[] ensureCapacity(System.Object[] array, int minCapacity)
        {
            int oldCapacity = array.Length;
            System.Object[] newArray;
            if (minCapacity > oldCapacity)
            {
                int newCapacity = (oldCapacity * 31;
                if (newCapacity < minCapacity)
                {
                    newCapacity = minCapacity;
                }

                newArray = new System.Object[newCapacity];
                Array.Copy(array, 0, newArray, 0, oldCapacity);
            }
            else
            {
                newArray = array;
            }
            return newArray;
        }
    }
}

   
    
    
    
  
Related examples in the same category
1.Generates a hashcode for the string array
2.removes the specified strings in the string array from the input string
3.Count how many times a word appears in an array of words.
4.Find all unique words in an array of words.
5.Gets an array of sentences from a string.
6.Array To New Line Separated String
7.New Line Separated String To Array
8.returns the elements of the array as a string, delimited with the default delimitor
9.Strings to byte array.
10.String Array To String
11.Processes a string and returns the arguments in an array.
12.Demonstrate string arraysDemonstrate string arrays
13.String To Char ArrayString To Char Array
14.Comparing string to char array
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.