using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace list
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//将List<double> 转为 byte[]
static byte[] ConvertDoubleArrayToBytes(List<double> matrix)
{
if (matrix == null)
{
return new byte[0];
}
using (MemoryStream stream = new MemoryStream())
{
BinaryWriter bw = new BinaryWriter(stream);
foreach (var item in matrix)
{
bw.Write(item);
}
return stream.ToArray();
}
}
//将byte[] 转为 List<double>
static List<double> ConvertBytesToDoubleArray(byte[] matrix)
{
if (matrix == null)
return null;
List<double> result = new List<double>();
using (var br = new BinaryReader(new MemoryStream(matrix)))
{
var ptCount = matrix.Length / 8;
for (int i = 0; i < ptCount; i++)
{
result.Add(br.ReadDouble());
}
return result;
}
}
private void Form1_Load(object sender, EventArgs e)
{
List<double> data = new List<double> { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
byte[] bdata = ConvertDoubleArrayToBytes(data);
data = ConvertBytesToDoubleArray(bdata);
foreach (var item in data)
{
listBox1.Items.Add(item);
}
}
}
}
运行结果:
List<T>是泛型集合 这种集合规定了集合内的数据类型,只能存放<T>的T类型数据; 而ArrayList不是泛型,这种集合中可以存放任意类型数据; 举个简单例子:List<Student> students=new List<Student>(); 那么读取数据时就不用类型转化了,即:Student stu=students[0]; 增、删、改、查的方法:students.Add(T t);//增 students.Remove(int index);//删 stucents.Remove(T t);//删 students[]=//修改的数据 //查或者改 遍历集合类似于遍历数组 ArrayList students=new ArrayList(); Student stu=students[0] as Student;