Click here to Skip to main content
15,909,205 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: C# for Mac and Linux ? Pin
IGx8923-Mar-04 15:59
IGx8923-Mar-04 15:59 
GeneralMonitoring System temperature Pin
Ian Powell10-Mar-04 7:19
Ian Powell10-Mar-04 7:19 
GeneralRe: Monitoring System temperature Pin
ian mariano11-Mar-04 18:33
ian mariano11-Mar-04 18:33 
GeneralRe: Monitoring System temperature Pin
eggie515-May-04 10:37
eggie515-May-04 10:37 
GeneralRe: Monitoring System temperature Pin
ian mariano11-Mar-04 18:37
ian mariano11-Mar-04 18:37 
GeneralRe: Monitoring System temperature Pin
ian mariano11-Mar-04 18:42
ian mariano11-Mar-04 18:42 
Questionhow can i save 2D array of double to a file? Pin
Marco M.10-Mar-04 4:06
Marco M.10-Mar-04 4:06 
AnswerRe: how can i save 2D array of double to a file? Pin
ian mariano11-Mar-04 19:32
ian mariano11-Mar-04 19:32 
Suppose wr is your BinaryWriter, your multidimensional (rectangular) array, theArray is 2D. Store and write the dimensions out thus (C#):
int mupper = theArray.GetUpperBound(0);
int nupper = theArray.GetUpperBound(1);
 
ws.Write(mupper);
ws.Write(nupper);

Then write out the bytes:
for (int i = 0; i < mupper; i++)
{
   for (int j = 0; j < nupper; i++)
      wr.Write(theArray[i, j]);
}

To read it back in, you do the reverse:
//  rdr is a BinaryReader
int mupper = rdr.ReadInt32();
int nupper = rdr.ReadInt32();
byte[,] theArray = new byte[mupper, nupper];
 
for (int i = 0; i < mupper; i++)
{
   for (int j = 0; j < nupper; i++)
      theArray[i, j] = rdr.ReadByte();
}

The problem is that this is not that efficient. If, however, you used a jagged array (actually, the secondary array never differs in length so for all purposes it's still rectangular not jagged,) you can define theArray 2d, m,n like this:
byte[][]  theArray = new byte[m][];
 
for (int i = 0; i < theArray.Length; i++)
   theArray[i] = new byte[n];

You access the byte at x, y thus: theArray[x][y]

Saving is a lot more efficient using a jagged array:
//  wr is our BinaryWriter
wr.Write(theArray.Length);  //  m
wr.Write(theArray[0].Length);  //  n
  
for (int i = 0; i < theArray.Length; i++)
   wr.Write(theArray[i]);

Reading back in is also more efficient:
//  rdr is a BinaryReader
int m = rdr.ReadInt32();
int n = rdr.ReadInt32();
byte[][] theArray = new byte[m][];
 
for (int i = 0; i < theArray.Length; i++)
   theArray[i] = rdr.ReadBytes(n);


The jagged array code will also save non-rectangular arrays without modification.

Ian Mariano - http://www.ian-space.com/


"We are all wave equations in the information matrix of the universe" - me

GeneralRe: how can i save 2D array of double to a file? Pin
Marco M.13-Mar-04 1:02
Marco M.13-Mar-04 1:02 
GeneralRe: how can i save 2D array of double to a file? Pin
ian mariano15-Mar-04 12:08
ian mariano15-Mar-04 12:08 
AnswerRe: how can i save 2D array of double to a file? Pin
Roman Rodov16-Mar-04 17:14
Roman Rodov16-Mar-04 17:14 
GeneralExchange server 5.5 Pin
pushpi10-Mar-04 1:02
pushpi10-Mar-04 1:02 
GeneralSave word Pin
Sreepathi8-Mar-04 11:53
Sreepathi8-Mar-04 11:53 
GeneralRe: Save word Pin
Peter Greenall8-Mar-04 12:04
Peter Greenall8-Mar-04 12:04 
GeneralRe: Save word Pin
Sreepathi8-Mar-04 12:52
Sreepathi8-Mar-04 12:52 
GeneralRe: Save word Pin
Philip Fitzsimons9-Mar-04 6:21
Philip Fitzsimons9-Mar-04 6:21 
QuestionOracle Notification Services?? Pin
Chen Venkataraman8-Mar-04 9:42
Chen Venkataraman8-Mar-04 9:42 
General.net 1.1 bug? XmlDocument.CreateProcessingInstruction Pin
Anonymous8-Mar-04 9:14
Anonymous8-Mar-04 9:14 
GeneralRe: .net 1.1 bug? XmlDocument.CreateProcessingInstruction Pin
Philip Fitzsimons9-Mar-04 6:24
Philip Fitzsimons9-Mar-04 6:24 
Question.NET platform independant? Pin
Peter Greenall8-Mar-04 8:23
Peter Greenall8-Mar-04 8:23 
AnswerRe: .NET platform independant? Pin
John Kuhn8-Mar-04 8:51
John Kuhn8-Mar-04 8:51 
GeneralRe: .NET platform independant? Pin
Michael Flanakin17-Mar-04 2:17
Michael Flanakin17-Mar-04 2:17 
GeneralRe: .NET platform independant? Pin
John Kuhn17-Mar-04 5:56
John Kuhn17-Mar-04 5:56 
GeneralRe: .NET platform independant? Pin
Michael Flanakin19-Mar-04 1:46
Michael Flanakin19-Mar-04 1:46 
GeneralRe: .NET platform independant? Pin
John Kuhn22-Mar-04 15:36
John Kuhn22-Mar-04 15:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.