搜索阵列在特定的指数值 [英] Searching arrays for a value at specific indices

查看:91
本文介绍了搜索阵列在特定的指数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经宣布了几个阵列,这些阵列中,我们有以下值:

I have declared a few arrays, in these array we have the following values:

{0, 1, 1, 0, 0}
{1, 0, 0, 0, 1}
{0, 1, 0, 0, 0}

我有相同数量的值的多个阵列。

I have multiple arrays with the same number of values.

有关我的Andr​​oid应用程序,我将有一组按钮。

For my Android application I am going to have a set of buttons.

如果按钮1被推动,我想知道是否有任何1的是在第1位,第3,和第5和多少。如果按钮2被推动时,我想知道是否有任何1的是在第1位,第2,第3和4以及有多少

If Button 1 is pushed, I want to know if any 1's are in positions 1, 3, and 5 and how many. If Button 2 is pushed, I want to know if any 1's are in positions 1, 2, 3, and 4 and how many.

我有搜索显示如何找出如果1的阵列中存在,但不是在特定位置的一切。任何建议/帮助?

Everything I have searched shows how to find out if 1's exist in the array, but not at a specific position. Any suggestions/help?

这是什么,我试图完成,如果你想阅读,但我应该能够把事情做好了有益的解决方案,我的上述问题的详细信息:

Details on what I am trying to accomplish if you wish to read, though I should be able to figure everything out with a helpful solution to my above question:

我在13阵列排列在一个8x8格64按钮重新人体躯干的present节和64个数字(0或1的)。该阵列确定是否一个机构是在上述躯干部分之一present(1,如果器官是present,0如果没有present)。

I have 64 buttons arranged in an 8x8 grid to represent sections of the human torso, and 64 numbers (0's or 1's) in my 13 arrays. The arrays identify if an organ is present in one of the aforementioned torso sections (1 if the organ is present, 0 if not present).

我要推一个按钮,搜索所有的数组为1在该位置。如果我按下按钮35,我想知道,如果肝脏是在该节present。

I want to push a button, and search all of the arrays for a 1 at that position. If I push button 35, I want to know if the liver is present in that section.

我的最终产量将告诉用户有多大比例的器官很可能是在该节。如果一个部分包含一块的脏器这将是一个1,然后通过含有该机构部的总数除以。

My ultimate output will tell the user what percentage of the organ is likely to be in that section. If a section contains a piece of the organ it will be a 1, and then divided by the total number of sections containing that organ.

如果你读过这一步,我是从正确的角度袭击这个问题,你有更多的想法?

If you've read this far, am I attacking this problem from the correct angle, do you have additional ideas?

推荐答案

从你在你的问题描述一下,你可以这样做只是这样的:

From what you have describe in your question, you can do it just like this:

假设你有一个数组:

int[][] numArray = {{0, 1, 1, 0, 0},
                    {1, 0, 0, 0, 1},
                    {0, 1, 0, 0, 0}};

您只需要有这样的方法:

you just need to have method like this:

int countOne(int[] num){  
    int count=0;
    for(int i=0;i<numArray.length;i++){
        for(int j=0;j<num.length;j++){
            if(numArray[i][num[j]]==1){
                count++;
            }
        }
    }
    return count;
}   

<青霉>如果按钮1被推动,我想知道是否有任何1的是在第1位,第3,和第5和多少:的

调用方法:

 int[] button1 = {0,2,4};
 System.out.println("the number of 1's:"+countOne(button1));

<青霉>如果按钮2被推动时,我想知道是否有任何1的是在第1位,第2,第3和4以及有多少。的

 int[] button2 = {0,1,2,3};
 System.out.println("the number of 1's:"+countOne(button2));

这篇关于搜索阵列在特定的指数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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