如果需要查看更多文章,請微信搜索公眾號 csharp編程大全,需要進C#交流群群請加微信z438679770,備注進群, 我邀請你進群! ! !

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 = https://www.cnblogs.com/zyr365/archive/2020/11/24/new List { 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;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/227016.html
標籤:.NET技术
上一篇:Accord.NET入門
