请澄清这一点.......
问题描述:
如果我包含console.writeline(),那么在这3种方法中没有获取最后的值...澄清...
if i include console.writeline() , then am not getting the last values in those 3 methods... clarify...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Array_List
{
class Program
{
static void Main(string[] args)
{
Program pg = new Program();
pg.HashTable();
Console.WriteLine();
pg.Dictionary();
Console.WriteLine();
pg.table();
Console.ReadLine();
}
public void HashTable()
{
Hashtable h = new Hashtable();
h.Add(1, "Sunday");
h.Add(2, "Monday");
h.Add(3, "tuesday");
h.Add(4, "Wednesday");
h.Add(5, "Thursday");
h.Add(6, "Friday");
h.Add(7, "Saturday");
//h.Add(8, " ");
for (int j = 1; j < h.Count; j++)
{
Console.WriteLine(h[j]);
}
Console.WriteLine();
// Console.ReadLine();
}
public void Dictionary()
{
Dictionary<int,> dict = new Dictionary<int,>();
dict.Add(1, "Jan");
dict.Add(2, "Feb");
dict.Add(3, "Mar");
dict.Add(4, "Apr");
dict.Add(5, "May");
dict.Add(6, "June");
dict.Add(7, "July");
dict.Add(8, "Aug");
dict.Add(9, "Sept");
dict.Add(10, "Oct");
dict.Add(11, "Nov");
dict.Add(12, "Dec");
for (int k = 1; k < dict.Count; k++)
{
Console.WriteLine(dict[k]);
}
Console.WriteLine();
// Console.ReadLine();
}
public void table()
{
ArrayList names = new ArrayList();
names.Add("ARUL");
names.Add("POO");
names.Add("KAYAL");
names.Add("MOM");
names.Add("DAD");
for (int i = 0; i < names.Count; i++)
{
Console.WriteLine(names[i]);
}
}
}
}
答
如果您的索引从1(哈希表和字典)开始,则您的迭代变量应该使用util到达最后一个元素:j <= h.Count
和.
If your indexes are starting from 1 (Hashtable and Dictionary) your iteration variable should go util reaches the last element:j <= h.Count
andk <= dict.Count
respectively.
使用foreach
语句来迭代您的集合,这样就不必关心索引了.
Use the foreach
statement to iterate your collections, then there is no need to be concerned with indices.