Chapter 6 Array - Kennesaw State University

[Pages:12]Chapter 6 Array

1. How to declare and create an array? a. The declaration of an array variable does not allocate any space in memory for the array b. You cannot assign elements to an array unless it has already been created.

2. An array variable is not a primitive data type. 3. How to access elements of an array? 4. Index range? 5. Array initializer: {...} 6. Passing an array argument to a method

a. Pass by reference: the called method can modify the elements in the caller's original array

b. Pass by value?

Exercises:

1. What is the output of the following code? int x = 30; int[] numbers = new int[x]; x = 60; System.out.println("x is " + x); System.out.println("The size of numbers is " + numbers.length);

2. True or false a. Every element in an array has the same type b. The array size is fixed after it is declared c. The array size is fixed after it is created d. The elements in an array must be of primitive data type e. When an array is passed to a method, a new array is created and passed to the method.

3. Which of the following statements are valid array declarations? a. int i = new int(30); b. double d[] = new double[30]; c. char[] r = new char(1 .. 30); d. int i[] = (3, 4, 3, 2); e. float f[] = {2.3, 4.5, 6.6}; f. char[] c = new char();

4. Write statements to do the following a. Create an array to hold 10 double values b. Assign value 5.5 to the last element in the array c. Display the sum of the first two elements d. Write a loop that computes the sum of all elements in the array e. Write a loop that finds the minimum element in the array f. Randomly generate an index and display the element of this index in the array g. Use an array initializer to create an array with initial values 3.5, 5.5, 4.52, and 5.6

5. Fix the errors in the following code public class Test{ public static void main(String[] args) { double[100] r;

for (int i=0; i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download