Java Programming - Chapter 7: Arrays

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What are variable-length argument lists?

A mechanism that makes it possible to write a method that takes any number of arguments when it is called.

What is the sequential search algorithm?

A method that is used to locate a specific item in a larger collection of data. It uses a loop and goes through each element and compares it to the value that is being searched for.

What is the difference between an array's size declarator and a subscript?

A size declarator tells us the size of an array, the subscript allows us to locate a specific element in an array (as an index)

What is null in Java?

A special value in Java that indicates the array elements do not yet have reference objects.

What is a sorting algorithm?

A technique for stepping through an array and rearranging its contents in some order.

What is a two-dimensional array?

An array of an array

What is wrong with the following array declarations? int [ ] readings = new int[-1]; double [ ] measurements = new double [4.5];

The size declarator of an array must NOT be negative and it must not be a floating number, it must be a whole integer.

How do you create an array in Java?

There are two ways you can declare and initialize an array in Java. The first is with the new keyword, where you have to initialize the values one by one. The second is by putting the values in curly braces. dataType [] nameOfArray = new dataType [size] dataType [ ] nameOfArray = {value1, value2, value3, value4}

TRUE OR FALSE: Once an array size is created it cannot be changed.

True

How are OBJECTS in an array accessed?

With subscripts, just like any other data type.

Consider the following code: int [ ] firstArray = { 5, 10, 15, 20 }; int [ ] secondArray = new int[5]; for (int index = 0; index < firstArray.length; index++) { secondArray[index] = firstArray[index]; } Does the code properly copy the contents of an array into another array?

YES => The loop in this code copies each element of firstArray to the corresponding element of secondArray.

What is a ragged array?

a two-dimensional array where the rows have a different number of columns

If you need to access the values that are stored in an array, from the first element to the last element, is it better to use the enhanced for loop for the traditional for loop?

enhanced for loop

A video rental store keeps videos on 50 racks with 10 shelves each. Each shelf holds 25 videos. Declare a three-dimensional array large enough to represent the store's storage system.

final int RACKS = 50; final int SHELVES = 10; final int VIDEOS = 25; int[][][] videoNumbers = new int[RACKS][SHELVES][VIDEOS];

write a 14-element float array referenced by the variable miles

float [ ] mile = new float[14];

B. Write a loop that displays the contents of each element in the array you declared in A.

for (index = 0; index < planets.length; index++) { System.out.println(planet[index]); }

Look at the following statements: int[] a = { 1, 2, 3, 4, 5, 6, 7 }; int[] b = new int[7]; Write code that copies the a array to the b array.

for (int i = 0; i < a.length; i++) b[i] = a[i];

Write a loop that displays the first character of the strings stored in each element of the array you declared in A

for( index = 0; index < planets.length; index++) { System.out.print("The first letter of planet " + (index+1) + " is " + planet[index].charAt(0); }

The ArrayList class's ___ method returns the item stored at a specific index

get method => you pass the index as an argument to the method

What import statement must you include in your code in order to use the ArrayList class?

import java. util. ArrayList

Write a 100-element int array referenced by the variable employeeNumbers

int [ ] employeeNumbers = new int[100];

A program has the following declaration: double[ ] values; Write code that asks the user for the size of the array and then creates an array of the specified size, referenced by the values variable.

int size; System.out.print("Enter the size of the array: "); size = keyboard.nextInt(); values = new double[size];

What is capacity in an ArrayList?

it is the number of items it can store without having to increase its size

What does bounds checking mean?

it refers to when java does not allow a statement to use a subscript that is outside the range of valid subscript for an array

A two-dimensional array has multiple _____ fields

length

Look at the following method header: public static void myMethod(double[] array) Here is an array declaration: double[] numbers = new double[100]; Write a statement that passes the numbers array to the myMethod method.

myMethod(numbers);

Programs that process two-dimensional arrays can do so with ______ _____

nested loops

If you do not provide an initialization list for a string array, you must use the _________ keyword to create the array

new

When creating an initialization list for both one and two-dimensional arrays, you do not need to provide the ____ keyword

new

How would you correctly write a method header that returns an array of a double type? NOTE: The name of the method is called getArray

public static double [ ] getArray ( );

Write a method named zero, which accepts an int array as an argument and stores the value 0 in each element.

public static void Zero(int [ ] array) { for ( i = 0; i < array.length; i ++) { array[i] = 0; } }

How do you add items to an ArrayList object?

with add method

What is a class that Java provides that can be used for storing and retrieving objects?

ArrayList

How do you insert an item at a specific location in an ArrayList object?

ArrayList class has an overloaded version of the add method that allows you to add an item at a specific index.Ex.) List.add(1, "Mary");

Write a statement that creates an ArrayList object and assigns its address to a variable named frogs.

ArrayList frogs = new ArrayList();

Write a statement that creates an ArrayList object and assigns its address to a variable named lizards. The ArrayList should be able to store String objects only.

ArrayList<String>lizards = new ArrayList<String>();

When does bound checking occur in java

During runtime

You cannot removed items from an ArrayList (T/F)

FALSE -You can remove and add items to an ArrayList, it automatically shrinks when items are removed from it

It is required that the name of the main's parameter array be a r g s

FALSE => it can be anything you wish, however it is standard convention for the name args to be used.

An array and its reference variable are considered the same entity (T/F)

FALSE => They are two separate entities

You can compare two arrays with the == operator (T/F)

FALSE => You cannot compare the CONTENTS of arrays this way, however, the == compares the MEMORY ADDRESS of the arrays

When you create an uninitialized array of String objects, you do not need to assign a value to each element in the array (T/F)

FALSE => You must assign a value to each element in the array that you intend to use. In an uninitialized array, because they do not reference any objects, they are set to null.

When an entire array is passed into a method, the actual contents of the array is passed

FALSE => a reference to the array is passed, but not the actual array itself.

TRUE OR FALSE : Declaring an array reference variable creates an array

FALSE => declaring an array reference variable does not create an array

TRUE OR FALSE => int numbers [ ] is an incorrect array declaration notation

FALSE => this is an acceptable notation.

TRUE OR FALSE: It is not possible to declare a reference variable and create an instance of an array with one statement

FALSE => you can declare a reference variable and create an instance of an array with one statement. For example: int [ ] numbers = new int[6];

What does the diamond operator do?

It causes the compiler to infer the required data type from the reference variable declaration. example: ArrayList<InventoryItem> list = new ArrayList<>(); This creates an ArrayList that can hold InventoryItem objects.

Describe the enhanced for loop

It is designed to iterate once for every element in an array. Each time this loop iterates, it copies an array element to a variable

What does it mean for a subscript to be out-of-bounds?

It means that the subscript is outside of the array range

How do you remove an item from an ArrayList object?

List.remove(index);

Look at the following code: in [ ] array1 = {2, 4, 6, 8, 10}; int [ ] array2 = array1; Has array1 been copied into array 2?

NO! This is not how you properly copy the contents of an array into another array. This code makes a copy of the address stored in array1 and stores it in array2 (REFERENCE COPY). So, now array2 can now reference array1

To determine if two arrays are equal you must compare each of the elements of the two arrays. (T/F)

TRUE

When passing an array into a method as a n argument, you pass the value in the variable that references the array (T/F)

TRUE

You can create an ArrayList to hold any type of object (T/F)

TRUE

You can use the enhanced for loop on an ArrayList

TRUE

Methods can return arrays (T/F)

TRUE => A method can return a reference to an array

TRUE OR FALSE: An array's size declarator must be a non-negative integer

TRUE => it is also common practice to use a final variable as a size declarator. For example: final int NUM_ELEMENTS = 6; int [ ] numbers = new int[NUM_ELEMENTS];

TRUE OR FALSE: the subscript of the last element in an array is one less than the total numbers array

TRUE => subscript numbering always starts at 0

How do you retrieve a specific item from an ArrayList object?

The ArrayList class has a get method that retrieves an item at a specific index. We pass the index as an argument to the method. Ex.) List.get(3);

How do you determine an ArrayList object's size?

The ArrayList class has a size method that returns the number of items stored in the ArrayList.

Array

an object that can store a group of variables, all of the same data type

A program uses a variable named array that references an array of integers. You do not know the number of elements in the array. Write a for loop that stores −1 in each element of the array.

(int i = 0; i < array.length; i ++) array[i] = -1;

Describe the difference between the sequential search and the binary search

- sequential search starts with the first element and keeps going until it finds it or it reaches the end of the array - binary search starts with the element in the middle and keeps breaking the array in half unit it finds it

What value in an array does the selection sort algorithm look for first? When the selection sort finds this value, what does it do with it?

- the smallest value - moves it to element 0 and so on

The first element in an array is assigned the subscript __

0

What would the valid subscript values be in a four-element array of doubles?

0, 1, 2, 3

What is the output of the following code? int[] values = new int[5]; for (int count = 0; count < 5; count++) values[count] = count + 1; for (int count = 0; count < 5; count++) System.out.println(values[count]);

1 2 3 4 5

What's wrong with the following array declarations? int[] readings = new int[−1]; double[] measurements = new double[4.5];

1. The size declarator cannot be a negative number, the size declarator must be a positive expression 2. The size declarator cannot be a float number (such as 1.2, or 4.5, 5.5, 9.3, etc)

What are the two main steps in getting the average values in a numeric array?

1. getting the sum of all the values in the array 2. divide the sum by the number of elements in the array

When an ArrayList object is first created, using the no-arg constructor, it has an initial capacity of ___ items.

10

on average, with an array of 20,000 elements, how many comparisons will the sequential search perform? (assume the items being searched for are consistently found in the array)

10,000

Write code that declares a Rectangle array with five elements. Instantiate each element with a Rectangle object. Use the Rectangle constructor to initialize each object with values for the length and width fields.

Rectangle[] array = new Rectangle[5]; for (int i = 0; i < array.length; i++) { array[i] = new Rectangle(i, i + 1); }

What does the toString method do?

Returns a string representation of an object

What is the difference between an ArrayList object's size and its capacity?

Size=number of items stored in the Array List object. Capacity=number of items an ArrayList object can hold without having to increase its size.

A. Write a statement that declares a String array initialized with the following strings: "Mercury", "Venus", "Earth", and "Mars".

String[ ] planets = { "Mercury", "Venus", "Earth", "Mars" };

(T/F) You may create objects that are instances of classes that you have created.

TRUE

An ArrayList object automatically expands in size to accommodate the items stored in it. (T/F)

TRUE

Arrays can have mULTIPLE dimensions in Java (T/F)

TRUE

Declaring an array reference variable does not create an array (T/F)

TRUE

Vararg parameter

The ellipsis (three periods) that follows the data type indicates that the parameter is a special type of parameter. A vararg parameter can take a variable number of arguments. NOTE: vararg parameters are actually arrays

if a sequential search is performed on an array, and it is known that some items are searched for more frequently than others, how can the contents of the array be reordered to improve the average performance of the search?

The items frequently searched for can be stored near the beginning of the array.

What happens in Java when a program tries to use a subscript that is out-of-bounds?

The java program crashes, and then it displays a runtime error message

Write statements that create the following arrays: A 100-element int array referenced by the variable employeeNumbers. A 25-element double array referenced by the variable payRates. A 14-element float array referenced by the variable miles. A 1000-element char array referenced by the variable letters.

a. int[ ] employeeNumbers = new int[100]; b. double[ ] payRates = new double[25]; c. float [ ] miles = new float[14]; d. char[ ] letters = new char[1000];

To add items to the ArrayList object, you use the ___ method

add method examples: nameList.add("James"); nameList.add("Catherine"); nameList.add("Bill");

To declare a two-dimensional array in Java, you type two sets of ____ and two ____ __________ are required

brackets, size declarators

write a 1000-element char array referenced by the variable letters

char [ ] letters = new char[1000];

You can simplify the instantiation of an ArrayList by using the ________ _______ (<>)

diamond operator

write a 25-element double array referenced by the variable payRates

double [ ] payRates = new double[25];

Write a statement that creates and initializes a double array with the following values: 1.7, 6.4, 8.9, 3.1, and 9.2. How many elements are in the array?

double [ ] value = {1.7, 8.9, 3.1, 6.4, 9.2} There are 5 elements in the array

What is a subscript

used as an index to pinpoint a specific element within an array

Look at the following statements: int[] numbers1 = { 1, 3, 6, 9 }; int[] numbers2 = { 2, 4, 6, 8 }; int result; Write a statement that multiplies element 0 of the numbers1 array by element 3 of the numbers2 array and assigns the result to the result variable.

result = numbers1[0] * numbers2[3]; This would be multiplying 1 * 8

In a 2D array, each element has two subscripts: one for its ______ and one for its _____

row, column

The ArrayList class has a ____ method that reports the number of items stored in an ArrayList

size => This method returns the number of items as an int

The number inside the brackets of an array is called _________

size declarator => indicates the number of elements, or values that an array can hold

In an array, the elements within it can be accessed by its assigned number called ___________

subscript

How many times will the selection sort swap the smallest value in an array with another value?

until all the elements have been placed in their proper order


Kaugnay na mga set ng pag-aaral

Biology - Quiz 2: Properties of Compounds

View Set

Final Exam: World History Study Guide Part 1

View Set

Ch 28: The Normal Newborn: Needs and Care

View Set

Adding and Subtracting Polynomials 80%

View Set

Chapter Exam 4 part 2: Life Insurance- Provisions, Options and Riders

View Set

The Iroquois Creation Myth: “The World on Turtle’s Back”

View Set

n222 exam 3 coursepoint questions

View Set