Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

converting arraylist to array

ChrissyChrissy Posts: 9Member
I have an arraylist and I'm trying to convert it to a double array. How do I do this? Here is what I have so far: Thanks for any help!

StreamWriter swa=new StreamWriter("c:\afinal.txt");
StreamReader sra=new StreamReader("c:\Acolumn.txt");
string aLine="";


ArrayList aColumn = new ArrayList ();
while (aLine != null)
{
aLine=sra.ReadLine();
if (aLine != null)
aColumn.Add(aLine);
Console.WriteLine(aLine);
}

Console.Read();
sra.Close();

object [] AField = aColumn.ToArray ();
Console.WriteLine (AField);
Console.ReadLine();

Comments

  • BaldusariusBaldusarius Posts: 162Member
    : I have an arraylist and I'm trying to convert it to a double array. How do I do this? Here is what I have so far: Thanks for any help!
    :
    : StreamWriter swa=new StreamWriter("c:\afinal.txt");
    : StreamReader sra=new StreamReader("c:\Acolumn.txt");
    : string aLine="";
    :
    :
    : ArrayList aColumn = new ArrayList ();
    : while (aLine != null)
    : {
    : aLine=sra.ReadLine();
    : if (aLine != null)
    : aColumn.Add(aLine);
    : Console.WriteLine(aLine);
    : }
    :
    : Console.Read();
    : sra.Close();
    :
    : object [] AField = aColumn.ToArray ();
    : Console.WriteLine (AField);
    : Console.ReadLine();
    :
    :

    The syntax to convert an ArrayList to a strongly-typed array is:
    [code]
    TargetType[] arr = (TargetType[])list.ToArray(typeof(TargetType));

    // for example
    string[] strArray = (string[])myList.ToArray(typeof(string));

    // or

    double[] dblArray = (double[])myList.ToArray(typeof(double));[/code]
  • ChrissyChrissy Posts: 9Member
    Okay, I used that syntax and I got a runtime error that said at least one element in the source array could not be cast down to the destination type. Here is the only difference in my code:

    double[] AField = (double[])aColumn.ToArray (typeof (double));
    Console.WriteLine (AField);

    : : I have an arraylist and I'm trying to convert it to a double array. How do I do this? Here is what I have so far: Thanks for any help!
    : :
    : : StreamWriter swa=new StreamWriter("c:\afinal.txt");
    : : StreamReader sra=new StreamReader("c:\Acolumn.txt");
    : : string aLine="";
    : :
    : :
    : : ArrayList aColumn = new ArrayList ();
    : : while (aLine != null)
    : : {
    : : aLine=sra.ReadLine();
    : : if (aLine != null)
    : : aColumn.Add(aLine);
    : : Console.WriteLine(aLine);
    : : }
    : :
    : : Console.Read();
    : : sra.Close();
    : :
    : : object [] AField = aColumn.ToArray ();
    : : Console.WriteLine (AField);
    : : Console.ReadLine();
    : :
    : :
    :
    : The syntax to convert an ArrayList to a strongly-typed array is:
    : [code]
    : TargetType[] arr = (TargetType[])list.ToArray(typeof(TargetType));
    :
    : // for example
    : string[] strArray = (string[])myList.ToArray(typeof(string));
    :
    : // or
    :
    : double[] dblArray = (double[])myList.ToArray(typeof(double));[/code]
    :

  • BaldusariusBaldusarius Posts: 162Member
    When you call ArrayList.ToArray, the ArrayList casts the objects it holds to the target type.

    StreamReader.Readline returns a string, which you are adding to the ArrayList. Since there is no implicit conversion defined between string and double, the call is failing.

    The solution is to only add the type you're planning on casting to. You can do that by calling double.Parse(string) or using System.Convert class. Those will throw exceptions if the string cannot be converted, so you can either catch that exception and ignore that particular value or test the string using a regular expression to make sure the call will succeed.


  • ChrissyChrissy Posts: 9Member
    Okay, please bear with me, I'm still very new at programming in general, let alone c#. Here is my code now. I am getting an error that says the input string was not in a correct format. I dont understand why. I believe my code reads in a text file of numbers into a string arraylist, then I'm not sure what it does from there.
    Thanks so much for any help!

    using System;
    using System.IO;
    using System.Collections;
    namespace Program2
    {
    class Class1
    {
    static public void Main(string[] args)
    {
    StreamWriter swa=new StreamWriter("c:\afinal.txt");
    StreamReader sra=new StreamReader("c:\AColumn.txt");
    string aLine="";

    ArrayList aColumn = new ArrayList ();
    while (aLine != null)
    {
    aLine=sra.ReadLine();
    if (aLine != null)
    aColumn.Add(aLine);

    Console.WriteLine(aLine);

    }

    Console.ReadLine();
    sra.Close();
    double pline;
    double[] AField = new double[aColumn.Count];
    for ( int i = 0; i < AField.Length; i++ )
    {
    foreach (string line in aColumn)
    pline = double.Parse((string) line);
    Console.WriteLine (AField [i]);
    }


    Console.ReadLine();
    swa.Flush();
    swa.Close();
    }
    }
    }
  • BaldusariusBaldusarius Posts: 162Member
    The best advice I can give a new programmer is to get to know your debugger. Doing so will help you more easily identify where your code is going wrong and will help you learn quite a bit about how your application executes. I am constantly learning new things when taking the time to step through someone else's (not to mention my own) code.

    When your application throws the exception, hit the "Break" button and take a look at the "Autos" window. If it's not visible, you can show it by selecting Debug->Windows->Autos or hitting Ctrl+Alt+V, then A.

    The line the debugger stopped on will be:[code]pline = double.Parse((string) line);[/code]If you look at the current value of the "line" variable in the Autos window, the problem should be apparent. As I mentioned before, double.Parse will throw an exception if the string passed to it cannot be converted. That includes empty strings.

    I hope that solves your problem.

    : Okay, please bear with me, I'm still very new at programming in general, let alone c#. Here is my code now. I am getting an error that says the input string was not in a correct format. I dont understand why. I believe my code reads in a text file of numbers into a string arraylist, then I'm not sure what it does from there.
    : Thanks so much for any help!
    :
    : using System;
    : using System.IO;
    : using System.Collections;
    : namespace Program2
    : {
    : class Class1
    : {
    : static public void Main(string[] args)
    : {
    : StreamWriter swa=new StreamWriter("c:\afinal.txt");
    : StreamReader sra=new StreamReader("c:\AColumn.txt");
    : string aLine="";
    :
    : ArrayList aColumn = new ArrayList ();
    : while (aLine != null)
    : {
    : aLine=sra.ReadLine();
    : if (aLine != null)
    : aColumn.Add(aLine);
    :
    : Console.WriteLine(aLine);
    :
    : }
    :
    : Console.ReadLine();
    : sra.Close();
    : double pline;
    : double[] AField = new double[aColumn.Count];
    : for ( int i = 0; i < AField.Length; i++ )
    : {
    : foreach (string line in aColumn)
    : pline = double.Parse((string) line);
    : Console.WriteLine (AField [i]);
    : }
    :
    :
    : Console.ReadLine();
    : swa.Flush();
    : swa.Close();
    : }
    : }
    : }
    :

  • ajikoeajikoe Posts: 4Member
    you can convert the ArrayList value into double first than you than transfer it into Array.
    double[] dou = new double[aColumn.Count];
    for(int i=0; i<caldou.Count; i++){
    dou[i] = Double.Parse(aColumn[i].ToString());
    }

    Sincerely Yours,
    Pujo Aji

    : I have an arraylist and I'm trying to convert it to a double array. How do I do this? Here is what I have so far: Thanks for any help!
    :
    : StreamWriter swa=new StreamWriter("c:\afinal.txt");
    : StreamReader sra=new StreamReader("c:\Acolumn.txt");
    : string aLine="";
    :
    :
    : ArrayList aColumn = new ArrayList ();
    : while (aLine != null)
    : {
    : aLine=sra.ReadLine();
    : if (aLine != null)
    : aColumn.Add(aLine);
    : Console.WriteLine(aLine);
    : }
    :
    : Console.Read();
    : sra.Close();
    :
    : object [] AField = aColumn.ToArray ();
    : Console.WriteLine (AField);
    : Console.ReadLine();
    :
    :

Sign In or Register to comment.