0

Suppose a={1,2,3} and b={4,5,6} and I want to return an array containing the middle elements of a and b, i.e., {2,5}. I tried to use "merge":

public int[] middleValueArray(int[] a, int[] b) {
 int[] both=merge(a[1],b[1]);
 return both;
}

This does not seem to work. Is there a simple way to approach this? Thanks!

John
  • 21
  • 4

4 Answers4

1

More general solution will be:

public int[] middleValueArray(int[] a, int[] b) {
    return new int[]{a[a.length/2], b[b.length/2]};
}
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
0

you can do it like this:

public int[] middleValueArray(int[] a, int[] b) {
 int[] both=new int[]{a[1],b[1]};
 return both;
}
Václav Struhár
  • 1,739
  • 13
  • 21
0

Use this:-

public int[] middleValueArray(int[] a, int[] b) {
     int[] both={a[1],b[1]};
     return both;
    }
bit-shashank
  • 887
  • 11
  • 27
0

More general solution would be to convert it to list and then add list one by one..for example..

 import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    public class Main {
       public static void main(String args[]) {
          String a[] = { "A", "E", "I" };
          String b[] = { "O", "U" };
          List list = new ArrayList(Arrays.asList(a));
          list.addAll(Arrays.asList(b));
          Object[] c = list.toArray();
          System.out.println(Arrays.toString(c));
       }
    }