0

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.

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

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.

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:

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).

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.

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?

Zong
  • 6,160
  • 5
  • 32
  • 46
  • iterate through the array and check the values? – leigero Feb 26 '14 at 04:54
  • What is the problem, precisely? When you ask "am I attacking this problem from the correct angle", what are you referring to? I'm sure you know how to access an array... – Zong Feb 26 '14 at 04:55
  • without starting and showing any codes, its more like you are asking us to do your job – Baby Feb 26 '14 at 04:56
  • You might want to look into a SparseArray – midhunhk Feb 26 '14 at 04:58
  • @RafaEl I don't think I can use ArrayList, HashSet, binary search, or a foreach loop. My understanding is that those options only let me search for existence of a value in the entire array, not for existence of a value at a specific point in the array. I haven't shown any code because none of the above would work, and I don't know where to start on this part of my project. – William From Detroit Feb 26 '14 at 05:06
  • Not entirely sure what the issue is but looks like you could make use of bitvector and lookup table to quickly find the number of set bit in a byte. – anonymous Feb 26 '14 at 05:07
  • @silverback the "valueAt" method looks promising, Do you know of any resources that might help me understand it and how it is used? I am youtubing it now, perhaps you know of a good tutorial. – William From Detroit Feb 26 '14 at 05:13
  • You do know `array[index]` gives you the value at the given index in the array, right? I really hope you do... – Zong Feb 26 '14 at 05:15
  • @ZongZhengLi no I don't. Can array[index] find multiple instances of a value at different indices? I'm pretty new at this. This is for a school project, and I'm the person in my group who is supposed to complete this application. I've not taken a single programming class. I've tried to learn on my own, but do not have the time to learn within the due date so I attempt to do what I can. Every time I don't understand something, I ask, and I get or see responses like that, and I get discouraged from actually trying to learn anything. – William From Detroit Feb 26 '14 at 05:28
  • That is very unfortunate. Somehow you became aware of arrays without knowing about their most basic of operations. I strongly suggest reviewing the fundamentals of data structures such as arrays and constructs such as loops. Without a minimum level of knowledge I think you'll find it difficult to complete this project. – Zong Feb 26 '14 at 05:46
  • Can you check this http://stackoverflow.com/a/393255/592025 – midhunhk Feb 26 '14 at 07:28

2 Answers2

1

I suggest you might want to do this using binary functions. here is an example for when button 2 is pressed first we declare the arrays as columns instead of rows

    byte col1 = 8; // equivalent of 0 1 0 0 0
    byte col2 = 1; //               0 0 0 0 1
    byte col3 = 2; //               0 0 0 1 0
    // this makes your original array look like
    // 0 0 0
    // 1 0 0
    // 0 0 0
    // 0 0 1
    // 0 1 0
    System.out.println(bitcount(col1) + bitcount(col2) + bitcount(col3));

    /* to set or unset bytes
    my_byte = my_byte | (1 << pos);
    To un-set a bit:
    my_byte = my_byte & ~(1 << pos);
    */
    }
static private int bitcount(byte n)  {
       int count = 0 ;
       while (n != 0)  {
          count++ ;
          n &= (n - 1) ;
       }
       return count ;
}

then we count by counting the bits in of each of the columns we want to count
Construction an logical expression which will count bits in a byte

Community
  • 1
  • 1
clancer
  • 613
  • 4
  • 10
  • Taking the log would give you the MSB, not the count, I believe. – Zong Feb 26 '14 at 05:05
  • @Zong Zheng Li you are right, will have to apply one of these methods instead http://stackoverflow.com/questions/2896374/construction-an-logical-expression-which-will-count-bits-in-a-byte – clancer Feb 26 '14 at 05:11
  • Better to just use [BitSet](http://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html) anyway. – Zong Feb 26 '14 at 05:12
1

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

Lets say you have an array:

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;
}   

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

call the method:

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

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

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