ArrayList 到文件...






2.56/5 (9投票s)
演示了一种非常简单的方法来创建和将多维(
引言
这是一个简单的演示,展示了如何通过示例应用程序将多维(虚拟)ArrayLists
保存到 text
文件。

关于 ArrayList
ArrayList
使用 array
实现 IList
接口,该 array
的大小根据需要动态增加,ArrayList
的容量是 ArrayList
可以容纳的元素数量。 当元素添加到 ArrayList
时,容量会根据需要通过重新分配自动增加;可以使用整数 index
访问 ArrayList
集合中的 Elements
。 此集合中的索引基于零; ArrayList
接受 null
引用(Visual Basic 中的 Nothing
)作为有效值,并允许重复元素。 不支持将多维数组用作 ArrayList
集合中的元素。
由于较新的 .NET 运行时在 System.Collections.Generic
中提供了更好的集合,因此在许多情况下不使用 ArrayList
。

ArrayList 的优点
- 自动调整大小
- 使用方便
ArrayList
具有添加、插入和删除一系列元素的方法- 使用
Synchronized
方法可以轻松创建线程安全的ArrayList
- 等等。
ArrayList 的缺点
ArrayList
仅限于下限为零的单维度,(但它可以用于存储objects
,我们可以使用此功能来使ArrayList
成为多维(虚拟))。ArrayList
仅限于累积Objects
- 性能方面非常慢
- 等等。
Using the Code
此页面不显示所有代码,而是显示基本结构。下载应用程序源代码以进行详细调查。
使 ArrayList 多维(虚拟)的基本原理
众所周知,ArrayList
可用于存储 objects
。 在这里,我们使用此功能来使 ArrayList
成为多维(虚拟),如下所示。
方法 1
'First declare a main ArrayList
Dim _MainList As New ArrayList
'Declare the inner ArrayList
Dim _InnerList As New ArrayList
'Adding data to the inner ArrayList
_InnerList.Add("MyData")
_InnerList.Add ("MoreData")
现在我们必须将内部 ArrayList
添加到我们的主要 ArrayList
,因此实际上我们有一个多维(虚拟)ArrayList
。
_MainList.Add(_InnerList)

方法 2
'First declare a main ArrayList
Dim _MainList As New ArrayList
'Declare the inner element as Object and add data to the inner element
Dim _InnerList As Object
现在我们必须将内部element
添加到我们的主要 ArrayList
,因此实际上我们有一个多维(虚拟)ArrayList
。
_MainList.Add(_InnerList)

方法 3
这次我们将使用structure
作为内部 element
。
Public Structure Contacts
Private m_FirstName As String
Private m_LastName As String
Private m_Phone As String
Public Property FirstName() As String
Get
Return m_FirstName
End Get
Set(ByVal value As String)
m_FirstName = value
End Set
End Property
Public Property LastName() As String
Get
Return m_LastName
End Get
Set(ByVal value As String)
m_LastName = value
End Set
End Property
Public Property Phone() As String
Get
Return m_Phone
End Get
Set(ByVal value As String)
m_Phone = value
End Set
End Property
End Structure
'Then declare a main ArrayList
Dim _MainList As New ArrayList
'Declare the inner element as an instance of Structure Contacts
Dim _InnerList As New Contacts
'Adding data to the inner element Contacts
_InnerList.FirstName = "Alex"
_InnerList.SecondName = "Tomy"
_InnerList.Phone = "123456789"
现在我们必须将内部 element
添加到我们的主要 ArrayList
,因此实际上我们有一个多维(虚拟)ArrayList
。
_MainList.Add(_InnerList)

Using the Code
这篇简短的文章以一个示例(通讯录)的形式呈现,其中包含将多维(虚拟)ArrayLists
保存到 text
文件的简单步骤。
示例应用程序包含以下文件

Contacts.vb (用于管理联系人详细信息的结构)
cLibrary.vb (用于使用 structure
Contacts
的instance
将ArrayList
{多维} 保存到文件的Class
)

步骤 1
首先创建 structure Contacts
(请参阅 Contacts.vb 以了解如何...!!)
第二步
创建一个Class
,使用structure Contacts
的instance
将ArrayList
{多维}保存到文件。
Friend Class cLibrary
'TO DO
End Class
创建一个名为 Library
As ArrayList
的属性来管理 structure Contacts
的多维 ArrayList
。
Public Property Library() As ArrayList
Get
Return m_Library
End Get
Set(ByVal value As ArrayList)
m_Library = value
End Set
End Property
如何将 ArrayList 保存到文本文件?
Public Sub WriteToFile(ByVal _FileName As String, ByVal _ArrayList As ArrayList)
Dim oWrite As System.IO.StreamWriter
oWrite = File.CreateText(_FileName)
For i As Integer = 0 To _ArrayList.Count - 1
oWrite.WriteLine(_ArrayList.Item(i))
'here Overridden Function ToString is used to convert the structure
'Contacts to String value before writing to the text file
Next
oWrite.Close()
End Sub
如何将文本文件读取到 ArrayList?
Public Function ReadFromFile(ByVal _FileName As String) As ArrayList
Dim oRead As System.IO.StreamReader
Dim _ArrayList As New ArrayList
Dim _Contacts As New Contacts
_ArrayList.Clear()
If (File.Exists(_FileName) = True) Then
oRead = File.OpenText(_FileName)
While oRead.Peek <> -1
_Contacts.FromString(oRead.ReadLine())
_ArrayList.Add(_Contacts)
End While
oRead.Close()
End If
Return _ArrayList
End Function
(请参阅 cLibrary.vb
以了解如何...!!)
步骤 3
Public Class FrmMain
Private _Lib As New cLibrary("ContactsLib.txt")
End Class
如何从 Form Class 将项目添加到 Library?
Dim _Contact As New Contacts
_Contact.FirstName = Trim(Me.TB_FirstName.Text)
_Contact.LastName = Trim(Me.TB_LastName.Text)
_Contact.Age = Val(Trim(Me.TB_Age.Text))
_Contact.PostBox = Trim(Me.TB_PostBox.Text)
_Contact.Street = Trim(Me.CB_Street.Text)
_Contact.City = Trim(Me.CB_City.Text)
_Contact.Phone = Trim(Me.TB_Phone.Text)
_Contact.Mobile = Trim(Me.TB_Mobile.Text)
_Contact.Email = Trim(Me.TB_Email.Text)
_Lib.Library.Add(_Contact)
_Lib.SaveLibrary()
如何从 Form Class 更新 Library 中的现有项目?
Dim _Index As Integer = _Lib.IndexOf(Me.CB_SelectContacts.Text)
Dim _Contact As New Contacts
_Contact.FirstName = Trim(Me.TB_FirstName.Text)
_Contact.LastName = Trim(Me.TB_LastName.Text)
_Contact.Age = Val(Trim(Me.TB_Age.Text))
_Contact.PostBox = Trim(Me.TB_PostBox.Text)
_Contact.Street = Trim(Me.CB_Street.Text)
_Contact.City = Trim(Me.CB_City.Text)
_Contact.Phone = Trim(Me.TB_Phone.Text)
_Contact.Mobile = Trim(Me.TB_Mobile.Text)
_Contact.Email = Trim(Me.TB_Email.Text)
_Lib.Library.Item(_Index) = _Contact
_Lib.SaveLibrary()
如何从 Form Class 删除 Library 中的现有项目?
If (_Lib.Remove(Me.CB_SelectContacts.Text) = True) Then
_Lib.SaveLibrary()
End If
如何从 Form Class 检索 Library 中现有项目的详细信息?
Dim _Contact As New Contacts
_Contact = _Lib.GetDetails(Me.CB_SelectContacts.Text)
With _Contact
Me.TB_FullName.Text = .Name
Me.TB_FirstName.Text = .FirstName
Me.TB_LastName.Text = .LastName
Me.TB_Age.Text = .Age
Me.TB_PostBox.Text = .PostBox
Me.CB_Street.Text = .Street
Me.CB_City.Text = .City
Me.TB_Phone.Text = .Phone
Me.TB_Mobile.Text = .Mobile
Me.TB_Email.Text = .Email
End With
End Sub
(请参阅 FrmMain.vb 以了解如何...!!)
结论
我的代码中可能存在错误;如果您发现任何错误,请告诉我,我会尽力修复它。 欢迎提出意见和建议。
希望这有帮助。
编程愉快!!!
历史
- 2010 年 8 月 18 日:初始发布