学习笔记(四):交错数组

一、定义格式:数据类型[][] 变量名称  (二维交错数组)

  例: int [][] arr;

     arr = new int [3][];

     arr[0] = new int [3]{ 1,2,3 };   //每一行都是一个数组

     arr[1] = new int [5]{ 1,2,3,4,5};

     arr[2] = new int [4]{ 1,2,3,4 };

     for( int i=0;i<=arr.GetUpperBound(0);i++)  //GetUpperBound(0) 获取数组的行的最大下标

     {

       for(int j=0;j<arr[i].Length;j++)

       {

         Console.Write(arr[i][j]+" ");

       }

       Console.WriteLine();

     }

    //用foreach方法

    foreach(int[] nums in arr )

    {

      foreach(int n in nums)

      {

        Console.Write(n+" ");

      }

      Console.WriteLine();

    }

 

二、公交查询系统

  1)输入站名,查询出经过该站的公交车

  2)输入车,查询出该车所经过的站

  3)输入起始站和终点站,查询所有满足的车。(提示:有可能倒车)

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

namespace lianxi
{
class Program
{
private static string[][] buses;
static void Main(string[] args)
{
Init();
/*1.输入站名,查询处经过该站的公交车
Console.WriteLine("请输入一个站名:");
string stationName = Console.ReadLine();
string[] bus = GetBusByStation(stationName);
foreach (string b in bus)
{
if (b != null)
{
Console.WriteLine(b);
}

}
*/

/*2.输入车,查询出该车经过所有的站
Console.WriteLine("请输入车次:");
string bus = Console.ReadLine();
string[] stations = GetStationByBus(bus);
foreach (string s in stations)
{
if (s != null)
{
Console.Write(s + ",");
}
}
Console.WriteLine();
*/

//3.输入起始站和终点站,查询所有满足的车。(提示:有可能倒车)
Console.WriteLine("请输入起始站:");
string startStation = Console.ReadLine();
Console.WriteLine("请输入终点站:");
string endStation = Console.ReadLine();
string[] startBus = GetBusByStation(startStation);
string[] endBus = GetBusByStation(endStation);
foreach (string sb in startBus)
{
if (sb == null)
{
break;
}
foreach (string eb in endBus)
{
if (eb == null)
{
break;
}
if (sb == eb)
{
Console.WriteLine("{0}路车可以直接经过{1}到{2}", sb, startStation, endStation);
}
else
{
string[] startStations = GetStationByBus(sb);
string[] endStations = GetStationByBus(eb);
foreach (string ss in startStations)
{
if (ss != null)
{
foreach (string es in endStations)
{
if (es != null)
{
if (ss == es)
{
Console.WriteLine("可以先坐{0}路车到{1},然后换乘{2}到终点站{3}",sb,ss,eb,endStation);
}
}
}
}
}
}
}
}
}
#region 初始化数组
/// <summary>
/// 初始化数组
/// </summary>
public static void Init()
{
buses = new string[3][];
buses[0] = new string[6] { "1", "张家口南站", "北方学院", "张家口北站", "烟厂", "展览馆" };
buses[1] = new string[7] { "7", "华联超市", "通泰集团", "北方学院", "钻石南路北口", "烟厂", "展览馆" };
buses[2] = new string[6] { "3", "钻石南路北口", "国土资源局", "茶坊", "红旗楼", "展览馆" };
}
#endregion

#region 根据车次,获取该车经过的车站
/// <summary>
/// 根据车次,获取该车经过的车站
/// </summary>
/// <param name="bus">车次</param>
/// <returns>该车经过的车站</returns>
public static string[] GetStationByBus(string bus)
{
string[] stations = new string[6];
int idx = 0;
for (int i = 0; i <= buses.GetUpperBound(0); i++)
{
for (int j = 0; j < buses[i].Length; j++)
{
if (buses[i][0] == bus && j > 0)
{
stations[idx++] = buses[i][j];
}
}
}
return stations;
}
#endregion

#region 根据站名,来得到经过该站的所有公交车
/// <summary>
/// 根据站名,来得到经过该站的所有公交车
/// </summary>
/// <param name="stationName"></param>
/// <returns></returns>
public static string[] GetBusByStation(string stationName)
{
string[] bus = new string[buses.GetUpperBound(0) + 1];
int idx = 0;
for (int i = 0; i <= buses.GetUpperBound(0); i++)
{
for (int j = 0; j < buses[i].Length; j++)
{
if (buses[i][j] == stationName)
{
bus[idx++] = buses[i][0];
}
}
}
return bus;
}
#endregion

}
}

posted @ 2011-10-09 22:48  L.M  阅读(471)  评论(0编辑  收藏  举报