如何将某个值与数组中的值进行比较?

How to compare a certain value to value in array?

我想从一个数组中输出我在其他数组中声明的某些值。 现在我得到了类似的东西:

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]+", ");
}

我将 1 and 0 位放入 tab[] array 并且我想输出那些位,在 index in tab[] 数组中 具有 确切数字 ,如 skip[] 数组 .

[编辑] tab[]数组有1和0,因为:

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("");

我想在这里应用汉明码,所以我需要输出那些位,tab[]数组中的索引具有奇偶校验,比如1,2,4,8,16,32,64

为了更好地描述它,这是我用来制作程序的教程https://www.youtube.com/watch?v=JAMLuxdHH8o我现在,我想输出位,索引是:2,3,6,7,10 ,11,14,15 等等...

如果你想输出数组的值,我认为最简单的方法可能是 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();

如果您希望打印 tab 值(并且您尚未发布它们的初始化方式),您可以使用 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();

或者只使用 Arrays.toString(int[]):

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

由于 tab 是一个由 110 个零组成的数组,因此您无能为力。

好的,我到了要点,我需要检查每个 "pair" 数组中的位是偶数还是奇数(我的意思是 1 位,因为 0 不算数)。如何检查数组中某些位的总和?

到目前为止我写的全部代码是:

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("");
                }
        }

    }
}