英文:
Creation of new field that will point the next empty position of an array
问题
class Store
{
//Fields
string name;
Manager manager;
Employee[] employees = new Employee[5];
// 在Store
类中创建一个新字段,用于指向类型为Employee
的数组中下一个空位置。
// 我不知道如何创建这个字段。
}
英文:
A little bit background -
my assignment is to create new classes and to use them in each other.
there are 3 classes - Manager
,Employee
and Store
.<br/>
How do I create a new field (in class Store
) that will point the next null position on the array of type Employee
? I don't even know how to create this field.
class Store
{
//Fields
string name;
Manager manager;
Employee[] employees = new Employee[5]
}
答案1
得分: 1
你通过索引引用数组的内容。由于无论如何都需要跟踪数组中的项数,当前的元素数量始终指向数组的下一项:
class Store
{
//字段
string name;
Manager manager;
Employee[] employees = new Employee[5];
int employeeCount = 0;
}
当你向数组添加和删除项时,不要忘记维护你的项数。此外,当你从数组中间删除项时,需要将后续的项上移。
当你使用List
时,它会为你处理所有这些,但我猜这不是你的任务的重点。
英文:
You refer to the contents of the array by index. Since you need to keep track of the number of items in the array anyway, the current number of elements will always point to the next item of the array:
class Store
{
//Fields
string name;
Manager manager;
Employee[] employees = new Employee[5];
int employeeCount = 0;
}
You should not forget to maintain your item count when you add and remove items to your array. You should also be aware that you need to move the following items up when you remove items for the middle of the array.
When you use a List
, it will do all of this for you, but I guess this is not the point of your assignment.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论