Ios 如何在不使用NSArray、NSDictionary、NSSet的情况下创建阵列。想要创建数组、添加元素和删除元素这有意义吗?

Ios 如何在不使用NSArray、NSDictionary、NSSet的情况下创建阵列。想要创建数组、添加元素和删除元素这有意义吗?,ios,objective-c,arrays,Ios,Objective C,Arrays,如何在不使用NSArray、NSDictionary、NSSet甚至NSMutableArray、NSMutableDictionary的情况下创建数组?我想在Xcode中使用Objective-C创建一个数组、添加元素和删除元素 我的主要目标是通过提供objectAtIndex来添加和删除元素,并显示数组中的元素数 int n[ 10 ]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array

如何在不使用
NSArray
NSDictionary
NSSet
甚至
NSMutableArray
NSMutableDictionary
的情况下创建数组?我想在Xcode中使用Objective-C创建一个数组、添加元素和删除元素

我的主要目标是通过提供objectAtIndex来添加和删除元素,并显示数组中的元素数

int n[ 10 ]; /* n is an array of 10 integers */
int i,j;

/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ )
{
    n[ i ] = i + 101; /* set element at location i to i + 100 */
}

/* output each array element's value */
for (j = 0; j < 10; j++ )
{
    printf("Element[%d] = %d\n", j, n[j] );
}

printf("Number of Elements in Array %lu",sizeof(n) / sizeof(int));


//    /* loop for the deletion  */
int loc = 5,num = 10;
printf("\nRemove  Element[%d] from Array:",loc);
while (loc < num) {
    n[loc - 1] = n[loc];
    loc++;
}
num--;  // No of elements reduced by 1

//Print Array
for (i = 0; i < num; i++)
    printf("\n %d", n[i]);

NSLog(@"Count of Array %d",num);
int n[10];/*n是由10个整数组成的数组*/
int i,j;
/*将数组n的元素初始化为0*/
对于(i=0;i<10;i++)
{
n[i]=i+101;/*将位置i处的元素设置为i+100*/
}
/*输出每个数组元素的值*/
对于(j=0;j<10;j++)
{
printf(“元素[%d]=%d\n”,j,n[j]);
}
printf(“数组%lu中的元素数”,sizeof(n)/sizeof(int));
///*用于删除的循环*/
int loc=5,num=10;
printf(“\n从数组中删除元素[%d]:,loc);
while(loc<代码> > p>可以在C++中使用STD::vector。您可以使用Swift数组

显然,有很多方法可以做到这一点,而不必使用前面提到的任何类型/结构,但我想问题是为什么?我想NSMutableArray正是您所需要的。 但要回答您的问题,只需定义数组:例如

NSString *aString[20];

但是,您将负责向数组中添加和从数组中删除,这在时间上很昂贵,而且容易出现编码错误。

为什么要这样做?有没有具体原因?因此,您要问的是如何在obj-c中编写一个类似数组的容器,它可以根据需要进行扩展和收缩,支持随机访问,但是不使用任何obj-c容器类型?堆栈溢出不是编码服务。您正在询问如何复制
NSMutableArray
。简短回答:“通过编写大量的代码。”这是企业中众所周知的“已解决的问题”。你为什么要做大量的工作来完成操作系统已经提供的东西?这闻起来像是学校布置的作业。@Duncac:这意味着这个可怜的孩子必须写代码。可怜的小尼基,我也不想用swift阵法!(抱歉,问题陈述定义了这个!)所以我不得不在Xcode中使用基本的c逻辑。