【问题标题】:How to create an array of objects without knowing specified amount ahead of time如何在不提前知道指定数量的情况下创建对象数组
【发布时间】:2018-05-05 11:25:00
【问题描述】:

我正在上入门级编程课程。我们最近一直在研究对象,并被要求制作一个程序,该程序可以获取一些用户输入,然后创建具有我们赋予它们的属性的动物对象。我们只需要制作 2 个对象,但我想自己分拆并创建一个程序,要求:

对于一些输入,然后它将该信息放入一个未声明的对象数组中,称为 Animal 类的 animal。然后它会询问您是否要制作另一种动物,如果是,它会重复输入并将其放入数组中的下一个元素中。

我无法让它运行,我很确定我没有正确初始化数组,我查看了整个堆栈溢出但我找不到任何可以让我创建对象数组的东西未指定的大小。我想创建一个新对象,并将其构造函数值放入未指定大小的数组的元素中。

这是我目前遇到的 2 个错误:

错误 CS0650 错误的数组声明器:要声明托管数组的秩 说明符在变量的标识符之前。声明固定大小 缓冲区字段,在字段类型前使用固定关键字

错误 CS0270 无法在变量声明中指定数组大小 (尝试使用“新”表达式进行初始化)

这是我的主要代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayOfObjectsAnimalMaker
{
    class Program
    {
        static void Main(string[] args)
        {
            string another = "y";
            int count = 0;
            string species;
            string age;

            while (another == "y")
            {
                Console.WriteLine("Type in the animal's species: ");
                species = Console.ReadLine();

                Console.WriteLine("Type in the animal's age: ");
                age = Console.ReadLine();

                Animal animal[count] = new Animal(species, age);

                Console.WriteLine("Type y to create another animal or n to end: ");
                another = Console.ReadLine();

                count++;
            }
        }
    }
}

这是我的动物课:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayOfObjectsAnimalMaker
{
    class Animal
    {
        private string species;
        private string age;

        public Animal(string s, string a)
        {
            this.species = s;
            this.age = a;
        }

        public void DisplayInfo()
        {
            Console.WriteLine("This is a " + this.species + " that is " + this.age + " years old.");
        }
    }
}

我期待学习如何创建大小未定的对象数组。

【问题讨论】:

  • 这有点超出你现在的位置。然而,作为即将发生的事情的一个小预览:当您处理未知大小时,数组不是您想要使用的类型。解决方案是实现一个集合,然后在每次您想添加另一个动物时添加到该集合中。
  • 您可以使用List<Animal> 而不是Animal[]。然后你可以拨打thatList.Add(new Animal( .. ));
  • 使用List<Animal> 而不是Animal[]
  • 您还想在循环上方声明/创建该列表,而不是在循环内部。

标签: c# arrays oop object


【解决方案1】:

您可以使用List<T>

List<T> 通过使用一个数组来实现IList<T>,该数组的大小根据需要动态增加。

// create a list to hold animals in 
var allAnimals = new List<Animal>();    

while (another == "y")
{
    Console.WriteLine("Type in the animal's species: ");
    species = Console.ReadLine();

    Console.WriteLine("Type in the animal's age: ");
    age = Console.ReadLine();

    // create the animal..
    var newAnimal = new Animal(species, age);
    // ..and add it in the list.
    allAnimals.Add(newAnimal);

    Console.WriteLine("Type y to create another animal or n to end: ");       
    another = Console.ReadLine();
}

【讨论】:

  • 感谢我不知道列表的帮助!是否可以像数组一样使用它们?
  • @JGoss 是的,他们可以——至于其中一个的用例,请查看this answer
【解决方案2】:

一旦声明,数组的大小是固定的。 拥有动态集合大小的最简单方法是使用 List 类。 如果您仅限于使用数组,则可以从固定大小的数组(例如 1)开始。然后,当您需要更多空间时,您将分配一个两倍于原始数组的新数组,将原始数组复制到新数组中,然后添加新数据。 您可以根据需要重复此过程。

【讨论】:

  • 这实际上是List 为您所做的。
  • @DanielS Lists 以默认容量开始,当需要扩展时会不断加倍。
  • 我想我们都同意 List 更适合这项任务。但是,由于问题是关于数组的,所以我展示了它是如何完成的。是的,List 也是这样做的。
  • 我不知道列表是否更好用,我肯定会使用它们。
猜你喜欢
  • 1970-01-01
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 2015-11-14
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
  • 2015-10-12
相关资源
最近更新 更多