Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 对象的动态数组_Arrays_Object_Dynamic - Fatal编程技术网

Arrays 对象的动态数组

Arrays 对象的动态数组,arrays,object,dynamic,Arrays,Object,Dynamic,也许解决办法很简单。一定是的,但也许我忽略了什么 我有: public class Object { public int pos_x; public int pos_y; } Object testObject[] = new object[10] 然后在函数中的某个地方 testObject[1].pos_x = 1; 它会强制关闭我的应用。。怎样?为什么?这可能是什么原因造成的 而且。理想情况下,我需要这样的东西 testObject[].add_new_object

也许解决办法很简单。一定是的,但也许我忽略了什么

我有:

public class Object {
    public int pos_x;
    public int pos_y;
}

Object testObject[] = new object[10]
然后在函数中的某个地方

testObject[1].pos_x = 1;
它会强制关闭我的应用。。怎样?为什么?这可能是什么原因造成的

而且。理想情况下,我需要这样的东西

testObject[].add_new_object();
testobject[].remove_item(3);
这能做到吗


感谢您的帮助

您已分配了一个可容纳10个对象的阵列


您还需要分配对象。

我不确定您正在使用的语言-如果C#您不能使用“Object”作为类名

首先创建自定义对象(“对象”数据类型):

…很公平,这是一个非常基本的类,包含坐标。接下来,要创建MyObject的数组。为此,将数组类型声明为
MyObject[]
,并提供可选大小:

MyObject[] myObjArray = new MyObject[10]; // this gives a zero-based array of 10 elements, from 0-9
现在,您有了填充数组的任务。最常用的方法是使用计数范围为0到9的计数器变量,与数组中的元素相同:

for (int i=0; i<=9; i++)
{
    myObjArray[i] = new MyObject();

    // you can also assign the variables' values here
    myObjArray[i].pos_x = GetNextXVal(); // get the X value from somewhere
    myObjArray[i].pos_y = GetNextYVal(); // get the y value from somewhere
}  

for(int i=0;iIs this Javascript?Platform?…和,数组是任何集合中动态性最低的一个…事实上,a会说数组从来都不是“动态的”。请指定您的语言。要解释Steve的答案,在Object testObject[]=new Object[10]之后,您必须(对于从0到9的每个i):testObject[i] =新对象();
for (int i=0; i<=9; i++)
{
    myObjArray[i] = new MyObject();

    // you can also assign the variables' values here
    myObjArray[i].pos_x = GetNextXVal(); // get the X value from somewhere
    myObjArray[i].pos_y = GetNextYVal(); // get the y value from somewhere
}