2010-12-23 145 views
1

对于数组中的每个元素,我需要一个唯一的标识符,例如Seat1,Seat2,Seat 3 .......一直到数组长度的末尾。二维数组

目前我也做了以下内容:

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控制台。

我需要用Seat1,Seat2,Seat3 ....替换“元素”到数组长度的末尾。

任何帮助将不胜感激!

谢谢!

+1

你在写什么语言? – 2010-12-23 18:32:04

回答

4

使用ID和占用(?)属性创建座位类(或结构,如果更合适的话)。制作这种类型的数组。

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的语法,但只是做一些外部柜台它们编号每次你设定的时间

,只是说虚假的每一个错误,不使用布尔,或者写一个类来保存真假和数量信息

0

tvanfosson,我努力让你的编码工作,四把它变成一个新的班级我的主要方法参见下文:

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"); 
      } 
    } 
} 

这是正确的,因为我似乎收到很多语法错误

谢谢!