dynamic multidimension arrays

I am trying to annalyze data from a csv file by reading the data into a dynamic 2-dimensional array. I figured out how to do this with C++ using vectors but I am running into trouble with C#. This is an example of the csv file.

00983,PR
00985,PR
00987,PR
009HH,PR

Here is a sample of the code I am working on

namespace fileoperation
{
using System;
using System.IO ;
using System.Collections;

class TestArrayList
{
public static void Main()
{
int x = 0;

//[blue] I want to make this a dynamic 2-d array[/blue]
string[,] myList = new string[2,1];

StreamReader sr = new
//int y = 0;
StreamReader(@G: est.csv ) ;
do
{
myList[0,0] = sr.ReadLine( ) ;
/*[blue] I want the entries comma deliminated so the data left of the comma corresponds to x and each line corresponds to y like a table*/[/blue]

Console.WriteLine( myList[0,0]) ;
x++;
} while ( x < 2 ) ;


sr.Close( ) ;


}
}
}


Comments

  • [b][red]This message was edited by martijn82 at 2004-1-7 1:32:19[/red][/b][hr]
    [b][red]This message was edited by martijn82 at 2004-1-5 5:8:4[/red][/b][hr]
    : I am trying to annalyze data from a csv file by reading the data into a dynamic 2-dimensional array. I figured out how to do this with C++ using vectors but I am running into trouble with C#. This is an example of the csv file.
    :
    : 00983,PR
    : 00985,PR
    : 00987,PR
    : 009HH,PR
    :
    : Here is a sample of the code I am working on
    :
    : namespace fileoperation
    : {
    : using System;
    : using System.IO ;
    : using System.Collections;
    :
    : class TestArrayList
    : {
    : public static void Main()
    : {
    : int x = 0;
    :
    : //[blue] I want to make this a dynamic 2-d array[/blue]
    : string[,] myList = new string[2,1];
    :
    : StreamReader sr = new
    : //int y = 0;
    : StreamReader(@G: est.csv ) ;
    : do
    : {
    : myList[0,0] = sr.ReadLine( ) ;
    : /*[blue] I want the entries comma deliminated so the data left of the comma corresponds to x and each line corresponds to y like a table*/[/blue]
    :
    : Console.WriteLine( myList[0,0]) ;
    : x++;
    : } while ( x < 2 ) ;
    :
    :
    : sr.Close( ) ;
    :
    :
    : }
    : }
    : }
    :
    :

    I would suggest to use an ArrayList for every line. For the elements on one line you could in this case use a conventional array of 2 elements. If the line can contain more values, use another ArrayList for every element. An ArrayList contains pointers and can not contain a standard string that is marshalled by value. So the String class is used to store the string values.
    For example:

    ArrayList rows = new ArrayList();
    ArrayList columns;

    columns = new ArrayList()
    columns.Add(new String("(0,0)").ToCharArray() );
    columns.Add(new String("(1,0)").ToCharArray());
    rows.Add(columns);

    columns = new ArrayList()
    columns.Add(new String("(0,1)").ToCharArray());
    columns.Add(new String("(1,1)").ToCharArray());
    rows.Add(columns);

    To get the 2nd element from the first line:
    ((String) ((ArrayList)rows[0])[1]).ToString();


Sign In or Register to comment.

Howdy, Stranger!

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

Categories

In this Discussion