当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Array Slice to create new array

问题描述:

How can I assign a slice of an array to a new array. I am trying to assign the maximal consecutive of equal elements to a new array.

I think I have the basic logic right but cannot slice to a new array. This is exercise 4 in Arrays - Fundamentals of computer programming with c#.

using System;

using System.Collections.Generic;

class ArrayEx4

{

static void Main()

{

int[] testArray = { 1, 1, 2, 3, 2, 2, 2, 1 }; // Output wanted {2,2,2};

int count = 0;

int bestCount = 0;

int startArray = 0;

int endArray = 0;

int first = 0;

for (int i = 0; i < testArray.Length; i++)

{

if (testArray[i] == testArray[i+1])

{

count += 1;

i = first;

if (count > bestCount)

{

count = bestCount;

startArray = first;

endArray = first + bestCount;

}

}

int[] bestArray = testArray.Slice(startArray, endArray);

Console.WriteLine("The best array is {0}", bestArray);

}

}

}

I am getting a directive error so I added using System.Collections.Generic; but I am still getting this error.

C:\Users\Sayth\Documents\Scripts>csc ArrayEx4.cs

Microsoft (R) Visual C# Compiler version 4.0.30319.33440

for Microsoft (R) .NET Framework 4.5

Copyright (C) Microsoft Corporation. All rights reserved.

ArrayEx4.cs(28,32): error CS1061: 'System.Array' does not contain a definition

for 'Slice' and no extension method 'Slice' accepting a first argument

of type 'System.Array' could be found (are you missing a using directive

or an assembly reference?)

EDIT

The logic in my solution above never worked. Here is the updated code including answer below as working.

using System;

using System.Linq;

class Arr4

{

static void Main()

{

int[] testArray = { 1, 1, 2, 3, 2, 2, 2, 1 }; // Output wanted {2,2,2};

int count = 0;

int bestCount = 0;

int start = 0;

int a = 0;

for(int i = 0; i < testArray.Length -1; i++)

{

while (testArray[i] == testArray[a])

{

count += 1;

a += 1;

if (count > bestCount)

{

start = i;

bestCount = count;

}

}

}

int end = bestCount + start;

Console.WriteLine("The start is {0} and count is {1}", start, bestCount);

var bestArray = testArray.Skip(start)

.Take(end - start)

.ToArray();

Console.Write("{ ");

Console.Write(string.Join(", ", bestArray));

Console.Write(" }");

}

}

网友答案:

Using Linq you could do this:

var bestArray = testArray.Skip(startArray) 
                         .Take(endArray - startArray)
                         .ToArray();

(Don't forget to add using System.Linq)

网友答案:

In c#, you don't need to loop through the array manually, you can just use LINQ.

This will get the job done for you :)

int[] testArray = { 1, 1, 2, 3, 2, 2, 2, 1 }; // Output wanted {2,2,2};

int[] maxSequence = testArray.Select((n, i) => new { Value = n, Index = i})
    .OrderBy(s => s.Value)
    .Select((o, i) => new { Value = o.Value, Diff = i - o.Index } )
    .GroupBy(s => new { s.Value, s.Diff})
    .OrderByDescending(g => g.Count())
    .First()
    .Select(f => f.Value)
    .ToArray();
分享给朋友:
您可能感兴趣的文章:
随机阅读: