Login

Welcome, Guest. Please login or register.

September 09, 2025, 07:38:49 am

Author Topic: Most efficient way of reading a file into an array (VB.net)  (Read 3037 times)  Share 

0 Members and 1 Guest are viewing this topic.

Yendall

  • Victorian
  • Forum Leader
  • ****
  • Posts: 808
  • Respect: +38
0
What do you guys think is the most efficient way of reading a file into an array.
I know there are various different ways of doing so, such as stream-reader, FileOpen functions and new FileReader declarations. However, there must be one that is easiest to use and will get you top marks for code efficiency. Just wondering what you guys think.

Thanks.
2013 - 2016: Bachelor of Computer Science @ RMIT
2017 - 2018: Master of Data Science @ RMIT
ΟΟΟΟ
VCE '12: | English | I.T: Applications | I.T: Software Development | Music Performance Solo |  Further Mathematics | Studio Arts |

paulsterio

  • ATAR Notes Legend
  • *******
  • Posts: 4803
  • I <3 2SHAN
  • Respect: +430
Re: Most efficient way of reading a file into an array (VB.net)
« Reply #1 on: July 31, 2012, 06:08:03 pm »
0
Code: [Select]
Imports System
Imports System.IO

Dim Array() As String = File.ReadAllLines(C:\file.txt)

That's what I would do, of course replace C:\file.txt with whatever.

Yendall

  • Victorian
  • Forum Leader
  • ****
  • Posts: 808
  • Respect: +38
Re: Most efficient way of reading a file into an array (VB.net)
« Reply #2 on: July 31, 2012, 06:11:24 pm »
0
Code: [Select]
Imports System
Imports System.IO

Dim Array() As String = File.ReadAllLines(C:\file.txt)

That's what I would do, of course replace C:\file.txt with whatever.

I was thinking that as well, it seems the easiest way. However, are the boundaries of the array automatically set given how many lines are in the file?

say you coded:
Code: [Select]
Dim Array() As String = File.ReadAllLines(C:\file.txt)
and file.txt had 100 lines of integers.
Would the the array automatically become:
Code: [Select]
Array(100)
Or do you have to set the boundaries some other way? say, through global declaration?
2013 - 2016: Bachelor of Computer Science @ RMIT
2017 - 2018: Master of Data Science @ RMIT
ΟΟΟΟ
VCE '12: | English | I.T: Applications | I.T: Software Development | Music Performance Solo |  Further Mathematics | Studio Arts |

paulsterio

  • ATAR Notes Legend
  • *******
  • Posts: 4803
  • I <3 2SHAN
  • Respect: +430
Re: Most efficient way of reading a file into an array (VB.net)
« Reply #3 on: July 31, 2012, 06:16:19 pm »
0
No, you're saying

Code: [Select]
Dim Array() As String
which means it's a dynamic array, not a static (which is what Array(100) would be)

Yendall

  • Victorian
  • Forum Leader
  • ****
  • Posts: 808
  • Respect: +38
Re: Most efficient way of reading a file into an array (VB.net)
« Reply #4 on: July 31, 2012, 06:18:31 pm »
0
No, you're saying

Code: [Select]
Dim Array() As String
which means it's a dynamic array, not a static (which is what Array(100) would be)

Oh okay, sorry I was never told that there was a difference in array structures! So a dynamic array would be the best to use because you can essentially add to the array without any boundary errors?
2013 - 2016: Bachelor of Computer Science @ RMIT
2017 - 2018: Master of Data Science @ RMIT
ΟΟΟΟ
VCE '12: | English | I.T: Applications | I.T: Software Development | Music Performance Solo |  Further Mathematics | Studio Arts |

paulsterio

  • ATAR Notes Legend
  • *******
  • Posts: 4803
  • I <3 2SHAN
  • Respect: +430
Re: Most efficient way of reading a file into an array (VB.net)
« Reply #5 on: July 31, 2012, 06:26:10 pm »
0
Yes, pretty much a Dynamic array will continue to increase in size until it reaches it's logical limit (running out of RAM space).

http://en.wikipedia.org/wiki/Dynamic_array

Yendall

  • Victorian
  • Forum Leader
  • ****
  • Posts: 808
  • Respect: +38
Re: Most efficient way of reading a file into an array (VB.net)
« Reply #6 on: July 31, 2012, 06:27:42 pm »
0
Okay great, thanks for your help! My teacher seems to be infatuated by static arrays.
2013 - 2016: Bachelor of Computer Science @ RMIT
2017 - 2018: Master of Data Science @ RMIT
ΟΟΟΟ
VCE '12: | English | I.T: Applications | I.T: Software Development | Music Performance Solo |  Further Mathematics | Studio Arts |

paulsterio

  • ATAR Notes Legend
  • *******
  • Posts: 4803
  • I <3 2SHAN
  • Respect: +430
Re: Most efficient way of reading a file into an array (VB.net)
« Reply #7 on: July 31, 2012, 06:31:44 pm »
0
Okay great, thanks for your help! My teacher seems to be infatuated by static arrays.

Well static arrays were the hype back in the VB6 days, so I guess he would have his reasons :P

Also, for beginner programmers, static arrays are better because it encourages thinking ahead rather than defining a million dynamic arrays and a million variables for everything, which is what novice programmers do when they don't design their software properly.

Yendall

  • Victorian
  • Forum Leader
  • ****
  • Posts: 808
  • Respect: +38
Re: Most efficient way of reading a file into an array (VB.net)
« Reply #8 on: July 31, 2012, 06:47:41 pm »
0
Okay great, thanks for your help! My teacher seems to be infatuated by static arrays.

Well static arrays were the hype back in the VB6 days, so I guess he would have his reasons :P

Also, for beginner programmers, static arrays are better because it encourages thinking ahead rather than defining a million dynamic arrays and a million variables for everything, which is what novice programmers do when they don't design their software properly.
haha yes that's true.

Yeah I definitely consider dimensions when it comes to defining my arrays. I like to use static arrays so I know the lowest and highest values at all times, and work around that. Although, i'm not very experienced with arrays.
2013 - 2016: Bachelor of Computer Science @ RMIT
2017 - 2018: Master of Data Science @ RMIT
ΟΟΟΟ
VCE '12: | English | I.T: Applications | I.T: Software Development | Music Performance Solo |  Further Mathematics | Studio Arts |

Lasercookie

  • Honorary Moderator
  • ATAR Notes Legend
  • *******
  • Posts: 3167
  • Respect: +326
Re: Most efficient way of reading a file into an array (VB.net)
« Reply #9 on: July 31, 2012, 07:02:26 pm »
0
It'll come down to what's the appropriate tool for what you're trying to do though. If you had a task and you might find that a certain a data structure other than a standard array is a bit more appropriate e.g. multidimensional array (study design mandates 2-dimensional arrays), tuple (called 'records' on the study design), map/associative array (not on the study design, but pretty useful) etc. you might find that there's an easier or more elegant way for reading the data into that.
 
On static array vs dynamic arrays, again it depends on what you're doing. If you have know that you're going to have a fixed amount of data, then static array may be easier, that way you can deal with the overflow a bit easier (using exception handling for example).

As another example, let's say you're implementing a stack using arrays. If you want a fixed size stack then static array would be a good choice. If you wanted to have a resizable stack (or even determine the size of the stack during run-time), then dynamic arrays might be a better choice.

It also depends on the programming language you're using and what methods of dealing with things are available to you.

Yendall

  • Victorian
  • Forum Leader
  • ****
  • Posts: 808
  • Respect: +38
Re: Most efficient way of reading a file into an array (VB.net)
« Reply #10 on: July 31, 2012, 07:07:56 pm »
0
It'll come down to what's the appropriate tool for what you're trying to do though. If you had a task and you might find that a certain a data structure other than a standard array is a bit more appropriate e.g. multidimensional array (study design mandates 2-dimensional arrays), tuple (called 'records' on the study design), map/associative array (not on the study design, but pretty useful) etc. you might find that there's an easier or more elegant way for reading the data into that.
 
On static array vs dynamic arrays, again it depends on what you're doing. If you have know that you're going to have a fixed amount of data, then static array may be easier, that way you can deal with the overflow a bit easier (using exception handling for example).

As another example, let's say you're implementing a stack using arrays. If you want a fixed size stack then static array would be a good choice. If you wanted to have a resizable stack (or even determine the size of the stack during run-time), then dynamic arrays might be a better choice.

It also depends on the programming language you're using and what methods of dealing with things are available to you.
I think given the study design for SD, static arrays would be the best to use when handled fixed data. That's what my teacher says anyway. However, if I had to amend a text file and had to read it back into the array, the dimensions could possibly be altered. Which would utilise dynamic arrays, but I don't think that's essential for the SD study design anyway.

When I implement stacks, I use a static array in order to identify the top and bottom of the stack. If it's dynamic, it goes on forever until memory is exhausted. Dynamic arrays are useful for programs that deal with a lot of data, for SD, I don't think they are the most efficient. But than again, like you said, depends on what your program is trying to achieve.
2013 - 2016: Bachelor of Computer Science @ RMIT
2017 - 2018: Master of Data Science @ RMIT
ΟΟΟΟ
VCE '12: | English | I.T: Applications | I.T: Software Development | Music Performance Solo |  Further Mathematics | Studio Arts |