创建一个新字段,该字段将指向数组的下一个空位置。

huangapple go评论73阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月3日 23:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定