Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-27
4
Medium Priority
?
600 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 500 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 500 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 500 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

971 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