Level 67 Level 69
Level 68

[Quiz] Arrays II


132 words 0 ignored

Ready to learn       Ready to review

Ignore words

Check the boxes below to ignore/unignore words, then click save at the bottom. Ignored words will never appear in any learning session.

All None

Ignore?
ellipsis (...)
the _______ that follows the data type indicates a special type of parameter known as a *vararg parameter*
arrays (524)
vararg parameters are actually _______
ArrayList (526)
_______ is a class in the Java API similar to an array and allows you to *store & retrieve objects*
object's size (526)
Unlike an array, an *ArrayList* _______ is automatically adjusted to accomodate the number of items being stored in it
ArrayList object automatically expands/shrinks as items are added/removed (526)
An *ArrayList* is similar to an array of objects, but offers many advantages over an array including:
java.util package
ArrayList class is in the _______ package
ArrayList<String> nameList =
create an *ArrayList object* and store its address [code]
Add
ArrayList object methods:
bounds error
index values of an array range from 0 to length -1. accessing a nonexistent element results in a _______
a[i]
you can access array elements with an integer index, using the notation _______
generic class
class parameterized by type of data it holds
wrapper classes
wrapper classes allow for primitive data types to be inserted into a collection (like an arraylist). They are useful whenever it would be convenient to treat a piece of primitive data as if it were an object.
tabular two demensional arangement
two dimensional arrays form this
clone method
copy the elements of an array
int[] a;
What is the syntax for declaring variable containing an array of integers
What is the syntax to CREATE a new array?
int[] a = new int[10]; //creates & initializes array a with 10 integers
0
What is the initial index of an array?
with zeros
How is an array of numbers initialized
with false
How is an array of Boolean initialized
with null
How is an array of objects initialized
no
Is a compiler needed to run executable program?
ArrayList
a class that supports dynamic arrays that can grow as needed. That is, an ArrayList can dynamically increase or decrease in size.
arrayname.length
how do you determine the length of an array
enhanced for
what's the easiest way to loop through an array or any other list?
for (variable : collection) statement
what's the syntax of an enhanced for loop?
enhanced for traverses the elements not the index values
how does the enhanced for differ from a traditional for loop in array processing
System.out.println(Arrays.toString(arrayName));
how do you print all the elements of an array?
int[] a = {1,3,4,5,6,7};
how do you create an int array named a and initialize the variables?
it changes the value of the other. they both refer to the same array.
if you copy 1 array into another what happens if you change the values of one
int[] arrayName = Arrays.copyOf(originalName, originalName.length);
what is the syntax if you actually do want a COPY of an array?
int[] arrayName = Arrays.copyOf(originalName, 2*originalName.length); (example)
When creating a copy using Arrays.copyOf, how do you increase the size of the array?
java.util.Arrays;
What package is Arrays in?
API
Application Programming Interface. The specification of how a programmer writing an application accesses the behavior and state of classes and objects.
Appliances
Networked devices such as printers, Java technology-enabled terminals, and clients, that are managed using applications built using the Java Management API (JMAPI).
concat() method
The concat() method will create and return a new String object, which is the concatenation of the current String object and the String object given as an argument. The concat() method returns a reference v…
ensureCapacity() method
The ensureCapacity() method is used to preallocate room for a certain number of characters after a StringBuffer has been constructed.
identifier
A name for a variable or constant. An identifier must begin with a letter and can include any number of letters, numbers, and some special characters.
sort()
The sort method of the Collections class sorts the elements of an ArrayList in ascending order, according to the natural ordering of its elements.
ArrayList<Integer> numbers = new ArrayList<Integer>();
How do you create a new array list named numbers for an unknown amount of numbers?
Give one reason you may use an array list over an integer array.
You need to manipulate the length of the Array after you create it.
for (int i = 1; i <= 20; i++)
How could you create an array list of the first 20 numbers? (You don't need to define the array list)
numbers.set(2, 20);
Show how to replace the 3rd number in an array list with the number 20
numbers.isEmpty();
Say you were given an array list full of an unknown amount of numbers. How would you find out if the array had numbers in it?
System.out.print(numbers.get(4));
How would you print out the 5th element in the array?
numbers.contains(2);
Given an array list of numbers, how would you know if the array list had a 2 in it?
numbers.indexOf(1);
Say you were given an array list of the first 10 numbers in a random order. How would you find the position of the first number? (1, not the first number in the array list).
for (int i; numbers)
Write a for loop to change a given array list of the first 20 numbers to the second 20 numbers
numbers.clear();
I don't like it. Get rid of all the numbers.
numbers.indexof(6);
What's the position of 6? Give the command and the answer.
numbers.remove(0);
There are too many 1's. Get rid of 2 of them.
numbers.set(5, 3);
Where are the 3's. Replace a number with a 3.
letters.size();
How many letters are in it? Give the command and the answer.
letters.contains("G");
Don't look at Array List letters if you've seen it. Does it contain a G? Answer not necessary, just the command.
System.out.print(letters.get(13));
Print out the last element in letters.
letters.add("L");
There aren't enough letters here. Add some on.
letters.get(4);
What's the 5th element in the array? Replace it with the 13th element in the array. Give commands and answers.
letters.add("2");
Array List letters = {"A", "A", "B", "C", "E", "E", "F", "F", "H", "H", "H", "I", "J", "K"}. I don't like letters. Add some numbers to this array.
char streetAddress[]= new char[80];
Write a statement that declares an array named streetAddress that contains exactly eighty elements of type char.
int scores[] = new int[25];
Declare and instantiate an array named scores of twenty-five elements of type int .
int a[] = {10, 20, 30,40,50,60,70,80,90,100};
Declare an array named a of ten elements of type int and initialize the elements (starting with the first) to the values 10 , 20 , ..., 100 respectively.
String week[] = {"mon", "tue", "wed", "thu", "fri", "sat", "sun"};
Declare an array reference variable, week, and initialize it to an array containing the strings "mon", "tue", "wed", "thu", "fri", "sat", "sun" (in that order).
a[0]=3;
Given that an array named a with elements of type int has been declared, assign 3 to its first element
a[0] +=10;
Write a single statement that adds 10 to the value stored in the first element of the array.
a[a.length -1] =-1;
to the last element in a .
a[k]=15;
Assume that an array of int s named a has been declared with 12 elements. The integer variable k holds a value between 0 and 6 . Assign 15 to the array element whose index is k .
a[k+1]=9;
An array of int s named a has been declared with 12 elements. The integer variable k holds a value between 0 and 6 . Assign 9 to the element just after a[k] .
a[k-1]=22;
An array of int s named a has been declared with 12 elements. The integer variable k holds a value between 2 and 8 . Assign 22 to the element just before a[k] .
x = arr[arr.length - 2 ];
Assume that the array arr has been declared. Write a statement that assigns the next to last element of the array to the variable x , which has already been declared.
total =0;
Given an array of ints named x and an int variable named total that has already been declared, write some code that places the sum of all the elements of the array x into to…
a loop
is a body of statements that is executed repeatedly for some number of iterations
The for loop
takes the form:
The do/while loop
this loop has a nice feature but annoying syntax:
Using a loop with an array
Here is an example with a for loop
ArrayList Object and Loop
works like an array but has several advantages:
import java.util.*;
To use ArrayList objects, you must use this statement:
ArrayList myList = new ArrayList();
You can create an ArrayList like this:
myList.add(someObject);
You can put objects on an ArrayList like this:
True.
In Java, Exceptions are objects. True or false?
What are the three steps in the creation of an array?
D.A.I. declaration of an array, allocation of an array, and initialization of array elements.
What are the parts of an array declaration?
An array declaration is composed of an array type, a variable name, and one or more occurrences of [].
True or False: Square brackets can not follow the variable type in a declaration.
False. The square brackets can follow either the variable type or the name in the declaration.
False. The square brackets can be distributed across both the type and the name, e.g., int [] foo [];
True or False: Square brackets for multidimensional arrays must be contiguous, that is, all located after the type or the variable name.
True or False: it's valid to define the size of an array with its declaration.
False. An array declaration creates a variable that refers to null. The size can only be allocated when the variable is declared with a "new" statement. And the size must be specified on the right-hand-…
new Type[intSize];
What is the format of an array allocation statement?
Objects: null,
What are the default value types stored in arrays?
True or False: Arrays have a length() method that returns the number of elements stored in the array.
False. Arrays have a length variable, not method, that specifies the number of elements stored in the array.
What is the System method that copies array values from one array into another?
void arraycopy(Object src, int srcPos, Object dest, int destPos, int length). The "length" parameter refers to the number of elements to be copied from the source to the destination.
In what Java SE package is the ArrayList class found?
Package java.util contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).
True. See the constructor "ArrayList(int initialCapacity)"
True or False: you may specify an initial size to create an ArrayList.
What is the behavior of the ArrayList method clone()?
It returns a shallow copy of the ArrayList instance. "Shallow copy" means that the method creates a new instance of the ArrayList to be cloned, but the ArrayList elements aren't copied.
True or False: ArrayList objects provide better performance than arrays.
False. The collections ArrayList implementation reduces development time, but does not guarantee better performance over arrays.
True or False: ArrayList objects require less low-level implementation than arrays.
True. The java.util package provides an ArrayList implementation that automatically resizes the list to a larger capacity, if needed when adding an element. With arrays, one would have to develop code to create a…
To get a human-readable toString(), you must use Arrays.toString(), like this:
How to output a nice human readable list of elements for an array?
Element-by-element comparison: This will compare arrays properly, if
What are some different ways to code an equality comparison between two arrays?
Simple element-by-element copy: Tedious but can implement either a
What are some different ways to code the copying of an array?
java.util
What package does the Arrays class belong to?
myArrayList.indexOf(NOT_PRESENT_ELEMENT);
-1
True of False: the output of the following ArrayList call myArrayList.toString() will be an object reference like "[@13456"
False. It will be a human readable, square bracket delimited list of the toString() calls of the individual elements, like "[1,2,3]"
True of False: the ArrayList class has a method length()
False. It has a size() method that returns the number of elements in the list.
list.remove(1);
It compiles, but throws IndexOutOfBoundsException since adding at position 1 will fail, only index 0 is available. There is a method remove(int) to remove the element at index int.
String ejg2[][] = new String[][] { {}, {} };
No, not the first and second lines. The first line needs to have individual elements enclosed with curly-braces. The second line is missing a pair of "[]" in the RHS.
False. Only Objects.
True or False: an ArrayList will accept primitives as elements.
System.out.println(multiStrArr[2].toString()); // line5
line 1: A
How do you access array elements?
with an integer index using the notation a[ i ]
When an array is created, all values are initialized with
0 (for an array of numbers such as int[ ] or double[ ]), false (for a bolean[ ] array), or null (for an array of object references)
Declaration of an array variable
double[ ] data= new double[10];
0 to length-1
Index values of an array range from
Array length is
fixed (if you decide you need to add additional elements, then you need to make a new array)
int[ ] primes = {2, 3, 5, 7, 11};
If you know all the elements that you want to place in the array, you can create it by
ArrayList class
Manages a sequence of objects
Benefits of Array lists
array lists can grow and shrink as needed, the ArrayList class supplies methods for many common tasks such as inserting and removing elements
The ArrayList class is a _______ class
generic; ArrayList<T> collects objects of type T
Example of array list
ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
You use this to add an object to the end of an array list
add method; The size of the array increases after each call to the add method. The size method yields the current size of the ArrayList.
get method;
To get objects out of the array list, use the
set method;
To set an array list element to a new value, use the
accounts.add(i, this)
You can also insert an object in the middle of an array list, do
remove method;
Removes an element from an array list and reduces the size of the array list by 1
a.length
Use this to find the length of an array
a.size()
Use this to find the size of an Array list
a.length()
Use this to find the length of a String
wrapper class
To treat primitive type values as objects, you use a
auto-boxing
Conversion between primitive types and the corresponding wrapper class
ArrayList<Double>
You can collect a sequence of floating-point numbers in
Traverses all elements of a collection
the enhanced for loop; (double e : data) read as "for each e in data"
for each loop
Used to execute a loop for each element in the collection. In each iteration the variable is assigned the next element of the collection. Then the statement is executed.
double data[ ] = ...;
Traversing an array with a regular for loop
alter the contents of the array
when an array is passed as a parameter, it is possible to
two-dimensional arrays
form a tabular, two dimensional arrangement with an index pair a[i][j]
a second reference to the same array
An array variable stores a reference to the array. Copying the variable yields
System.arraycopy method
use this method to copy array elements from one array to another
how to grow an array that has run out of space
create a new larger array, copy all elements into the array, store the reference to the new array in the array variable.