Question: Looping Over an Array For this one you start with a hard-coded, but large-ish, array. Your job is to print out, on separate lines, the

Looping Over an Array

For this one you start with a hard-coded, but large-ish, array.

Your job is to print out, on separate lines, the length of the array, then each element in order they appear in the array.

public class PrintArray { public static void main(String[] args) { int[] a = {249, 243, 234, 488, 192, 235, 127, 203, 370, 473, 392, 126, 278, 403, 386, 17, 220, 331, 135, 171, 45, 377, 339, 123, 377, 113, 276, 147, 307, 480, 240, 449, 20, 135, 180, 425, 350, 242, 103, 225, 496, 379, 337, 221, 475, 157, 402, 284, 395, 220, 475, 375, 479, 413, 182, 193, 64, 407, 211, 11, 65, 106, 456, 479, 185, 14, 151, 108, 414, 284, 143, 217, 437, 477, 139, 51, 474, 324, 111, 433, 225, 335, 261, 19, 325, 226, 81, 219, 267, 340, 442, 225, 7, 198, 239, 129, 354, 238, 134, 255}; import java.util.Arrays; public class Swap { public static int[] swap(int[] numbers) { // In the coming weeks, the lecturer will explain all of the code above. But for now here is an explanation: // 1. In the code above we have provided an integer array "numbers", containing two elements. // In the space provided below, write code to swap the values in "numbers[0]" and "numbers[1]". // Do NOT initialise "numbers[0]" or "numbers[1]". Just write the three lines that swap the values. // Add your code BELOW this line. Do NOT change anything ABOVE this comment line. // Add your code ABOVE this line. Do NOT change anything BELOW this comment line. return numbers; // Do NOT chage this line of code } // do NOT change anything below this line public static void main(String[] args) { int[] numbers = {1, 2}; System.out.println("initially the array was: " + Arrays.toString(numbers)); System.out.println("After calling Swap method, the array is: " +Arrays.toString(swap(numbers))); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!