0

public int combineArrays(int[] a, int[] b) {

int[] result = new int[a.length + b.length]; 
int index = 0;

for (int i : a) {
  result[index] = i;
  index++;
}
for (int i : b) { 
  result[index] = i;
  index++; 
}
return result;

} Apologies, here is the correct code.

  • Note that you're assuming the arrays are the same length here. You need to decide how to handle different length arrays. – Andy Turner Nov 14 '18 at 22:05
  • `System.arraycopy` – MadProgrammer Nov 14 '18 at 22:05
  • @BackSlash I was thinking of just append one to the other :/, to interleaving them – MadProgrammer Nov 14 '18 at 22:08
  • Method should return an array, i.e. the signature should be `public int[] combineArrays(int[] a, int[] b)` instead – Mick Mnemonic Nov 14 '18 at 22:11
  • 1
    `Stream.of(a, b).flatMapToInt(Arrays::stream).toArray()` – shmosel Nov 14 '18 at 22:18
  • By the way, if you aren't really doing this for an assignment, I'd highly recommend using something like: al=new ArrayList(a);al.addAll(b); I recommend leaving it as an ArrayList when you are done and would even say that a and b should already be ArrayLists. Do you have a strong reason to use arrays directly??? (Hint: There almost isn't a strong reason to use arrays directly except when directed because you're supposed to be learning how to code.) – Bill K Nov 14 '18 at 22:27

0 Answers0