将两个int数组连接并排序到一个int数组中

时间:2021-11-30 12:21:09

I know a similar question has been asked and I have researched this website. I have tried to use some of the answers but my syntax is still not working.

我知道有类似的问题已被问及我已经研究过这个网站。我试图使用一些答案,但我的语法仍然无法正常工作。

I am going through a previous examination paper to help build my knowledge of Java. Please forgive any errors in my code, I am still learning the Java syntax.

我正在阅读以前的试卷,以帮助我建立自己的Java知识。请原谅我的代码中的任何错误,我仍在学习Java语法。

Here is my question:

这是我的问题:

Implement a method static int[] splice(int[] v, int[] w) that (assuming the lengths of v and w are the same) returns an array in which the elements if v and w are interleaved, starting with the first element of v, followed by the first element of w, followed by the second element of v, etc. For example, if v = { 1, 3, 5 } and w = { 2, 4, 6 } the call splice should return { 1, 2, 3, 4, 5, 6 }.

实现一个方法static int [] splice(int [] v,int [] w)(假设v和w的长度相同)返回一个数组,其中v和w的元素是交错的,从第一个开始v的元素,后跟w的第一个元素,后跟v的第二个元素等。例如,如果v = {1,3,5}和w = {2,4,6},则调用拼接应该返回{1,2,3,4,5,6}。

If the input arrays have different lengths, the method should return null.

如果输入数组具有不同的长度,则该方法应返回null。

Here is my code:

这是我的代码:

import java.util.*;

public class TwoArraysBecomeOne {

public static void main(String[] args) {

    int[] v = { 1, 3, 5 };
    int[] w = { 2, 4, 6 };

    splice(v,w);
}

public static int[] splice(int[] v, int[] w){

    if(v != w) {
        return null;
    }

    int[] x = new int[v.length + w.length];

    for (int i = 0; i < v.length; i++) {
        x[i] = v[i] + w[i];
    }

    for (int j = 0; j < x.length; j++) {
        System.out.println(x[j]);
    }

    return x;

    }
}

I am aware that my current syntax is producing a new array x = { 3, 7, 11 }. I have removed the various attempts to try and concatenate my code as this was causing errors in my code. I just require some pointers to help answer this question.

我知道我当前的语法正在生成一个新的数组x = {3,7,11}。我已经删除了尝试连接我的代码的各种尝试,因为这导致我的代码出错。我只需要一些指示来帮助回答这个问题。

Again, please forgive any errors in my code, I am still learning Java.

再次,请原谅我的代码中的任何错误,我仍在学习Java。

Thank you.

2 个解决方案

#1


2  

try this

public static void main(String[] args) throws Exception {
    int[] v = { 1, 3, 5 };
    int[] w = { 2, 4, 6 };
    int[] res = splice(v, w);
    System.out.println(Arrays.toString(res));
}

private static int[] splice(int[] v, int[] w) {
    if (v.length != w.length) {
        return null;
    }
    int[] a = new int[v.length + w.length];
    for (int i = 0; i < v.length; i++) {
        a[i * 2] = v[i];
        a[i * 2 + 1] = w[i];
    }
    return a;
}

output

[1, 2, 3, 4, 5, 6]

#2


2  

You just have a small error, you don't want to be summing the elements of the arrays, but placing them side by side in the new array

你只是有一个小错误,你不想总结数组的元素,但将它们并排放在新数组中

for (int i = 0; i < v.length; i++) {
    x[i*2] = v[i];
    x[i*2+1] = w[i];
}

#1


2  

try this

public static void main(String[] args) throws Exception {
    int[] v = { 1, 3, 5 };
    int[] w = { 2, 4, 6 };
    int[] res = splice(v, w);
    System.out.println(Arrays.toString(res));
}

private static int[] splice(int[] v, int[] w) {
    if (v.length != w.length) {
        return null;
    }
    int[] a = new int[v.length + w.length];
    for (int i = 0; i < v.length; i++) {
        a[i * 2] = v[i];
        a[i * 2 + 1] = w[i];
    }
    return a;
}

output

[1, 2, 3, 4, 5, 6]

#2


2  

You just have a small error, you don't want to be summing the elements of the arrays, but placing them side by side in the new array

你只是有一个小错误,你不想总结数组的元素,但将它们并排放在新数组中

for (int i = 0; i < v.length; i++) {
    x[i*2] = v[i];
    x[i*2+1] = w[i];
}