Array Basics: Exercise 1
Array Basics: Exercise 1
Array Basics: Exercise 1
Array Basics
Content
Uses of pre-built data structures
An array is a data structure that is available for a programmer to use in Java. You will find that there are
situations where arrays will work perfectly for your program and they will facilitate finding a solution. Other
times an array might not be the best data structure to use and you might have to use something different.
As you complete this activity, think carefully about how arrays could be used in your future programs.
For each of the following questions, write the appropriate code in your notebook (you will need to have
this exercise in your notebook to receive full credit during notebook checks). Using your array whiteboard
and markers, construct a model of the object constructed by Java.
To get full credit on this assignment, you must record your answers in your notebook and construct
models of the arrays using the whiteboard or papers given to you. Your instructor will be checking to
make sure you are able to model array construction using the modeling materials provided to you.
Exercise 1 (Example)
Construct an array that stores 8 elements of type int. Don’t forget to show how Java auto-initializes your
elements!
array[0] = 8;
array[1] = 7;
array[2] = -3;
Problem 2c.
Generate code that constructs an array with 5 elements, and fills the array with 5 consecutive integers.
int array3 = new int[5];
Problem 2d.
Construct a loop that will grab input from the user and traverse the array to fill it in.
scanner input = new Scanner(System.in);
int[] array4 = new int[10];
Problem 2e.
Using only one statement, declare an array of six integer values: -4, 19, 3, 20, 72, 58.
Problem 3a.
Using array.length, write code that will output the last value of the array you constructed in Problem 2d.
Hint: Don’t forget to use 0-based indexing!
System.out.printIn(array4[array4.length-1]);
Problem 3b.
Generate code to output the middle value of the array you wrote in Problem 2d.
Problem 3c.
Using the array you wrote in Problem 2d, write code that decrements each element by 1.
for(int i=0; i<array4.length; i++){
Problem 3d.
Write code that creates an array named data, with the following contents:
data
1 4 9 16 25
0 1 2 3 4
array[0] = 1;
array[1] = 2;
array[2] = 9;
array[3] = 16;
array[4] = 25;
Problem 3e.
Construct an array called even that stores the first 499 even integers starting at 0
int array0 = new int[499];
Problem 3f.
Write code that will output the value at index 100 in array even.
Problem 3g.
Write code that accesses array even and outputs the value at indexes 278 and 456.
int array2 = new int[278];
for(int i=0; i<278; i++){
array2[i] = i;
}
int array4 = new int[456];
Problem 3h.
Write a method that stores all odd numbers between -4 and 5 into an array using a loop. Make the
array’s size exactly large enough to store the numbers (including -4 and 5).
scanner input = new Scanner(System.in);
int[] array4 = new int[5];
Problem 3i.
Try generalizing your code from Problem 3h so that it will work for any minimum and maximum values,
not just -4 and 5. To test your generalized code, use the modeling supplies to test out your code using
different values.