2010-12-23 34 views
1

配列の各要素に対して、配列の長さの最後までSeat1、Seat2、Seat 3 .........などの一意の識別子が必要です。2次元配列

現在、私は次のことを行っている:

int rows = 10, cols = 10; 
bool[ , ] seatArray = new bool[rows , cols]; //10 rows, 10 collums 

for (int i = 0; i < rows; i++) 
    for (int j = 0; j < cols; j++) 
    { 
     seatArray[i, j] = false; 
    } 

    foreach (bool element in seatArray) 
    { 
     Console.WriteLine("element {0}", element); 
    } 
} 

この単純にコンソールで100 x "の要素がFalse" と言います。

"Element"をSeat1、Seat2、Seat3 ....と置き換える必要があります。

ご協力いただければ幸いです!

ありがとうございました!

+1

あなたはどの言語を書いていますか? –

+0

Cシャープ。ありがとうございました – Simagen

答えて

4

IDと占有(?)プロパティを持つSeatクラス(または必要に応じて構造)を作成します。この型の配列を作成します。

public class Seat 
{ 
    public string ID { get; set; } 
    public bool Occupied { get; set; } 
} 

int rows = 10, cols = 10; 
Seat[,] seats = new Seat[rows,cols]; 

for (int i = 0; i < rows; ++i) 
{ 
    for (int j = 0; j < cols; ++j) 
    { 
     seats[i,j] = new Seat { ID = "Seat" + (i*cols + j), Occupied = false }; 
    } 
} 

foreach (var seat in seats) 
{ 
    Console.WriteLine("{0} is{1} occupied", seat.ID, seat.Occupied ? "" : " not"); 
} 
0
int count = 1; 

for (int i = 0; i < rows; i++) 
    for (int j = 0; j < cols; j++) 
    { 
    seatArray[i, j] = count; 
    count++; 
    } 

    foreach (bool element in seatArray) 
    { 
    Console.WriteLine("element {0}", element); 
    } 

これがそう構文をIDKのが、ちょうどそれらに番号を付けるために、いくつかの外部カウンタを行う

ちょうどあなたがfalseに一人一人を設定するたびに偽の言うこと、ブール値を使用していけないされてどのような言語は考えていません、または真偽と番号情報

0

tvanfossonを保持するクラスを作成し、私はあなたのコーディング作業をするのに苦労しています、IV私のメインメソッドオフ新しいクラスに入れて下記を参照:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication2 
{ 
    class Class1 
    { 
     public class Seat 
      { 
       public string ID { get; set; } 
       public bool Occupied { get; set; } 
      } 

      int rows = 10, cols = 10; 
      Seat[,] seats = new Seat[rows,cols]; 

      for (int i = 0; i < rows; ++i) 
      { 
       for (int j = 0; j < cols; ++j) 
       { 
        seats[i,j] = new Seat { ID = "Seat" + (i*cols + j), Occupied = false }; 
       } 
      } 

      foreach (var seat in seats) 
      { 
       Console.WriteLine("{0} is{1} occupied", seat.ID, seat.Occupied ? "" : " not"); 
      } 
    } 
} 

これは正しいですが、私は多くの構文エラーを受けているようです。

ありがとう!