Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. public static int[] sumNeighbours(int[] intArray){
  2. int[] resultArray = new int[intArray.length];
  3. if(intArray.length == 1){
  4. resultArray[0] = intArray[0];
  5. return resultArray;
  6. }
  7. else{
  8. for(int index = 0; index < intArray.length; index++){
  9. if(index == 0){
  10. resultArray[0] = intArray[0] + intArray[1];
  11. }
  12. else if(index == intArray.length - 1){
  13. resultArray[intArray.length - 1] = intArray[intArray.length - 1] + intArray[intArray.length - 2];
  14. }
  15. else{
  16. resultArray[index] = intArray[index - 1] + intArray[index] + intArray[index + 1];
  17. }
  18. }
  19. return resultArray;
  20. }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement