// Helene Martin, CSE 142 // Examples of passing arrays as parameters and returning them from methods import java.util.*; public class ArrayExamples { public static void main(String[] args) { int[] vals = {6, 2, 4, 5}; System.out.println(sum(vals)); int[] vals2 = {5, 23, 7, 56}; int[] merged = merge(vals, vals2); System.out.println(Arrays.toString(merged)); } // takes in two arrays of integers // returns a new array containing elements from the first array // followed by elements from second public static int[] merge(int[] a1, int[] a2) { int[] merged = new int[a1.length + a2.length]; for (int i = 0; i < a1.length; i++) { merged[i] = a1[i]; } for (int i = 0; i < a2.length; i++) { merged[a1.length + i] = a2[i]; } return merged; } // takes in an array of integers // returns the sum of all its values public static int sum(int[] values) { int total = 0; for (int i = 0; i < values.length; i++) { total += values[i]; } return total; } }