Solved

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

Posted on 2011-09-27
4
569 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 74

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now