Array
Objective
In this Assignment, we test your knowledge of using arrays in Java. In this assignment, you will create a Java program. Requirements are given below:
Introduction
An array is a simple data structure used to store a collection of data in a contiguous block of memory. Each element in the collection is accessed using an index, and the elements are easy to find because they are stored sequentially in memory. Because the collection of elements in an array is stored as a big block of data, we typically use arrays when we know exactly how many pieces of data we are going to have.
Tasks:
Write a program that creates an array of integers with a length of 3. Assign the values 1, 2, and 3 to the indexes. Print out each array element.
Write a program that returns the middle element in an array. Give the following values to the integer array: {13, 5, 7, 68, 2} and produce the following output: 7
Write a program that creates an array of String type and initializes it with the strings “red,” “green,” “blue,” and “yellow.” Print out the array length. Make a copy using the clone( ) method. Use the Arrays.toString( ) method on the new array to verify that the original array was copied.
Write a program that creates an integer array with 5 elements (i.e., numbers). The numbers can be any integers. Print out the value at the first index and the last index using length - 1 as the index. Now try printing the value at index = length (e.g., myArray[myArray.length] ). Notice the type of exception which is produced. Now try to assign a value to the array index 5. You should get the same type of exception.
Write a program where you create an integer array with a length of 5. Loop through the array and assign the value of the loop control variable (e.g., i) to the corresponding index in the array.
Write a program where you create an integer array of 5 numbers. Loop through the array and assign the value of the loop control variable multiplied by 2 to the corresponding index in the array.
Write a program where you create an array of 5 elements. Loop through the array and print the value of each element, except for the middle (index 2) element.
Write a program that creates a String array of 5 elements and swaps the first element with the middle element without creating a new array.
Write a program to sort the following int array in ascending order: {4, 2, 9, 13, 1, 0}. Print the array in ascending order, and print the smallest and the largest element of the array. The output will look like the following:
Create an array that includes an integer, 3 strings, and 1 double. Print the array.
Write some Java code that asks the user how many favorite things they have. Based on their answer, you should create a String array of the correct size. Then ask the user to enter the things and store them in the array you created. Finally, print out the contents of the array.
Example:
Last updated