473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

arraylist to array

I would simply like to convert the arraylist i read in to an array of type
double. The code below gives me an error that at least one element in the
source array could not be cast down to the destination array. What do I need
to change?

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[] AField = (double[])aColumn.ToArray (typeof (double));
Console.WriteLine (AField);
}
Console.ReadLine();
}}}
Nov 16 '05 #1
8 4335
Christine <Ch*******@discussions.microsoft.com> wrote:
I would simply like to convert the arraylist i read in to an array of type
double. The code below gives me an error that at least one element in the
source array could not be cast down to the destination array. What do I need
to change?


You need to add doubles to the ArrayList instead of strings. Parse the
doubles as you go, and add the parsed double to the ArrayList.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
What do I need to change?


You're currently putting strings into the ArrayList. So you can either
use ToArray() to retrieve a string[], or change the code to convert
the string to a double before putting it into the ArrayList (but keep
in mind that that would cause boxing). Personally I'd probably do
somehting like this

double[] AField = new double[aColumn.Count];
for ( int i = 0; i < AField.Length; i++ )
AField[i] = Double.Parse((string)aColumn[i]);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3
I thought that was working in my code, but apparently not. I got an error
message that says Input string was not in a correct format.

"Mattias Sjögren" wrote:
What do I need to change?


You're currently putting strings into the ArrayList. So you can either
use ToArray() to retrieve a string[], or change the code to convert
the string to a double before putting it into the ArrayList (but keep
in mind that that would cause boxing). Personally I'd probably do
somehting like this

double[] AField = new double[aColumn.Count];
for ( int i = 0; i < AField.Length; i++ )
AField[i] = Double.Parse((string)aColumn[i]);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #4
Christine <Ch*******@discussions.microsoft.com> wrote:
I thought that was working in my code, but apparently not. I got an error
message that says Input string was not in a correct format.


And what was the input string?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
The input string is just a text file of numbers.

"Jon Skeet [C# MVP]" wrote:
Christine <Ch*******@discussions.microsoft.com> wrote:
I thought that was working in my code, but apparently not. I got an error
message that says Input string was not in a correct format.


And what was the input string?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
I'm sorry, that's not entirely correct. The input at that point of the code
is an arraylist. The arraylist came from a text file of numbers.

"Christine" wrote:
The input string is just a text file of numbers.

"Jon Skeet [C# MVP]" wrote:
Christine <Ch*******@discussions.microsoft.com> wrote:
I thought that was working in my code, but apparently not. I got an error
message that says Input string was not in a correct format.


And what was the input string?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7
Christine <Ch*******@discussions.microsoft.com> wrote:
The input string is just a text file of numbers.


Well it clearly failed with one of those input strings.

I suggest you catch the exception and print out what string it was that
caused it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Christine <Ch*******@discussions.microsoft.com> wrote:
I'm sorry, that's not entirely correct. The input at that point of the code
is an arraylist. The arraylist came from a text file of numbers.


But you're parsing each string in turn, according to your previously
posted code. One of those strings isn't being parsed. You need to find
out which, why it's there, and why it's not being parsed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jamus Sprinson | last post by:
Before I continue, I'm going to begin by saying I'm not by any means an expert- I've been using .NET with C# for about 4 months now, and basically just learning by example and docs. A game...
7
by: William Stacey [MVP] | last post by:
I can think of a couple ways to do this, but was wonder what the fastest way you can think of. Want to take any arraylist of > 1 element and make the first element the last, and all the rest of...
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
6
by: rdi | last post by:
I have 3 classes: mailBox, mailFilter & mailFilterSet I then have an arraylist. Each element in the array is of type mailBox (variable name is mbox). One item of the mailbox class is an...
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
5
by: Paul | last post by:
Off the cuff, does anyone know if arraylist is more efficeint at adding items to an array than redim preserve? Paul <begin loop> Dim c As Integer = SomeArray.GetUpperBound(0) + 1 ReDim...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.