Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Query not working in client's PC 1 35
How do ASP.NET and MVC work together? 4 29
Excel file not created as expected 7 50
C# Json POSt to Rest API 4 37
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 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

15 Experts available now in Live!

Get 1:1 Help Now