2017-04-23 4 views
-2

Ich habe ein 1D-Array:C#: Split 1D Array in 2D-Array

string[] Technology = new [] {Smartphone, Laptop, Tablet, Desktop, Server, Mainframe}; 

Wie kann ich das in zwei Hälften geteilt und setzen die beiden Teile in eine größere 2D-Array, so dass die folgenden beiden Rück true:

// categorizedTechnology[0].SequenceEquals(new [] {Smartphone, Laptop, Tablet}); 
// categorizedTechnology[1].SequenceEquals(new [] {Desktop, Server, Mainframe}); 
+0

Was die Logik verwendet wird, aussehen würde Ihre Technologie zu kategorisieren? – squillman

+0

@squillman Das Array wird einfach in zwei – ComputersAreCool

Antwort

0

Wenn Sie genau 6 Artikel Sie können dies tun:

string[] Technology = new string[6] { "Smartphone", "Laptop", "Tablet", "Desktop", "Server", "Mainframe" }; 

var firstChunk = Technology.Take(3); 
var secondChunk = Technology.Skip(3).Take(3); 

string[,] array2D = new string[,] { 
    { firstChunk.First(), firstChunk.Skip(1).First(), firstChunk.Last()}, 
    { secondChunk.First(), secondChunk.Skip(1).First(), secondChunk.Last()} }; 

oder einfach tun:

string[,] array2D = new string[,] { 
     { Technology[0], Technology[1], Technology[2]}, 
     { Technology[3], Technology[4], Technology[5]} }; 
+0

aufgeteilt, die ihm zwei eindimensionale Arrays, nicht eine zweidimensionale – Nino

+0

@Nino Dank geben wird. Bearbeitete meine Antwort. – CodingYoshi

+0

@CodingYoshi Danke CodierungYoshi! – ComputersAreCool

0

Wenn Sie dies explizit angeben möchten, wie viele Elemente in welchen Bereich verschoben werden, müssen Sie ein anderes Array erstellen, das die Kategorie enthält. Ich wähle ein Array von "bool", um anzugeben, ob jeder Gegenstand tragbar ist oder nicht. Dann habe ich die beiden Arrays gezippt und gefiltert, ob sie portabel sind oder nicht.

string[] technology = new string[] { "Smartphone", "Laptop", "Tablet", "Desktop", "Server", "Mainframe" }; 
bool[] isPortable = new bool[] { true, true, true, false, false, false }; 
string[][] categorizedTechnology = new string[][] 
{ 
    technology.Zip(isPortable, (tech, test)=> test?tech: null).Where(tech=> tech!=null).ToArray(), 
    technology.Zip(isPortable, (tech, test)=> test?null: tech).Where(tech=> tech!=null).ToArray() 
}; 

Aber das ist immer noch nicht das richtige was dabei zu tun. Hier ist objektorientierte Programmierung wichtig, da die Technologie Kategorieinformation ist muss zu einer einzigen Einheit wie eine Klasse oder Struktur kombiniert werden.

Dies ist, wie es mit Klassen und LINQ

public class Technology 
{ 
    public string Name { get; set; } 
    public bool IsPortable { get; set; } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     Technology[] list = new Technology[] { 
      new Technology() { Name="Smartphone", IsPortable=true }, 
      new Technology() { Name="Laptop", IsPortable=true }, 
      new Technology() { Name="Tablet", IsPortable=true }, 
      new Technology() { Name="Desktop", IsPortable=false }, 
      new Technology() { Name="Server", IsPortable=false }, 
      new Technology() { Name="Mainframe", IsPortable=false }, 
     }; 

     Technology[][] groupedTechnology 
      = list.GroupBy((tech) => tech.IsPortable) 
       .Select((group) => group.ToArray()).ToArray(); 
    } 
}