Array List
-
Hi Everybody, I wrote a class like this public class Node { private int data; private string key; #region Constructors public Node(string key, int data) { this.key = key; this.data = data; } #endregion #region Public Properties public int Value { get { return data; } set { data = value; } } public string Key { get { return key; } set { key = value; } } #endregion } public class test { public static void Main() { ArrayList a = new ArrayList(); Node n = new Node("ab", 1); a.Add(n); PrintValues(a); } public static void PrintValues( IEnumerable myList ) { System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "\t{0}", myEnumerator.Current.ToString()); Console.WriteLine(); } } My problem is How do I get and set values for each node in this Array List and How do I print out to the screen. Thanks for your help and time. Raj
-
Hi Everybody, I wrote a class like this public class Node { private int data; private string key; #region Constructors public Node(string key, int data) { this.key = key; this.data = data; } #endregion #region Public Properties public int Value { get { return data; } set { data = value; } } public string Key { get { return key; } set { key = value; } } #endregion } public class test { public static void Main() { ArrayList a = new ArrayList(); Node n = new Node("ab", 1); a.Add(n); PrintValues(a); } public static void PrintValues( IEnumerable myList ) { System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "\t{0}", myEnumerator.Current.ToString()); Console.WriteLine(); } } My problem is How do I get and set values for each node in this Array List and How do I print out to the screen. Thanks for your help and time. Raj
You have to cast the elements of your ArrayList.
public static void PrintValues( IEnumerable myList ) { System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); while ( myEnumerator.MoveNext() ) { Console.Write( "\t{0}", ((Node) myEnumerator.Current).Key.ToString()); Console.WriteLine(); } }