Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过索引Java在数组中查找异或_Java - Fatal编程技术网

通过索引Java在数组中查找异或

通过索引Java在数组中查找异或,java,Java,我有一个长度为15:10101000000010的字符串。我想对数组中的每一个备选索引进行如下排他性处理,如下所示 " //在这里,^一直跳过1个索引,直到字符串长度结束 " //这里^skips 2 index and let 2 go,skips 2 let 2 go。。。等等 我不知道如何接近这一点。创建一个参数为数组长度的方法,并让该方法返回一个整数0或1,这主要是因为它是异或 所以为了再次澄清。。。我希望这是做任何大小的数组。现在大概17岁 int z = a[3] ^ a[

我有一个长度为15:10101000000010的字符串。我想对数组中的每一个备选索引进行如下排他性处理,如下所示

"

//在这里,^一直跳过1个索引,直到字符串长度结束

"

//这里^skips 2 index and let 2 go,skips 2 let 2 go。。。等等

我不知道如何接近这一点。创建一个参数为数组长度的方法,并让该方法返回一个整数0或1,这主要是因为它是异或

所以为了再次澄清。。。我希望这是做任何大小的数组。现在大概17岁

    int z = a[3] ^ a[ 5] ^ a[ 7] ^ a[ 9] ^ a[11] ^ a[13] ^ a[15] ^ a[17] 
这是汉明码

一个字节的数据:10011010 创建数据字,为奇偶校验位留出空间:u1 0 0 1 1 0 1 0 计算每个奇偶校验位a的奇偶校验?表示正在设置的位位置:

位置1检查位1,3,5,7,9,11: ? _ 1 _ 0 0 1 _ 1 0 1 0. 偶数奇偶校验,因此将位置1设置为0:0 1 0 1 0 1 位置2检查位2,3,6,7,10,11:

0?1 _ 0 0 1 _ 1 0 1 0. 奇偶校验,因此将位置2设置为1:011 001 010 位置4检查位4,5,6,7,12:

01??0 0 1 _ 1 0 1 0. 奇数奇偶校验,因此将位置4设置为1:0 1 0 1 1 0 位置8检查位8,9,10,11,12:

0110101?1 0 1 0. 偶数奇偶校验,因此将位置8设置为0:0 1 0 1 0 1 0 代号:0111100101010

"


如果性能允许,我建议使用for循环。
初始化为0,然后在for循环内检查索引并执行XOR或continue。

将尝试某种通用解决方案

int source[] = ...; // fill the source array
int steps[] = ...; // fill the array of steps
int startIndex = 0; // assign value of first, 0 being the first element of the array
int result = xorMethod(startIndex, source, steps);

public int xorMethod (int startIndex, int sourceArray[], int stepsArray[]) {
    // going to ignore all tests, because I'm lazy
    int xorResult = sourceArray[startindex];
    for (int i=1, step=startIndex+stepsArray[0] ; step<sourceArrray.length ; i++, step+=stepsArray[i%stepsArray.length]+1) {
        tmpInt ^= sourceArray[step];
    }
    return xorResult;
}
和结果应包含源[2]^source[5]^source[8]或2^5^8

另一个例子:

int source[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19};
int steps[] = {1, 2};
int startIndex = 0;
int result = xorMethod(startIndex, source, steps);

结果应该包含源[0]^source[2]^source[5]^source[7]^source[10]^source[12]^source[15]^source[17]。

我知道这不关我的事,但我能问一下为什么吗?很抱歉,我不明白第二种模式是什么意思。所以您需要带起始索引的代码和带步骤的数组。对的旁注:上一个示例的大小可能为17,但使用17的索引会使您越界。为什么您总是将索引0跳过为2?您的示例字符串长度仅为14?它实际上是一个字符串还是一个数组?@Braj索引形成一个两步模式:跳过2个元素,不跳过任何元素。所以遵循3,6,7,10,11,14,15的模式,…步骤数组的确切含义是什么?步骤数组包含XOR ed元素之间的步骤。如果您想跳过一个元素,遵循模式1,3,5,…,它将包含1个元素,{1}。如果您想跳过1,然后跳过2,然后循环执行,步骤={1,2}@user3550935请告诉我这段代码是否有用,在这种情况下,请向上投票并接受正确的答案。
 int d[]=new int[7];
 for(int i=0;i<7;i++)
 {
 d[i]=sc.nextInt();
 }
 int p[]=new int[4];
 p[0]=d[0]^d[1]^d[3]^d[4]^d[6];
 p[1]=d[0]^d[2]^d[3]^d[5]^d[6];
 p[2]=d[1]^d[2]^d[3];
 p[3]=d[4]^d[5]^d[6];
int source[] = ...; // fill the source array
int steps[] = ...; // fill the array of steps
int startIndex = 0; // assign value of first, 0 being the first element of the array
int result = xorMethod(startIndex, source, steps);

public int xorMethod (int startIndex, int sourceArray[], int stepsArray[]) {
    // going to ignore all tests, because I'm lazy
    int xorResult = sourceArray[startindex];
    for (int i=1, step=startIndex+stepsArray[0] ; step<sourceArrray.length ; i++, step+=stepsArray[i%stepsArray.length]+1) {
        tmpInt ^= sourceArray[step];
    }
    return xorResult;
}
int source[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int steps[] = {2};
int startIndex = 2;
int result = xorMethod(startIndex, source, steps);
int source[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19};
int steps[] = {1, 2};
int startIndex = 0;
int result = xorMethod(startIndex, source, steps);