如何将特定元素从数组存储到另一个数组? [英] how to store specific element from an array to different another array?

查看:120
本文介绍了如何将特定元素从数组存储到另一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我想从[]中搜索整数值= 1并存储在新数组b []中,则数组a []包含整数元素。任何人都可以告诉我怎么做c?

for example an array a[] contain integer element if i want to search for the integer value=1 from a[] and store in new array b[]. Can anyone tell me how to do in c?

推荐答案

我假设这是你之前问过的同样问题:如何将数组中的特定元素内容存储到另一个数组? [ ^ ]

在这种情况下,您要存储的是元素的索引,而不是元素本身(这没有任何意义)总而言之,因为新阵列将全部为1。



这并不困难,事实上它几乎肯定是你的作业 - 所以我赢了'给你代码!

实现这个:

1)创建一个与输入数组大小相同的新整数数组。称之为索引。

2)创建一个整数,调用nextIndex,将其设置为零。

3)使用for循环遍历所有元素你的inputArray,一个名为i的循环变量。

3.1)如果输入数组元素i等于你要查找的值(在你的例子中是1),那么存储值i到元素nextIndex的索引,并将nextIndex增加一。

4)循环后,nextIndex是你找到的值的数量和索引 包含输入数组中值的索引。



试一试。这不难! :笑:
I am assuming that this is the same problem you asked about before: how to get specific element content in an array an store to another array ?[^]
In which case what you are trying to store is the index to the elements, not the elements themselves (which would make no sense at all, since the new array would be all "1"s).

This isn't difficult, in fact it's almost certainly your homework - so I won't give you the code!
Implement this:
1) Create a new integer array, the same size as the input array. Call it "indexes".
2) Create an integer, call "nextIndex", set it to zero.
3) Use a for loop to run through all elements of your inputArray, which a loop variable called "i".
3.1) If input array element "i" is equal to the value you are looking for ("1" in your example), then store the value of "i" into "indexes" at element "nextIndex" and increment "nextIndex" by one.
4) After the loop, "nextIndex" is the number of values you found, and "indexes" contains the indexes to the values in the input array.

Give it a try. This isn't difficult! :laugh:






如上所述,您不知道新数组的大小,在这种情况下您可以使用以下,

创建一个arraylist然后再将该arraylist转换为integer类型的数组。



示例:



//假设您从前一个阵列得到一个数字,那就是100



x = a [3];



//添加此获取值arraylist



ArrayList arrlist =新ArrayList();



//arrList.Add(a[3]);或者以下

arrList.Add(x);

//将所需元素添加到arraylist后,需要将它们转换为整数数组。



int [] arrResult = arrList.ToArray((TypeOf(int)))as int [];



//这个arrResult数组包含你的价值..



谢谢......希望这能解决你的问题......
Hi,

As describe above you dont know the size of new array in that case you can use below,
Create an arraylist then converting that arraylist into array of type integer again.

Example:

// let assume you got a number from your previous array, That is 100

x=a[3];

// add this fetched value arraylist

ArrayList arrlist=New ArrayList();

//arrList.Add(a[3]); Or below
arrList.Add(x);
// after adding required elements to arraylist, You need to convert them to array of integers.

int[] arrResult=arrList.ToArray((TypeOf(int))) as int[];

//This arrResult array contains your value..

Thanks...Hope this solve your problem...


for(thresholdcount=0;thresholdcount<sample_length;thresholdcount++)>
{
 if(thxcross_pos[thresholdcount]==1)
	{
	//PZLoctn[thresholdcount]=thxcross_pos[thresholdcount];
	
	PZLoctn[thresholdcount]=thresholdcount;
	//printf("pzloctn:%f\n",PZLoctn[thresholdcount]);
	PZLoctn_counter=PZLoctn_counter+1;	
	}

   else if(thxcross_pos[thresholdcount]==-1)
        {
	//NZLoctn[thresholdcount]=thxcross_pos[thresholdcount];
	NZLoctn[thresholdcount]=thresholdcount;
       // printf("%f\n",NZLoctn[thresholdcount]);	
	NZLoctn_counter= NZLoctn_counter+1;
	}
}
printf("PZLoctn_counter:%d\tNZLoctn_counter: %d\n",PZLoctn_counter,NZLoctn_counter);
for(count=0;count<pzloctn_counter;count++)>
{
printf("\nnew array:%f",PZLoctn[count]);
}


这篇关于如何将特定元素从数组存储到另一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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