Starting out with java Ch. 7

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

arrays

(blank) allows us to create a collection of like values that are indexed.

primitive variables

(blank) are designed to hold only one value at a time.

size declarator

This specifies the number of elements in the array.

True

When an array is passed to a method, the method has access to the original array.

columns

You can create a ragged array by creating a two-dimensional array with a specific number of rows, but no...

search algorithm

A (blank) is a method of locating a specific item in a larger collection of data.

command line

A Java program can receive arguments from the operating system (blank)

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

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.

elements

An array is a list of data (blank)

1. the reference name 2. the subscript that identifies which element in the array to access. ex numbers[0]=20 is pronounced "numbers sub zero"

An array is accessed by what? Two things..

True

An array of String objects may be created, but if the array is uninitialized, each String in the array must be created individually.

True

Arrays are not limited to primitive data. An array of String objects can be created: String[] names = { "Bill", "Susan", "Steven", "Jean" };

subscript

Each element of an array is accessed by a number known as a (blank)

Initializing a two dimensional array

Initializing a two-dimensional array requires enclosing each row?s initialization list in its own set of braces. int[][] numbers = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; Java automatically creates the array and fills its elements with the initialization values. row 0 {1, 2, 3} row 1 {4, 5, 6} row 2 {7, 8, 9} Declares an array with three rows and three columns.

True

It is not required that the name of mains parameter array be args.

True

Java does not allow a statement to use a subscript that is outside the range of valid subscripts for an array.

true

T/F:An array can store any type of data but only one type of data at a time.

false, they are initialized to 0

T/F:Array element values are always initialized to 1.

true

T/F:Array indexes always start at 0.

False (You need to copy the individual elements of one array to another)

T/F:You can copy an array by merely assigning one reference variable to another.

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

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.

to print out the scores array

for (int row = 0; row < 3; row++) { for (int col = 0; col < 4; col++) { System.out.println(scores[row][col]); } }

Summing The Rows of a Two- Dimensional Array

int[][] numbers = {{ 1, 2, 3, 4}, int total; for (int row = 0; row < numbers.length; row++) { total = 0; for (int col = 0; col < numbers[row].length; col++) System.out.println("Total of row " }

Summing The Columns of a Two- Dimensional Array

int[][] numbers = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; int total; for (int col = 0; col < numbers[0].length; col++) { total = 0; for (int row = 0; row < numbers.length; row++) total += numbers[row][col]; System.out.println("Total of column " + col + " is " + total); }

The return type of the method must be declared as an array of the right type.

public static double[] getArray() { double[] array = { 1.2, 2.3, 4.5, 6.7, 8.9 }; return array; } The return type must be what?

myMethod(numbers);

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.

True

A two-dimensional array has multiple length fields.

garbage collected

An array reference can be assigned to another array of the same type. If the first array no longer has a reference to it, it will be what?

False

An array's size declarator can be a negative integer expression.

By providing an initialization list. The array is sized to hold the number of values in the list.

How do you define an array without providing a size declarator?

size declarator

In an array declaration, this indicates the number of elements that the array will have.

True

To access the length fields of the array: int[][] numbers = { { 1, 2, 3, 4 }, { 5, 6, 7 }, { 9, 10, 11, 12 } }; for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) System.out.println(numbers[row][col]);

the length field

Two-dimensional arrays are arrays of one- dimensional arrays. The length field of the array gives the number of rows in the array. Each row has a length constant tells how many columns is in that row. Each row can have a different number of columns.

True

Typically, if the amount of data that an array must hold is unknown: size the array to the largest expected number of elements. use a counting variable to keep track of how much valid data is in the array.

When the statement executes, it crashes the program and displays a runtime error message.

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

Array size declarator specifies the number values an array can hold. Subscript identifies a specific element in an array.

What is the difference between a size declarator and a subscript?

2 14 8

What is the output of the code? int[] values = {4,7,6,8,2}; System.out.println(values[4]); x = values[2] + values[3]; System.out.println(x); x = ++values[1]; System.out.println(x);

Selection sort

What is this algorithm? The smallest value in the array is located and moved to element 0. Then the next smallest value is located and moved to element 1. This process continues until all of the elements have been placed in their proper order.

Reading the contents of a file into an array

final int SIZE = 5; // Assuming we know the size. int[] numbers = new int[SIZE]; int i = 0; File file = new File ("Values.txt"); Scanner inputFile = new Scanner(file); while (inputFile.hasNext() && i < numbers.length) { numbers[i] = inputFile.nextInt(); i++; } inputFile.close();

It will have to read all 10,000 elements to find the value stored in the last element.

if a sequential search method is searching for a value that is stored in the last element of a 10,000 element array, how many elements will the search code have to read to locate the value?

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

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.

result = numbers1[0] * numbers2[3]; result = 1 * 8 = 8

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

Incorrect way. The == operator determines only whether array references point to the same array object.

int[] firstArray = { 5, 10, 15, 20, 25 }; int[] secondArray = { 5, 10, 15, 20, 25 }; if (firstArray == secondArray) System.out.println("The arrays are the same."); else System.out.println("The arrays are not the same."); Is this correct way of comparing arrays?

Saving the contents of an array to a file

int[] numbers = {10, 20, 30, 40, 50}; PrintWriter outputFile = new PrintWriter ("Values.txt"); for (int i = 0; i < numbers.length; i++) outputFile.println(numbers[i]); outputFile.close();

10 elements 0 9

int[] values = new int[10]; How many elements does the array have? What is the subscript of the first element in the array? What is the subscript of the last element in the array?

Summing The Elements of a Two- Dimensional Array

int[][] numbers = { { 1, 2, 3, 4 }, {5, 6, 7, 8}, {9, 10, 11, 12} }; int total; total = 0; for (int row = 0; row < numbers.length; row++) { for (int col = 0; col < numbers[row].length; col++) total += numbers[row][col]; } System.out.println("The total is " + total);

the program runs

Array bounds checking happens when...

length() You do write the parentheses after the name of the String class?s length method.

String objects have a method named what?

False

The first size declarator in the declaration of a two-dimensional array represents the number of columns. The second size declarator represents the number of rows.

0

The first subscript in an array is always (blank)

1 less than the number of elements

The last subscript in an array is always what?

True

The length field of the 2d array gives the number of rows in the array.

String array, args

The main method receives a (blank) as a parameter. The array that is passed into the (blank) parameter comes from the operating system command-line.

out of bounds

The subscript is outside the range of valid subscripts for the array.

True

The subscript of the last element in a single-dimensional array is one less than the total number of elements in the array.

8 rows 10 columns 80 elements sales[7][9]= 100;

double[] [] sales = new double[8][10]; How many rows does the array have? How many columns does the array have? How many elements does the array have? Write a statement that stores a number in the last column of the last row in the array.

length(You do not write a set of parentheses after its name)

Arrays have a final field named what?

The statement makes array1 reference the same array that array2 references. Both variables will reference the same array. To copy the contents of array1 to array2, the contents of array2's individual elements will have to be assigned to the elements of array 1.

Assuming that array1 and array2 are both array reference variable, why is it not possible to assign the contents of the array referenced by array2 to the array referenced by array1 with the following statement? array1 = array2;

True

Both of the following declarations are legal and equivalent: int[] numbers; int numbers[];

Accessing Two dimensional Array Elements

Programs that process two-dimensional arrays can do so with nested loops. To fill the scores array: Number of rows, not the largest subscript for (int row = 0; row < 3; row++) { for (int col = 0; col < 4; col++) { Number of columns, not the largest subscript System.out.print("Enter a score: "); scores[row][col] = keyboard.nextDouble(); } } keyboard references a Scanner object

True

String objects have several methods, including: toUpperCase compareTo equals charAt Each element of a String array is a String object. Methods can be used by using the array name and index as before. System.out.println(names[0].toUpperCase()); char letter = names[3].charAt(0);

false(they are simply accessed by the same name and a subscript)

T/F:Array elements cannot be treated as any other variable.

True

T/F:Array indexes always start at 0 and continue to (array.length - 1)

true

T/F:Once an array is created, an array size is fixed and cannot be changed.

False

T/F:There is a difference between passing a single or two-dimensional array as an argument to a method.

True

T/F:When a single element of an array is passed to a method it is handled like any other variable.

true

T/F:When creating arrays, the array size must be a non negative integer and it may be a literal value, a constant, or variable.

True, as the compiler does not display error messages instead the statement executes the program throws an exception

The Java compiler does not display an error message when it processes a statement that uses an invalid subscript.

True

The values in an initialization list are stored in the array in the order that they appear in the list.

sequential search algorithm

This (blank) uses a loop to: -sequentially step through an array, -compare each element with the search value, and -stop when the value is found or the end of the array is encountered.

length

This array field holds the number of elements that the array has.

subscript

This identifies a specific element in the array.

N/2

This is the typical number of comparisons performed by the sequential search on an array of N elements(assuming the search values are consistently found)

binary search

This search algorithm repeatedly divides the portion of an array being

sequential search

This search algorithm steps through an array, comparing each item with the search value.

Binary Search

What is this algorithm? requires an array sorted in ascending order. starts with the element in the middle of the array. If that element is the desired value, the search is over. Otherwise, the value in the middle element is either greater or less than the desired value If it is greater than the desired value, search in the first half of the array. Otherwise, search the last half of the array. Repeat as needed while adjusting start and end points of the search.

0 through 3 0 1 2 3(four elements)

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

An array's size declarator must be a non-negative integer expression. The first statement is incorrect because the size declarator is negative. The second statement is incorrect because the size declarator is a floating-point number.

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

braces

When initializing a two-dimensional array , you enclose each row's initialization list in...

True

When processing the data in a two- dimensional array, each element has two subscripts: one for its row and another for its column.

ragged array

When the rows of a two-dimensional array are of different lengths, the array is known as a...

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

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

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

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?

int[ ] employeeNumbers = new int[ 100 ];

Write statements that create the following array: A 100-element int array referenced by the variable employeeNumbers.

float[ ] miles = new float[ 14 ];

Write statements that create the following array: A 14-element float array referenced by the variable miles.


Kaugnay na mga set ng pag-aaral

Origin, insertion, action, innervation of the upper extremity

View Set

data analysis and statistics chapter 9

View Set

Naysoli Mejia , European Explores

View Set

Holt Science and Technology: Inside the Restless Earth, Chapter 5, Sections 1 and 2

View Set