Question Arraylist problem

Dorte

New member
Joined
Jan 8, 2009
Messages
3
Programming Experience
Beginner
Hey
I am new to vb and programming in general and I have this problem I just cant fix, so if anyone could help that would be great!!

I am trying to pull a column of numbers out of a csv file, sort them, eliminate duplicates and put the rest in a new textfile. My problem is that the output file from the arraylist is wrong.
In stead of a list of numbers like this:
1
2
3
I get:
1
1
2
1
2
3
This is what I what I got:
VB.NET:
        Dim sr1 As New IO.StreamReader(Path1.Text)
        Dim Linecont1() As String
        Dim header1 As String
        Dim Linelist1 As String
        Dim delim As String = ";"

        Dim i As Integer = -1
        Dim ID3table As New ArrayList
        Dim result As New System.Text.StringBuilder
        Dim ID3 As String
        header1 = sr1.ReadLine
        Dim sw1 As New IO.StreamWriter(My.Computer.FileSystem.GetParentPath(Path1.Text) & "/ID3temp.text")
        Dim sw2 As New IO.StreamWriter(My.Computer.FileSystem.GetParentPath(Path1.Text) & "/ID3.text")
        Dim count As Integer = ID3table.Count
       

        Do While Not sr1.EndOfStream
            Linelist1 = sr1.ReadLine
            Linecont1 = Linelist1.Split(delim)
            ID3 = Linecont1(10)
            sw1.WriteLine(ID3)
            ID3table.Add(ID3)
        Loop

        sw1.Close()

        ID3table.Sort()

        For j = count - 1 To 1 Step -1
            If (ID3table(j).ToString() = ID3table(j - 1).ToString()) Then
                ID3table.RemoveAt(j)
            End If
       
        For Each ID3 In ID3table
             result.AppendLine(ID3)
            sw2.Write(result.ToString())
        Next ID3

I put the extra output to textfile (ID3temp) in because I thought it might help me figure out what the problem was but the file contains the unsorted list just as it should.
Any input to fix this is greatly appreciated :eek:
 
Since you're sorting the array list as well you can do it in a loop.

VB.NET:
		Dim myArrayList As New ArrayList()
		myArrayList.AddRange(New Integer() {1, 1, 2, 1, 2, 3})

		myArrayList.Sort()

		For i As Integer = myArrayList.Count - 1 To 1 Step -1
			If CInt(myArrayList(i)) = CInt(myArrayList(i - 1)) Then
				myArrayList.RemoveAt(i)
			End If
		Next

It would probably be better to check if the ArrayList contains the value before adding it and then just doing a sort at the end. Link: ArrayList.Contains Method (System.Collections)
 
Also, don't use ArrayLists in VB 2005 or later. Use the generic List class; in your case a List(Of String). Then you won't have to call ToString all the time because the items are returned as String references rather than Object references.
 
Ok thanks for the tips, but how do I avoid having the contents of the array written to the txt file every time a new item has been added? What I want is an output file containing the array contents when all values have been added. Instead every time I add a value the current array contents are listet along with the last added value in my txt file.
 
If you only want the data written once when it has all been added then that's exactly what you do: only write the data once when it's all been added. If you don't want the data written every time an item is added then just don't write the data every time an item is added.
 
But thats just it, I thought thats what I was doing.. Why is that!?!
I add an item each time Ive read a line in my csv file in the Do-while-not-loop and afterwards I thought I was taking each entry in my array, converting it to a string and wrote it to my text file but my output says otherwise.. Where does it all go wrong?
 
In this example I'm just using a text file with values:

VB.NET:
1
1
3
1
2
3

You'll need to use your own method to get the values to populate your list.

Process flow:
1. Get value
2. Check if value is in list
a. If yes - discard
b. If no - add to list
3. Repeat 1 & 2 for all values
4. Sort list
5. Write list to file

VB.NET:
Imports System.IO

Public Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles MyBase.Load

		Dim myList As New List(Of Integer)

		Using sr As New StreamReader("C:\Temp\Integers.txt")
			For Each line As String In sr.ReadToEnd.Split(Environment.NewLine)
				'Check if the list already contains the value
				If Not myList.Contains(CInt(line)) Then
					'If the list doesn't contain the value add it
					myList.Add(CInt(line))
				End If
			Next
		End Using

		'Sort your list
		myList.Sort()

		'Write the file now that you've got all of your values
		Using sw As New StreamWriter("C:\Temp\UniqueIntegers.txt")
			'myList.ToArray - Convert the list to an array of integer
			'Array.ConvertAll - Convert the array of integer to an array of string
			'String.Join - Join all of the array elements with a new line seperating them
			sw.Write(String.Join(Environment.NewLine, Array.ConvertAll(Of Integer, String)(myList.ToArray, AddressOf ConvertIntToString)))
		End Using


	End Sub

	Private Function ConvertIntToString(ByVal intParam As Integer) As String
		Return intParam.ToString()
	End Function

End Class
 
Back
Top