// Zorah Fung, CSE 142 // An example of using Arrays as parameters and returns in methods import java.util.*; // to use the Arrays utility class public class ArrayMethods { public static void main(String[] args) { int[] a = {42, 17, 30, 2}; int[] b = {10, 51, 26}; System.out.println("Sum of array a is: " + sum(a)); System.out.println("a + b = " + Arrays.toString(concatenate(a, b))); } // Returns the sum of the values in the given array public static int sum(int[] a) { int sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } // Returns a new array representing the result of concatenating the given // a and b arrays public static int[] concatenate(int[] a, int[] b) { int[] result = new int[a.length + b.length]; // Copy everything from a for (int i = 0; i < a.length; i++) { result[i] = a[i]; } // Copy everything from b into the remainder of result for (int i = 0; i < b.length; i++) { result[a.length + i] = b[i]; } return result; } }