How to compare a certain value to value in array?

126 views Asked by At

I want to output certain values, which I declare in other array, from an array. Now I got something like that:

int [] tab = new int[110];
int [] skip = {3,4,8,9,12,13,16,17,20,21};

for (int i=0; i<110; i++) {
     if (i == any_value_of_skip[])
         System.out.print(tab[i]+", ");
}

I put 1 and 0 bits into tab[] array and I want to output those bits, at which index in tab[] array has exact number as in skip[] array.

[edit] tab[] array has 1 and 0, because:

for (int i = 0; i<110; i++) {
    Random r = new Random();
    tab[i] = r.nextInt(2);

    if (i%25==0) {
        System.out.println("");
    }

    System.out.print(tab[i]+"("+i+")"+", ");
}

System.out.println("");

And I want to apply Hamming code here, so I need to output those bits, which indexes in tab[] array has parity, like 1,2,4,8,16,32,64.

To better describe it, here is tutorial what I use to make a program https://www.youtube.com/watch?v=JAMLuxdHH8o I am at moment, where I want to output bits, which indexes are: 2,3,6,7,10,11,14,15 and so on...

2

There are 2 answers

1
Elliott Frisch On BEST ANSWER

If you want to output the values of the array, in my opinion the simplest method is probably a for-each loop:

int[] skip = {3, 4, 8, 9, 12, 13, 16, 17, 20, 21};
for (int i : skip) {
    System.out.print(i + ",");
}
System.out.println();

If you wish to print tab values (and you have not posted how they are initialized) you might use tab[i]:

int [] skip = {3, 4, 8, 9, 12, 13, 16, 17, 20, 21};
for (int i : skip) {
    System.out.print(tab[i] + ",");
}
System.out.println();

Or just use Arrays.toString(int[]):

System.out.println(Arrays.toString(skip));

Since tab is an array of 110 zeros, there is not much you would be able to do with it.

0
ScriptyChris On

Ok so i got to the point, where i need to check if bits from an every "pair" array are even or odd (i mean 1 bits, cause 0 doesn't count). How to check sum of certain bits from an array?

Whole code i wrote till now is:

package teleinformatykalab2;

import java.util.Arrays;
import java.util.List;
import java.util.Random;


public class TeleinformatykaLab2 {

    //Funkcja wypełniająca tablicę ciągiem losowych bitów 0 lub 1 //
    public static void Losowanie(int tab[]){

        ////int [] tab = new int[110];

        for (int i = 0; i<110; i++)
        {
            Random r = new Random();
            tab[i] = r.nextInt(2);
            if (i%25==0)
            {
                System.out.println("");
            }
            System.out.print(tab[i]+"("+i+")"+", ");

        }
        System.out.println("");

}
    /////////////////


    //Funkcja wyświetla bity, których indeks w tablicy jest wartością kolejnej potęgi liczby dwa//
    public static void zamienBity(int tab[], int tabBit[])
    {
        ////System.out.println("\n"+tab[0]+tab[109]+"\n");

        for (int i=0; i<110; i++)
        {

            if (isPowerOfTwo(i))
                {

                //tabBit[i]=tab[i];
                System.out.print(tab[i]+"("+i+")"+", ");
                }
            if (i%50==0)
            {
                System.out.println("");
            }

        }

        System.out.println("\n");

    }

    //Sprawdzenie ciągu kodem Hamminga//
    public static void sprawdzHamming(int tab[], int tabBit[])
    {
        // przypisanie do tabBit bitow o indeksie potęgi dwa
        for(int i=0;i<tabBit.length;i++)
        {
            tabBit[i] = tab[(int)Math.pow(2,i)];
            System.out.print(tabBit[i]+", ");
        }
        System.out.println("////\n");

        //sprawdzenie pierwszej pary bitów
        System.out.println("\nPierwsza para");

        int [] skip = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,36,39,41,43,45,47,49,
                        51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95,97,99,
                        101,103,105,107,109};    
        for (int i : skip)
        {
             System.out.print(tab[i] + "("+i+")"+",");

                if (i%25==0)
                {
                    System.out.println("");
                }
        }
        System.out.println("");

        //sprawdzenie drugiej pary bitów
        System.out.println("\nDruga para");

         int skip2 []= {3,6,7,10,11,14,15,18,19,22,23,26,27,30,31,34,35,38,39,42,43,46,47,
                        50,51,54,55,58,59,62,63,66,67,70,71,74,75,78,79,82,83,86,87,90,91,94,95,98,99,102,103,106,107};
          for (int j : skip2)
          {
          System.out.print(tab[j] + "("+j+")"+",");
          if (j%25==0)
                {
                    System.out.println("");
                }
          }
        System.out.println();

        //sprawdzenie trzeciej pary bitów
        System.out.println("\nTrzecia para");

         int skip3 []= {5,6,7, 12,13,14,15, 20,21,22,23, 28,29,30,31, 36,37,38,39, 44,45,46,47,
                        52,53,54,55, 60,61,62,63, 68,69,70,71, 76,77,78,79, 84,85,86,87, 92,93,94,95, 100,101,102,103, 108,109};
          for (int k : skip3)
          {
          System.out.print(tab[k] + "("+k+")"+",");
          if (k%18==0)
                {
                    System.out.println("");
                }
          }
        System.out.println();

         //sprawdzenie czwartej pary bitów
        System.out.println("\nCzwarta para");

         int skip4 []= {9,10,11,12,13,14,15,16, 25,26,27,28,29,30,31,32, 41,42,43,44,45,46,47,48,
                       57,58,59,60,61,62,63,64, 73,74,75,76,77,78,79,80, 89,90,91,92,93,94,95,96,
                       105,106,107,108,109};
          for (int l : skip4)
          {
          System.out.print(tab[l] + "("+l+")"+",");
          if (l%8==0)
                {
                    System.out.println("");
                }
          }
        System.out.println();

        //sprawdzenie piątej pary bitów
        System.out.println("\nPiąta para");

        for (int m=17; m<110; m++)
        {
            if ((m>=32 && m<=47) && (m>=64 && m<=79) && (m>=96 && m<=111)) continue;
            System.out.print(tab[m] + "("+m+")"+",");
            if (m%16==0)
                {
                    System.out.println("");
                }
        }
        System.out.println();

        //sprawdzenie szóśtej pary bitów
        System.out.println("\nSzóśta para");

        for (int m=33; m<110; m++)
        {
            if (m>=65 && m<=96)/* && (m>=64 && m<=79) && (m>=96 && m<=111))*/ continue;
            System.out.print(tab[m] + "("+m+")"+",");
            if (m%16==0)
                {
                    System.out.println("");
                }
        }
        System.out.println();

        //sprawdzenie siódmej pary bitów
        System.out.println("\nSiódma para");

        int [] sixtyfour = new int [120];

        for (int m=65; m<110; m++)
        {
            //if (m>=65 && m<=96)/* && (m>=64 && m<=79) && (m>=96 && m<=111))*/ continue;
            System.out.print(tab[m] + "("+m+")"+",");
            if (m%16==0)
                {
                    System.out.println("");
                }
        }
        System.out.println("\n");
        for (int n=65; n<110; n++)    
        {    
            sixtyfour[n] = tab[n];
            System.out.print(sixtyfour[n]+". ");
            if (n%16==0)
                {
                    System.out.println("");
                }
        }
        System.out.println();
}


    private static boolean isPowerOfTwo(int x)
    {
        //return (x & (x-1)) ==0;
         return (x!=0) && ((x&(x-1)) ==0);
    }

    public static void main(String[] args) {

        int [] tab = new int[110];


    // wywołanie metody wypełniającej tablicę ciągiem losowych bitów 0 lub 1
        Losowanie(tab);
        System.out.println("\n Bity potęgi dwa: ");
        int [] tabBit = new int [7];


        zamienBity(tab,tabBit);

        System.out.println("////Bity potęgi 2");
        sprawdzHamming(tab,tabBit);

        int liczby [] = new int[120];
        System.out.println("\n");
        for (int z=1; z<120;z++)
        {
            liczby[z]=z;
            System.out.print(liczby[z]+", ");
            if (z%32==0)
                {
                    System.out.println("");
                }
        }

    }
}