C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
converting arraylist to array Posted by Chrissy on 1 Oct 2004 at 11:06 AM
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();

Report
Re: converting arraylist to array Posted by Baldusarius on 3 Oct 2004 at 4:57 PM
: 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:
TargetType[] arr = (TargetType[])list.ToArray(typeof(TargetType));

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

// or

double[] dblArray = (double[])myList.ToArray(typeof(double));

Report
Re: converting arraylist to array Posted by Chrissy on 6 Oct 2004 at 7:56 AM
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:
:
: TargetType[] arr = (TargetType[])list.ToArray(typeof(TargetType));
: 
: // for example
: string[] strArray = (string[])myList.ToArray(typeof(string));
: 
: // or
: 
: double[] dblArray = (double[])myList.ToArray(typeof(double));

:

Report
Re: converting arraylist to array Posted by Baldusarius on 7 Oct 2004 at 8:29 AM
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.


Report
Re: converting arraylist to array Posted by Chrissy on 8 Oct 2004 at 6:40 AM
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();
}
}
}
Report
Re: converting arraylist to array Posted by Baldusarius on 8 Oct 2004 at 9:11 AM
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:
pline = double.Parse((string) line);
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();
: }
: }
: }
:

Report
Re: converting arraylist to array Posted by ajikoe on 1 Nov 2004 at 1:24 AM
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();
:
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.