Answered by:
arraylist of an arraylist?

Question
-
Hello
Can I create an ArrayList of ArrayList? Or perhaps an array of ArrayList?
Also, Chr() works in VB .Net. I wonder if there is another function (native to VB .net) that can do the same thing as Chr().
Thanks,
CMonday, April 13, 2009 6:51 PM
Answers
-
I meant, that if you can use 2.0, you shouldn't use ArrayList. List(of list(ofType))
- Proposed as answer by Arjun Paudel Tuesday, April 14, 2009 1:12 AM
- Marked as answer by Reed KimbleMVP, Moderator Wednesday, April 15, 2009 3:51 AM
Monday, April 13, 2009 8:10 PM -
Ok, then a Dictionary(Of String, String()) is probably most appropriate as each filename must be unique (so long as path is included anyway).
If all of the files were in the same folder, and all had a .txt extension, then you could use a simple routine such as the following to build your list of lists:
Private Function GetFileCache(ByVal pathToFolder As String) As Dictionary(Of String, String()) Dim result As New Dictionary(Of String, String()) Dim files() As String = System.IO.Directory.GetFiles(pathToFolder, "*.txt") For Each file As String In files result.Add(file, System.IO.File.ReadAllLines(file)) Next Return result End Function
This will build the dictionary such that each item is keyed with the filename and the value contains each line of text from the file. So with two listbox controls on a form, you could test the function with the following code:
Private mFileData As Dictionary(Of String, String()) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load mFileData = GetFileCache("c:\test") ListBox1.Items.AddRange(mFileData.Keys.ToArray) End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged ListBox2.Items.Clear() If ListBox1.SelectedIndex > -1 Then ListBox2.Items.AddRange(mFileData.Item(ListBox1.SelectedItem.ToString).ToArray) End If End Sub
Replace "c:\test" with a directory on your machine that contains some txt files. Run this code and you will see that when the form loads, ListBox1 is populated with the path and filename of each file. Selecting a file will populate ListBox2 with each of the lines of text in the file.
I think this should get you going in the right direction...
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"- Marked as answer by Carbond Tuesday, April 14, 2009 11:10 PM
Tuesday, April 14, 2009 9:56 PMModerator
All replies
-
You can create an arraylist and you can add any object to the array list. In your case another arraylist
Dim mainArrayList As ArrayList = New ArrayList()
Dim subArrayList As ArrayList = New ArrayList()
mainArrayList.Add(subArrayList)
Similarly you can have an array of arraylist
Dim arrayArrayList() As ArrayList
You can use Chr since its doing what you want from it :)
Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]Monday, April 13, 2009 7:03 PM -
Hello WiNd0wS-7:
Thanks.
If I do not know how many subArrayList there will be, is there any easy/elegant way to handle it? Preferably, no need to declare subArrayList1, subArrayList2 ... with an assumed maximum numbers of subArrayList.
CMonday, April 13, 2009 7:22 PM -
You can add as many sub array list to the main arraylist.
Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]Monday, April 13, 2009 7:23 PM -
The generic lists of .NET 2.0 provide improved perfomance.Monday, April 13, 2009 7:54 PM
-
John you are absolutely correct.
If you are using a List<ArrayList> then there is no need for an explicit cast.
Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]Monday, April 13, 2009 7:59 PM -
I meant, that if you can use 2.0, you shouldn't use ArrayList. List(of list(ofType))
- Proposed as answer by Arjun Paudel Tuesday, April 14, 2009 1:12 AM
- Marked as answer by Reed KimbleMVP, Moderator Wednesday, April 15, 2009 3:51 AM
Monday, April 13, 2009 8:10 PM -
Hi Carbond,
In the future, please post seperate questions in seperate threads.
Look at JohnWein's Post as it contains the best suggestion.
Yes, you can create and ArrayList which contains other ArrayLists. But using a generic collection of generic collections is better. And to go one step further, you may wish to create a custom class to encapsulate this list of lists - depending on the functionality that you need and the ultimate type of the objects stored, a custom class which inherits or contains the List(Of List(Of ObjectType)) may allow you to provide a more elegant solution.
As for Chr(), yes there are a number of other ways to achieve the same result, such as Char.Parse() and Convert.ToChar(). However, in VisualBasic the VB-specific commands such as Chr() are usually the most optimized (fastest) methods.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"Tuesday, April 14, 2009 2:54 PMModerator -
Hello Reed, John and WiNd0wS-7:
Thanks for all your suggestions and comments!
The particular purpose for the list of list or arraylist of arraylist is to store a list of files in the memory to save processing time. My program processes a bunch of text files. Each time when it runs, it processes either the same number of files as before or more. It uses the same set of files over and again in the same program run. Opening and closing files which are in a network folder appears to have taken quite a bit of time, and I like to speed it up.
Is list of list still the best solution in this case? Or there is yet a better way?
Thanks,
CTuesday, April 14, 2009 5:31 PM -
I would imagine you would use a List(of String). If you post your code, you'll get better responses.Tuesday, April 14, 2009 5:55 PM
-
The purpose of the inner list is still not clear... did you intend something like this:
[Parent List]
|
|----File Name 1
| [Child List]
| |
| |-----First line of text
| |-----Second line of text
| |-----...
| |-----Last line of text
|
|----File Name 2
| [Child List]
| |
| |-----First line of text
| |-----Second line of text
| |-----...
| |-----Last line of text
|
|---etc...
If that is not how you intended to use the list of lists, could you please elaborate on your intent?
As John stated, posting a relevant sample of your code (posting all your code is not advisable as few people will take the time to go through a TON of code) will help us help you. Often it is best to create a sample project that demonstrates (or recreates) your specific issue in as few lines of code as possible. Posting code which can be copy/pasted into VS to reproduce the issue is (typically) the quickest way to get answers from the community.
Now, I realize that you are trying to figure out which direction to go, so you may not have much in the way of code to post; however, it might do to create some psuedo code of what you envision needing.
At this point I'm thinking a dictionary of FileNames/FileData (eg "Dictionary(Of String, String())") might be the way to go...
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"Tuesday, April 14, 2009 7:21 PMModerator -
I'm enamored of the KeyValuePair(of TKey, TValue).Tuesday, April 14, 2009 7:53 PM
-
lol, yeah, its handy - I use it alot.
Dictionaries are good when the Key will be known to be unique.
List(Of KeyValuePair(Of TKey, TValue)) works pretty well when the key is known to NOT be unique.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"Tuesday, April 14, 2009 7:58 PMModerator -
Reed, it is exactly as you described in your diagram (if File Name1, File Name2 are the index of the Parent List, it is ok). When John replied, I did not have much code to post as I was not sure which approach to use, but the following is what I came up with using ArrayList. Shall I use Dictionary Of String or another approach? Thanks, C
Imports System.IO
Module Module1
'
Sub Main()
Dim ARYLST_DATA_FILE_CONTENTS As New ArrayList
Dim ARYLST_PATH_N_FILENAME As New ArrayList
Dim i As Integer
Dim j As Integer
Dim sr As StreamReader
Dim line As String = Nothing
j = 4
create_temp_files(j)
For i = 1 To j
ARYLST_PATH_N_FILENAME.Add("c:\temp\File" + i.ToString + ".txt")
Next
'Question: What to use to store the data into the memory?
For i = 0 To ARYLST_PATH_N_FILENAME.Count - 1
ARYLST_DATA_FILE_CONTENTS.Add(New ArrayList)
sr = New StreamReader(ARYLST_PATH_N_FILENAME.Item(i).ToString)
Do
line = sr.ReadLine
If line Is Nothing = False Then
ARYLST_DATA_FILE_CONTENTS.Item(i).add(line)
End If
Loop Until line Is Nothing
sr.Close()
Next
End Sub
Sub create_temp_files(ByVal j As Integer)
Dim sw As StreamWriter
Dim i As Integer
For i = 1 To j
sw = New StreamWriter("c:\temp\File" + i.ToString + ".txt", False)
sw.WriteLine("Data line 1")
sw.WriteLine("Data line 2")
sw.WriteLine("Data line ...")
sw.Close()
Next
End Sub
End ModuleTuesday, April 14, 2009 8:32 PM -
Ok, then a Dictionary(Of String, String()) is probably most appropriate as each filename must be unique (so long as path is included anyway).
If all of the files were in the same folder, and all had a .txt extension, then you could use a simple routine such as the following to build your list of lists:
Private Function GetFileCache(ByVal pathToFolder As String) As Dictionary(Of String, String()) Dim result As New Dictionary(Of String, String()) Dim files() As String = System.IO.Directory.GetFiles(pathToFolder, "*.txt") For Each file As String In files result.Add(file, System.IO.File.ReadAllLines(file)) Next Return result End Function
This will build the dictionary such that each item is keyed with the filename and the value contains each line of text from the file. So with two listbox controls on a form, you could test the function with the following code:
Private mFileData As Dictionary(Of String, String()) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load mFileData = GetFileCache("c:\test") ListBox1.Items.AddRange(mFileData.Keys.ToArray) End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged ListBox2.Items.Clear() If ListBox1.SelectedIndex > -1 Then ListBox2.Items.AddRange(mFileData.Item(ListBox1.SelectedItem.ToString).ToArray) End If End Sub
Replace "c:\test" with a directory on your machine that contains some txt files. Run this code and you will see that when the form loads, ListBox1 is populated with the path and filename of each file. Selecting a file will populate ListBox2 with each of the lines of text in the file.
I think this should get you going in the right direction...
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"- Marked as answer by Carbond Tuesday, April 14, 2009 11:10 PM
Tuesday, April 14, 2009 9:56 PMModerator -
PS - this example code is not optimized for huge text files (eg >50MB) so it might run real slow if you have giant text files in your folder... You could add a SuspendLayout calls to the ListBoxes to help some, but the fact is that this demo code was not meant to display large files.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"Tuesday, April 14, 2009 10:01 PMModerator -
Reed. Thanks a lot for your help. Our data files are at most a few MB.Tuesday, April 14, 2009 11:10 PM