Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.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
在奇数大小的数组中组合每2个元素,保留最后一个元素(Java)_Java_Arrays_String - Fatal编程技术网

在奇数大小的数组中组合每2个元素,保留最后一个元素(Java)

在奇数大小的数组中组合每2个元素,保留最后一个元素(Java),java,arrays,string,Java,Arrays,String,下面是我尝试做的一个例子: 假设我有一个字符串数组,内容为{“a”、“b”、“c”、“d”、“e”} 和第二个数组,其内容为{“f”、“g”、“h”、“i”} 我想要在第一个数组上返回{“ab”、“cd”、“e”},在第二个数组上返回{“fg”、“hi”}的代码。我正在使用Java。下面是我写的不起作用的代码 int val; if ((ciphertextVals.length % 2) == 0) val = ciphertextVals.length/2;

下面是我尝试做的一个例子:

假设我有一个字符串数组,内容为{“a”、“b”、“c”、“d”、“e”} 和第二个数组,其内容为{“f”、“g”、“h”、“i”} 我想要在第一个数组上返回{“ab”、“cd”、“e”},在第二个数组上返回{“fg”、“hi”}的代码。我正在使用Java。下面是我写的不起作用的代码

    int val;
    if ((ciphertextVals.length % 2) == 0)
        val = ciphertextVals.length/2;
    else
        val = ciphertextVals.length/2 + 1;

    String[] encryptedBinary = new String[val];
    for (int i = 0; i <= ciphertextVals.length-1; i+=2)
    {
        String bin1 = Integer.toBinaryString(ciphertextVals[i]);
        String result;
        if (i == ciphertextVals.length-1)
        {
            result = bin1;
        }
        else
        {
            String bin2 = Integer.toBinaryString(ciphertextVals[i+1]);
            result = bin1 + bin2;
        }
        encryptedBinary[i/2+1] = result;
    }
int-val;
如果((ciphertextVals.length%2)==0)
val=ciphertextVals.length/2;
其他的
val=ciphertextVals.length/2+1;
String[]encryptedBinary=新字符串[val];

对于(int i=0;i实际上,您正在使逻辑变得复杂,这不是必需的。
你所说的可以写成这样的代码:

ArrayList<String> encryptedBinaryList = new ArrayList<>();
String temp = "";
for (int i = 0; i < ciphertextVals.length; i++) {
    if (i % 2 == 0) {
        temp = Integer.toBinaryString(ciphertextVals[i]);
    } else {
    temp += Integer.toBinaryString(ciphertextVals[i]);
        encryptedBinaryList.add(temp);
    }
}
if (ciphertextVals.length % 2 != 0) {
    encryptedBinaryList.add(temp);
}
String encryptedBinary[] = new String[encryptedBinaryList.size()];
encryptedBinaryList.toArray(encryptedBinary);

实际上,您正在使逻辑变得复杂,这不是必需的。
你所说的可以写成这样的代码:

ArrayList<String> encryptedBinaryList = new ArrayList<>();
String temp = "";
for (int i = 0; i < ciphertextVals.length; i++) {
    if (i % 2 == 0) {
        temp = Integer.toBinaryString(ciphertextVals[i]);
    } else {
    temp += Integer.toBinaryString(ciphertextVals[i]);
        encryptedBinaryList.add(temp);
    }
}
if (ciphertextVals.length % 2 != 0) {
    encryptedBinaryList.add(temp);
}
String encryptedBinary[] = new String[encryptedBinaryList.size()];
encryptedBinaryList.toArray(encryptedBinary);

像这样的方法应该会奏效:

public static void main(String[] args) {

    String[] input1 = { "a", "b", "c", "d", "e" };
    String[] input2 = { "f", "g", "h", "i" };
    String[] input3 = {};

    System.out.println(Arrays.toString(combine(input1))); //prints [ab, cd, e]
    System.out.println(Arrays.toString(combine(input2))); // prints [fg, hi]
    System.out.println(Arrays.toString(combine(input3))); // prints []
}

public static String[] combine(String[] input) {
    boolean isOdd = input.length % 2 != 0;

    int newLength = (input.length + 1) / 2;
    String[] output = new String[newLength];

    for (int i = 0; i < input.length / 2; i++) {
        output[i] = input[2 * i] + input[2 * i + 1];
    }

    if (isOdd) {
        output[output.length - 1] = input[input.length - 1];
    }

    return output;
}
publicstaticvoidmain(字符串[]args){
字符串[]input1={“a”、“b”、“c”、“d”、“e”};
字符串[]input2={“f”、“g”、“h”、“i”};
字符串[]input3={};
System.out.println(Arrays.toString(combine(input1));//prints[ab,cd,e]
System.out.println(Arrays.toString(combine(input2));//prints[fg,hi]
System.out.println(Arrays.toString(combine(input3));//prints[]
}
公共静态字符串[]组合(字符串[]输入){
布尔isOdd=input.length%2!=0;
int newLength=(input.length+1)/2;
字符串[]输出=新字符串[newLength];
对于(int i=0;i
类似的方法应该可以:

public static void main(String[] args) {

    String[] input1 = { "a", "b", "c", "d", "e" };
    String[] input2 = { "f", "g", "h", "i" };
    String[] input3 = {};

    System.out.println(Arrays.toString(combine(input1))); //prints [ab, cd, e]
    System.out.println(Arrays.toString(combine(input2))); // prints [fg, hi]
    System.out.println(Arrays.toString(combine(input3))); // prints []
}

public static String[] combine(String[] input) {
    boolean isOdd = input.length % 2 != 0;

    int newLength = (input.length + 1) / 2;
    String[] output = new String[newLength];

    for (int i = 0; i < input.length / 2; i++) {
        output[i] = input[2 * i] + input[2 * i + 1];
    }

    if (isOdd) {
        output[output.length - 1] = input[input.length - 1];
    }

    return output;
}
publicstaticvoidmain(字符串[]args){
字符串[]input1={“a”、“b”、“c”、“d”、“e”};
字符串[]input2={“f”、“g”、“h”、“i”};
字符串[]input3={};
System.out.println(Arrays.toString(combine(input1));//prints[ab,cd,e]
System.out.println(Arrays.toString(combine(input2));//prints[fg,hi]
System.out.println(Arrays.toString(combine(input3));//prints[]
}
公共静态字符串[]组合(字符串[]输入){
布尔isOdd=input.length%2!=0;
int newLength=(input.length+1)/2;
字符串[]输出=新字符串[newLength];
对于(int i=0;i
argh.晚了几秒钟。我输入了这段代码,当我输入一个7元素数组时,它返回了一个8元素数组。可能。输入是{6,14,24,24,10,12,14}。我会检查并确保我添加的代码正确。嘿,它现在起作用了!非常感谢你,你是最棒的!啊。晚了几秒钟。我输入了此代码,当我输入一个7元素数组时,它最终返回了一个8元素数组。可能。输入是{6,14,24,24,10,12,14}.我会检查并确保我添加的代码正确。嘿,现在可以了!非常感谢你,你是最棒的!