Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if i include console.writeline() , then am not getting the last values in those 3 methods... clarify...

C#
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]);
                
            }

        }


    }
}
Posted
Updated 14-Sep-12 23:18pm
v2

If your indexes are starting from 1 (Hashtable and Dictionary) your iteration variable should go util reaches the last element: j <= h.Count and k <= dict.Count respectively.
 
Share this answer
 
Comments
arul mozhi 15-Sep-12 5:32am    
how to get all values including spaces?
Zoltán Zörgő 15-Sep-12 5:37am    
I don't understand - what do you mean by "spaces"? If you change the two for statements as I suggested, you will get all values. And you will get two empty rows between them, since you added a Console.WriteLine() at the end of the methods, and between the calls also.


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");
for (int j = 1; j <= h.Count; j++)
{
Console.WriteLine(h[j]);
}
}

public void Dictionary()
{
Dictionary<int,string> dict = new Dictionary<int,string>();
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]);
}
}

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]);
}
}
}
}
Zoltán Zörgő 15-Sep-12 5:45am    
If you got your answer feel free to accept the solution(s) you found useful.
arul mozhi 15-Sep-12 5:40am    
ya got the expected answer ... Thanku so much
Zoltán Zörgő 15-Sep-12 5:43am    
This will also work:
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");
foreach(DictionaryEntry s in h)
{
Console.WriteLine(s.Value);
}
}

public void Dictionary()
{
Dictionary<int,string> dict = new Dictionary<int,string>();
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");
foreach(KeyValuePair<int,string> s in dict)
{
Console.WriteLine(s.Value);
}
}

public void table()
{
ArrayList names = new ArrayList();

names.Add("ARUL");
names.Add("POO");
names.Add("KAYAL");
names.Add("MOM");
names.Add("DAD");
foreach(string s in names)
{
Console.WriteLine(s);
}
}
}
}

(As Richard also mentioned before I have posted this)
Use the foreach statement to iterate your collections, then there is no need to be concerned with indices.
 
Share this answer
 
Comments
arul mozhi 15-Sep-12 5:50am    
hm... thanku
arul mozhi 15-Sep-12 6:04am    
y am getting the value in reverse order for hash table function using foreach

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900