Solved

c# how do I cut down an array to a specific size?

Posted on 2011-09-27
4
572 Views
Last Modified: 2013-12-17
I want a function where I can pass in an array of strings and pass back an array of size 5 or smaller.  So if I pass in an array of size 100, return an array of size 5.  If I pass in an array of size 3, return the full array of size 3.
0
Comment
Question by:jackjohnson44
4 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 125 total points
ID: 36713303
It should be as simple as:
static string[] GetTrimmedArray(string[] theArr, int trimSize)
{
    if (theArr == null) throw new ArgumentException("Input array cannot be null.");

    if (trimSize < 0) throw new ArgumentException("Size of result array cannot be negative.");

    if (trimSize > theArr.Length) trimSize = theArr.Length;

    string[] result = new string[trimSize];

    for (int i = 0; i < trimSize; i++) { result[i] = theArr[i]; }

    return result;
}

Open in new window

0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 125 total points
ID: 36713304
static string[] YourFunction ( String[] x )
{

string[] y;
int count;

if ( x.Length > 5 )
   {
   count = 5;
   }
else
   {
   count = x.Length;
   }

y = new string[count];

for ( int index = 0; (index < count);  index++ )
   {
   y[index] = x[index];
   }

return y;

}

Open in new window

0
 
LVL 15

Accepted Solution

by:
x77 earned 125 total points
ID: 36713337
.Net Array hash method  Array.Resize.
It allows grow and also Trunc.

  array.resize(myArray, 5) - Now MyArray has 5 items. Itmaintains elements 0 to 4.
  Note that original array is not afected, we pass MyArray by Ref.
0
 
LVL 11

Assisted Solution

by:Sudhakar Pulivarthi
Sudhakar Pulivarthi earned 125 total points
ID: 36714444
hi,

If u do not know which all index the data is placed in an array, then u need to check through out the array and then construct a new array to the intended size.
static string[] TrimArray(string[] array, int trimSize)
        {
            // Perform primary validations on input parameters

            List<string> dataList = new List<string>();

            foreach (string data in array)
            {
                if (!string.IsNullOrEmpty(data))
                {
                    dataList.Add(data);

                    if (trimSize == dataList.Count)
                    {
                        // Max size for trim requested is attained.
                        break;
                    }
                }
            }

            return dataList.ToArray();
        }

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question