如何在不知道指定数量的情况下创建对象数组(How to create an array of objects without knowing specified amount ahead of tim

系统教程 行业动态 更新时间:2024-06-14 16:57:17
如何在不知道指定数量的情况下创建对象数组(How to create an array of objects without knowing specified amount ahead of time)

我在入门级编程课程。 我们最近一直在讨论对象,并被要求创建一个程序,获取一些用户输入,然后使用我们提供的属性创建动物对象。 我们只需要制作2个对象,但我想自己创建并创建一个程序,询问:

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

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

以下是我目前得到的2个错误:

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

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

这是我的主要代码:

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++; } } } }

这是我的Animal类:

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."); } } }

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

I am in an entry level programming class. We have been going over objects lately and were asked to make a program that gets a bit of user input and then creates objects of animals with the attributes we give them. We are only required to make 2 objects but I wanted to spin off on my own and create a program that asks:

For a bit of input, then it puts that info into an undeclared array of objects called animal of class Animal. Then it asks if you'd like to make another animal and if so it repeats the input and putting it into the next element in the array.

I'm having trouble getting it to run, I'm pretty sure I am not initializing the array correctly, I've looked all over stack overflow but I can't find anything that lets me create an array of objects of an unspecified size. I want to create a new object with its constructor values into an element of an array of unspecified size.

Here are the 2 errors I am currently getting:

Error CS0650 Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type

Error CS0270 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

Here is my main code:

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++; } } } }

and here is my Animal class:

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."); } } }

I am looking forward to learning how to create arrays of objects of an undetermined size.

最满意答案

您可以使用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(); }

You can use a List<T>.

List<T> implements IList<T> by using an array whose size is dynamically increased as required.

// 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(); }

更多推荐

本文发布于:2023-04-12 20:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/89916c4e7d4284478498c167a736d923.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   情况下   对象   数量   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!