Delphi 结构体数组使用
在开发中,能用结构体尽量不用类,因为结构体的存储机制是“栈”,这就意味着自动释放内存。但是在使用过程中也要注意以下几点:1.用完清空 2.用多少申请多少 下面用具体实例说明:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TScore=record
course:string;
score:real;
end;
TPerson=record
Name:string;
ScoreList:array of TScore;
end;
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure showPeople(p:TPerson);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
p:TPerson;
procedure TForm1.Button1Click(Sender: TObject);
begin
p.Name:='张三';
setlength(p.ScoreList,3);//有3门课程
p.ScoreList[0].course:='语文';
p.ScoreList[0].score:=98.5;
p.ScoreList[1].course:='数学';
p.ScoreList[1].score:=95;
p.ScoreList[2].course:='英语';
p.ScoreList[2].score:=87;
showPeople(p);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
p.Name:='李四';
setlength(p.ScoreList,3);//有3门课程
p.ScoreList[0].course:='语文';
p.ScoreList[0].score:=78.5;
p.ScoreList[1].course:='数学';
p.ScoreList[1].score:=70;
p.ScoreList[2].course:='英语';
p.ScoreList[2].score:=60;
showPeople(p);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
p.Name:='黄晓娜';
FillChar(p.ScoreList,sizeof(p.ScoreList),0); //这里清空缓存数据,生成 p.ScoreList=()
setlength(p.ScoreList,2);
//这种方法得到:p.ScoreList=((course='数学';score=78.5),(course='语文';score=70)) 采用截断模式
p.ScoreList[0].course:='语文';
p.ScoreList[0].score:=88.5;
p.ScoreList[1].course:='数学';
p.ScoreList[1].score:=90;
showPeople(p);
end;
procedure TForm1.showPeople(p: TPerson);
var
i:integer;
begin
memo1.Lines.Add(p.Name);
for I := Low(p.ScoreList) to High(p.ScoreList) do
begin
memo1.Lines.Add(format('%s:%.2f',[p.ScoreList[i].course,p.ScoreList[i].score]));
end;
end;
end.
说明:
综合上面的问题,如果采用setLength()方法来设置数组,容易因为缓存之前的数据未被更新而产生错误数据,最好使用fillChar()
方法进行清空后再申请。好比在使用FDStoreProcedure fdSp 实例对像时,使用前要 fdSp.Close; fdSp.Parameters.clear; 清空参数列表一样,这样更安全些。
Delphi 结构体数组使用相关推荐
- 【C++】【一】结构体数组
demo7:函数份文件编写 swap.h #include <iostream> using namespace std;//函数的声明 void swap(int a, int b); ...
- 【HDU】1305 Immediate Decodability(字典树:结构体数组,二维数组,链表/指针)
一.用的二维数组 #include <iostream> #include <cstring> #include <algorithm> using namespa ...
- 【HDU】1251统计难题 (字典树:二维数组,结构体数组,链表,map)
使用二维数组或者结构体数组都可以,但是在计数的时候有一点点小区别 一.结构体数组 #include <cstdio> #include <cstring> #include & ...
- 使用结构体数组统计男、女人数,计算全体学生的平均年龄、平均成绩,并将高于平均成绩的学生信息输出
<程序设计基础-c语言>杨莉 刘鸿翔 ISBN-978-7-03-032903-5 p165 习题6 3.输入10个学生的信息(包括学号.姓名.性别.年龄.成绩)组成结构体数组,分别统计男 ...
- matlab多维数组、结构体数组
1.多维数组 第三维称为页,需要注意的是每一页存放的二维数组维度要一致,也就是行列数要一致... 1 2 3 4 5 6 7 a=[1,2; 3,4]; b=[2,2; 5,6]; A ...
- c拆分字符串,并按照指定格式存入结构体数组
c将字符串拆分,并按照指定格式存入结构体数组 函数功能 代码实现 结果显示 函数功能 字符串格式:username1,password1;username2,password2; 结构体格式: typ ...
- 【C 语言】结构体 ( 结构体 数组 作为函数参数 | 数组 在 堆内存创建 )
文章目录 一.结构体 数组 作为函数参数 ( 数组 在 堆内存创建 ) 二.完整代码示例 一.结构体 数组 作为函数参数 ( 数组 在 堆内存创建 ) 在上一篇博客 [C 语言]结构体 ( 结构体 数 ...
- 【C 语言】文件操作 ( 读取文件中的结构体数组 | feof 函数使用注意事项 )
文章目录 一.读取文件中的结构体数组 | feof 函数使用注意事项 二.代码示例 一.读取文件中的结构体数组 | feof 函数使用注意事项 读取文件结构体时 , 可以循环读取文件中的数据 , 只使 ...
- golang结构体数组
转自: https://www.liaotaoo.cn/200.html package mainimport "fmt"type student struct{id intnam ...
最新文章
- java抽象类与抽象方法详解+练习题
- java中类/对象的初始化顺序以及静态代码块的使用
- BZOJ 4706: B君的多边形 找规律
- Google Hangouts支持使用Firefox WebRTC
- 前端对所有文件请求添加header_【前端面试必问】浏览器缓存原理?送你满分答案...
- redis RDB持久化中save和bgsave区别
- 苹果挂端口方法_调音台变身直播声卡的方法
- 如何把本地yum源给其他机器使用_配置本地yum源以及第3方软件仓库的搭建
- linux shell网卡,Shell脚本查看LINUX系统 网卡实时流量
- 本机web开发环境的搭建--nginx篇
- 如何在页面显示json数据
- 犀牛书学习笔记(3):函数
- esp32-cam拍照上传,微信小程序照片显示
- JAVA工作流的优雅实现方式
- PR预设:100种缩放旋转移动摇晃变形分割转场预设Transitions Pro for win/Mac​
- Linux常用英文单词
- 淘宝店铺排名还在用老方法吗,优化店铺排名方法是否正确?
- MNIST手写数据,从训练到数据预测(keras)
- 做鼻子测试软件,深度解析鼻子类型,测试你属于哪种鼻子?
- 鱼和熊掌兼得!这些应用是如何使用 Material Design 的?