|
-
May 12th, 2009, 01:36 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Array Questions
Heyy... So, I have this .txt file with 5 lines, let's say:
I
Have
An
Easy
Question
If I wanted to add those lines to an array list position, how would I do that?
-
May 12th, 2009, 01:42 AM
#2
Re: Array Questions
Instead of an array, use a List(of string) instead. It'll be much more flexible and user-friendly than an array will ever be (but you still have to learn arrays, which shows the basics of collecting things.)
All you really need to do is ADD them to the list, and viola, you're golden.
-
May 12th, 2009, 01:43 AM
#3
Thread Starter
Fanatic Member
Re: Array Questions
How could i add them? Using the streamreader.readline? or read to end?
-
May 12th, 2009, 01:45 AM
#4
Re: Array Questions
As long as the function returns a string, you can add it to the list (as long as the list is set for strings.)
Here's an example:
Code:
Dim s As New StreamReader
Dim L As New List(Of String)
L.Add(s.ReadLine)
-
May 12th, 2009, 02:06 AM
#5
Thread Starter
Fanatic Member
Re: Array Questions
I tried your code, but it only seems to add the first line ONLY...
If I chage it to: line.AddRange(reader.ReadToEnd), it adds all the lines in the first position of the list...
Last edited by tassa; May 12th, 2009 at 02:23 AM.
-
May 12th, 2009, 02:40 AM
#6
Thread Starter
Fanatic Member
Re: Array Questions
I fixed the code... This is it:
vb Code:
Using reader As New IO.StreamReader("C:\Array.txt") line.AddRange(Split(reader.ReadToEnd, vbNewLine)) End Using
Any recommendations or anything.. Please do
-
May 12th, 2009, 02:49 AM
#7
Re: Array Questions
Try this:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim fileContents As String = File.ReadAllText("C:\temp\test.txt") Dim splitter() As Char = {Chr(13)} Dim myArray() As String = fileContents.Split(splitter, StringSplitOptions.RemoveEmptyEntries) 'we have everything in the myArray array now 'lets fill it in a listbox for example ListBox1.DataSource = myArray End Sub
Pradeep
-
May 12th, 2009, 02:52 AM
#8
Re: Array Questions
vb.net Code:
Dim lines As String() = IO.File.ReadAllLines("file path here")
-
May 12th, 2009, 02:53 AM
#9
Re: Array Questions
Hi Pradeep..
Wont this work ?
vb Code:
Dim myArray() As String = System.IO.File.ReadAllLines("C:\temp\test.txt") 'we have everything in the myArray array now 'lets fill it in a listbox for example ListBox1.DataSource = myArray
Please mark you thread resolved using the Thread Tools as shown
-
May 12th, 2009, 03:02 AM
#10
Thread Starter
Fanatic Member
Re: Array Questions
 Originally Posted by jmcilhinney
vb.net Code:
Dim lines As String() = IO.File.ReadAllLines("file path here")
Where should I put that line of code? What's the difference between an Array and a List(Of T)? Doesn't Padreep's code and mine do the same?
-
May 12th, 2009, 03:03 AM
#11
Re: Array Questions
That's much better and easier.
-
May 12th, 2009, 03:20 AM
#12
Thread Starter
Fanatic Member
-
May 12th, 2009, 03:30 AM
#13
Re: Array Questions
vb Code:
Dim myArray() As String = System.IO.File.ReadAllLines("C:\temp\test.txt")
This. It is simple one line code that would get all the lines in the file in an array.
My code in post #7 was reading the file, then splitting it into an array. The only additional thing it was doing is removing any blank lines, if any. So that depends on which one you want to use.
-
May 12th, 2009, 11:46 AM
#14
Thread Starter
Fanatic Member
Re: Array Questions
Oh, I thought that what jmcilhinney wrote was a correction to your code. But the question remains... What's the basic difference between a list and an array?
-
May 12th, 2009, 01:11 PM
#15
Re: Array Questions
An array is a basic structure (usually found in all languages), which provides a mechanism to address a (consecutive) sequence of memory locations by its index value. All the elements in an array are of same data type i.e. they occupy same memory size. Usually accessing an element is very fast since the memory blocks are of equal size. So we just multiply the index value with memory size and we get the address of the element. However, adding/inserting/removing elements is a cumbersome operation.
A List is a specialized .NET class that can be used to store elements more or less like arrays. But it has many advanced features. You can access elements by their index values just like arrays. And adding/inserting/deleting elements is just a matter of a simple function call to the user, besides many more advanced features.
-
May 12th, 2009, 07:07 PM
#16
Re: Array Questions
Think of an array as an egg carton. When you make an egg carton it is a specific size, i.e. it can hold a specific number of eggs, and that size doesn't change. Initially each cup in the egg carton is empty. You can put an egg in each cup and you can remove eggs from those cups but the carton never changes size.
A List is like a magic egg carton. When you create it it exists but it has no cups. When you add an egg to magic carton it grows automatically so that it has just one cup for that one egg. If you add another egg the magic carton grows again to have two cups. You can then insert another egg in between those two and the carton grows again. You can then remove one of the eggs and the carton shrinks automatically so it's just got two cups for the two remaining eggs. If clear out all the remaining eggs the magic carton will shrink down again so it has no cups at all.
Arrays are very efficient so they should be used where you don't need this dynamic resizing behaviour. Lists are more convenient than arrays when dynamic resizing is required and, if a lot of resizing is required, then a List becomes more efficient than an array. That's because to "resize" an array you actually have to create a new array and copy all the elements from the original.
Here's something that not everyone knows: internally, a List actually stores its items in an array. The List provides fancy logic to hide that fact from the user but it's not really magic. Each time you add an item to a List it first checks whether the array is large enough to hold it. If it's not it will resize the array as mentioned before. The thing is, the array doesn't get resized every time you add an item. When the array is resized its size is doubled. That keeps the number of copying operations to a balanced level and, the bigger the array gets, the less often it gets resized.
-
May 12th, 2009, 10:10 PM
#17
Thread Starter
Fanatic Member
Re: Array Questions
Oh, thanks a lot for that explanation. I liked the (magic) egg carton example.
But, just one last question:
Where would I use the dynamic resizing option(?) of the Lists?
-
May 12th, 2009, 10:24 PM
#18
Re: Array Questions
 Originally Posted by tassa
Where would I use the dynamic resizing option(?) of the Lists?
Run this code:
Code:
Dim eggCarton As New List(Of String)
MessageBox.Show(String.Join(ControlChars.NewLine, _
eggCarton.ToArray()), _
"Count = " & eggCarton.Count)
eggCarton.Add("Egg 1")
MessageBox.Show(String.Join(ControlChars.NewLine, _
eggCarton.ToArray()), _
"Count = " & eggCarton.Count)
eggCarton.Add("Egg 2")
MessageBox.Show(String.Join(ControlChars.NewLine, _
eggCarton.ToArray()), _
"Count = " & eggCarton.Count)
eggCarton.Insert(1, "Egg 3")
MessageBox.Show(String.Join(ControlChars.NewLine, _
eggCarton.ToArray()), _
"Count = " & eggCarton.Count)
eggCarton.RemoveAt(0)
MessageBox.Show(String.Join(ControlChars.NewLine, _
eggCarton.ToArray()), _
"Count = " & eggCarton.Count)
eggCarton.Clear()
MessageBox.Show(String.Join(ControlChars.NewLine, _
eggCarton.ToArray()), _
"Count = " & eggCarton.Count)
-
Jun 4th, 2009, 06:11 PM
#19
Thread Starter
Fanatic Member
Re: Array Questions
 Originally Posted by Pradeep1210
Try this:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileContents As String = File.ReadAllText("C:\temp\test.txt")
Dim splitter() As Char = {Chr(13)}
Dim myArray() As String = fileContents.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
'we have everything in the myArray array now
'lets fill it in a listbox for example
ListBox1.DataSource = myArray
End Sub
Pradeep 
Hey Pradeep, I was looking into your code (again), and I didn't know why is that you used:
vb.net Code:
Dim splitter() As Char = {Chr(13)}
What does the "13" stand for exactly?
-
Jun 4th, 2009, 06:25 PM
#20
Re: Array Questions
Chr(13) I assume is for the enter button.
-
Jun 4th, 2009, 06:37 PM
#21
Re: Array Questions
Splitting on Chr(13), which is a carriage return, is not such a good idea because, by default, a Windows text file should use a carriage return and line feed pair as a line break. If you split on just the carriage return then your substrings will all have leading line feeds.
-
Jun 4th, 2009, 07:43 PM
#22
Thread Starter
Fanatic Member
-
Jun 4th, 2009, 07:48 PM
#23
Re: Array Questions
This:
would become this:
Understand now?
EDIT: wait....that's not right..thats for replacing..oops.
EDIT2: If I understand right, wouldn't it just add an extra carriage return between each line?
-
Jun 4th, 2009, 08:05 PM
#24
Re: Array Questions
 Originally Posted by tassa
In other words....?
If your file uses carriage returns only as line breaks then you should split on ControlChars.Cr. If your file uses carriage return and line feed pairs as line breaks then you should split on ControlChars.NewLine or Environment.NewLine. I've never tried it but you may be able to handle both with the one line of code by using a String array as delimiters and specifying both, with NewLine first so that it gets preference. It might be worth testing that to see if it works, if you need it.
-
Jun 4th, 2009, 09:07 PM
#25
Thread Starter
Fanatic Member
Re: Array Questions
What exactly is a carriage? What's it function?
-
Jun 4th, 2009, 09:11 PM
#26
Re: Array Questions
 Originally Posted by tassa
What exactly is a carriage? What's it function?
http://upload.wikimedia.org/wikipedi...a/a5/Enter.png
-
Jun 4th, 2009, 09:35 PM
#27
Re: Array Questions
 Originally Posted by tassa
What exactly is a carriage? What's it function?
The names hearken back to the days of typewriters. The carriage was the apparatus that held the paper and it would physically move to the left as you typed so that the characters would appear left-to-right. At the end of a line you would want the carriage to return to its original position. The line feed occurred by turning the roller that held the paper within the carriage so that the paper was feed up the height of one line. Generally speaking, when you finished typing a line you would hit the handle on the left hand side and it would return the carriage to its original position and feed the paper one line, hence the names carriage return and line feed.
In Windows a line break in a text file is represent by the ASCII characters for a carriage return (13) and a line feed (10). In C# and other C-based languages these are represented with "\r" and "\n". In VB they can be represent individually with ControlChars.Cr and ControlChars.Lf or they can be represented together with ControlChars.CrLf or ControlChars.NewLine. Environment.NewLine will return a string containing a carriage return and a line feed on Windows systems, but on other systems it will return only a line feed because non-Windows systems use only a line feed to represent a line break.
Note that, these days, Windows will interpret a line feed on its own as a line break. This is one reason that it's bad to split on just carriage returns. In that case it will leave behind line feeds and those will become line breaks preceding each substring.
Last edited by jmcilhinney; Jun 4th, 2009 at 09:41 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|