Arrays Test
暂无描述。系统推荐的高质量记忆内容,适合每天坚持背诵学习。
卡片预览 (24 张)
What is the differenc between a primitive varaible and an array?
• The primitive variables you have worked with so far are designed to hold one value at a time. • Each of the variable declaration causes only enough memory to be reserved to hold one value of the specified data type. • An array, however, is an object that can store a group of values, all of the same type.
How do you create and use an array?
• Creating and using an array in Java is similar to creating and using any other type of object: You declare a reference variable and use the new key word to create an instance of the array in memory.
What does this statement do? int[] numbers;
• This statement declares numbers as an array reference variable. • The numbers variable can reference an array of int values. • Notice that this statement looks like a regular int variable declaration except for the set of brackets that appears after the key word int. • The brackets indicate that this variable is a reference to an int array. • Declaring an array reference variable does not create an array.
What is a size declarator?
• The number inside the brackets is the array’s size declarator. • It indicates the number of elements, or values, the array can hold. • When this statement is executed, numbers will reference an array that can hold six elements, each one an int. • An array’s size declarator must be a non-negative integer expression. It can be a literal value, as shown in the previous examples, or a variable
What does this statement do? int[] numbers = new int[6];
As with any other type of object, it is possible to declare a reference variable and create an instance of an array with one statement.
How do you use final when declaring an array?
It is a common practice to use a final variable as a size declarator. Here is an example: final int NUM_ELEMENTS = 6; int[] numbers = new int[NUM_ELEMENTS]; • This practice can make programs easier to maintain. • When we store the size of an array in a variable, we can use the variable instead of a literal number when we refer to the size of the array. • If we ever need to change the array s size, we need only to change the value of the variable. • The variable should be final so its contents cannot be changed during the program’s execution. NOTE : Once an array is created, its size cannot be changed.
What is a subscript?
• Although an array has only one name, the elements in the array may be accessed and used as individual variables. • This is possible because each element is assigned a number known as a subscript. • A subscript is used as an index to pinpoint a specific element within an array. • The first element is assigned the subscript 0, the second element is assigned 1, and so forth. • The six elements in the numbers array seen below has subscripts 0 through 5. • Subscript numbering always starts at zero. • The subscript of the last element in an array is one less than the total number of elements in the array.
How do you assign a value to an element?
numbers[0] = 20; numbers[3] = 30;
How is numbers[0] pronounced?
The expression numbers[0] is pronounced “numbers sub zero.” You read these assignment statements as “numbers sub zero is assigned twenty” and “numbers sub three is assigned thirty.”
What does Java do by default with arrays?
By default, Java initializes array elements with 0. In the diagram above, values have not been stored in elements 1, 2,4, and 5, therefore they are shown as 0s.
What is the difference between a size declarator and a subscript?
• When you use the new key word to create an array object, the number inside the brackets is the size declarator. It indicates the number of elements in the array. • The number inside the brackets in an assignment statement or any statement that works with the contents of an array is a subscript. It is used to access a specific element in the array.
What is bounds checking?
• Java performs array bounds checking, which means that it does not allow a statement to use a subscript that is outside the range of valid subscripts for an array. For example, the following statement creates an array with 10 elements. The valid subscripts for the array are 0 through 9. int[] values = new int[10]; • Java will not allow a statement to use a subscript that is less than 0 or greater than 9 with this array. • Bounds checking occurs at runtime. • The Java compiler does not display an error message when it processes a statement that uses an invalid subscript. • Instead, when the statement executes, the program throws an exception and immediately terminates.
What is a off-by-one error?
• Because array subscripts start at 0 rather than 1, you have to be careful not to perform an off-by-one error. For example, look at the following code: // This code has an off-by-one error. final int SIZE = 100; int[] numbers = new int[SIZE]; for (int index = 1; index <= SIZE; index++) numbers[index] = 0; • The intent of this code is to create an array of integers with 100 elements, and store the value 0 in each element. • However, this code has an off-by-one error. • The loop uses its control variable, index, as a subscript with the numbers array. • During the loop’s execution, the variable index takes on the values 1 through 100, when it should take on the values 0 through 99. • As a result, the first element, which is at subscript 0, is skipped. • In addition, the loop attempts to use 100 as a subscript during the last iteration. • Because 100 is an invalid subscript, the program will throw an exception and halt.
What is an array?
• An array is a collection of values all wrapped up and given a name. It is the idea of having multiple values all contained in one named variable. • First, all of the items in an array must be of the same type, so you can create an array of integers or create an array of strings, but you can’t create an array that contains both. • When you declare an array, it’s always of a fixed size. You set the size when you declare it and you can’t change the size at runtime.
What is the code for initializing values in the declaration statement?
• This line initializes the values in the declaration statement. • We start off with the data type, we place the brackets either after the data type or after the array name, and then put in a pair of curly braces with our list of items as a comma delimited list. • The number of items in the array and their values are determined by that list. As with the previous declarations, once the array is set to have a particular number of items it will always have that number of items, you can’t change it at runtime. int a3[] = {3, 6, 9};
What is the syntax with arrays?
• So when using simple arrays, remember that the syntax can vary a little bit. You can place the initial brackets either after the data type or after the array name and you can either use the new keyword and an explicit number of items in the array, or you can use a comma delimited list wrapped in braces. Either way the array must contain items all of the same data type and it is not resizable at runtime.
How do you assign a value read by the keyboard and assign it to a value in an array?
hours[0] = keyboard.nextInt(); // This line gets the hours worked by employee 1 and // stores it in “hours sub 0”
How can subscript numbers be stored?
Subscript numbers can be stored in variables. This makes it possible to use a loop to “cycle through” an entire array, performing the same operation on each element. For example, the code in the previous example (ArrayDemo1) could be simplified by using two “for” loops: one for inputting the values into the array and the other for displaying the contents of the array.
What does this code do? for (int index = 0; index < EMPLOYEES; index++) { System.out.print(“Employee “ + (index + 1) + “: “); hours[index] = keyboard.nextInt();
The variable index starts at 0. During the loop’s first iteration, the user’s input is stored in hours[0]. Then, index is incremented, so its value becomes 1. During the next iteration, the user’s input is stored in hours[1]. This continues until values have been stored in all of the elements of the array.
What is an alternate method for displaying the contents of an array?
for (int element : hours) // { // System.out.println(element); // }
How do you reassign an array reference varaible to a different array?
int[] numbers = new int[10]; // This line creates an array referenced by the “numbers” variable. numbers = new int[5];
How do two varaible reference the same array?
int[] array1 = { 2, 4, 6, 8, 10 }; // This line creates an array and assigns its address to the “array1” variable. int[] array2 = array1; This line assigns array1 to array2. This does not make a copy of the array referenced by array1. Rather, it makes a copy of the address that is stored in array1 and stores it in array2. After this statement executes, both the array1 and array2 variables will reference the same array. This type of assignment operation is called a “reference copy”. Only the address of the array object is copied, not the contents of the array object.
What is the distinction between an array and the variable that references it?
• Because an array is an object, there is a distinction between an array and the variable that references it. • The array and the reference variable are two separate entities. • This is important to remember when we want to copy the contents of one array to another. • As we have seen in the previous code, you cannot copy an array by merely assigning one array reference variable to another. Instead, to copy an array you need to copy the individual elements of one array to another.
How do you copy the contents of an array to another?
int[] firstArray = { 5, 10, 15, 20, 25 }; // This statement creates and initializes an array of five elements referenced by a variable named “firstArray” int[] secondArray = new int[5]; // This statement creates an array of five elements referenced by a variable named "secondArray" for (int index = 0; index < firstArray.length; index++) { secondArray[index] = firstArray[index]; // This loop copies all the contents of "firstArray" into "secondArray". }