如何创建100个新对象的数组? [英] How to create array of 100 new objects?

查看:94
本文介绍了如何创建100个新对象的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试类似的事情:

I'm trying something like that:

class point
{
public int x;
public int y;
}

point[] array = new point[100];
array[0].x = 5;

这是错误:
对象引用未设置为的实例目的。 (@最后一行)

出什么问题了? :P

whats wrong? :P

推荐答案

它仅创建数组,但所有元素均初始化为null。

您需要循环或类似的方法来创建类的实例。
(在这种情况下,foreach循环不起作用)
示例:

It only creates the array, but all elements are initialized with null.
You need a loop or something similar to create instances of your class. (foreach loops dont work in this case) Example:

point[] array = new point[100];
for(int i = 0; i < 100; ++i)
{
    array[i] = new point();
}

array[0].x = 5;

这篇关于如何创建100个新对象的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆