Unit 7: ArrayList Flashcards
What differentiates an ArrayList from an Array?
An ArrayList’s size can change.
Why would you choose an Array over an ArrayList?
An ArrayList takes more memory. Unless you need to change the size of a list, an Array will run smoother and more efficiently.
Consider writing a program that reads the lines of any text file into a sequential
list of lines. Which of the following is a good reason to implement the list with
an ArrayList of String objects rather than an array of String objects?
(A) The get and set methods of ArrayList are more convenient than the []
notation for arrays.
(B) The size method of ArrayList provides instant access to the length of the
list.
(C) An ArrayList can contain objects of any type, which leads to greater generality.
(D) If any particular text file is unexpectedly long, the ArrayList will automatically be resized. The array, by contrast, may go out of bounds.
(E) The String methods are easier to use with an ArrayList than with an array
(D) If any particular text file is unexpectedly long, the ArrayList will automatically be resized. The array, by contrast, may go out of bounds.
Consider writing a program that produces statistics for long lists of numerical
data. Which of the following is the best reason to implement each list with an array of int (or double), rather than an ArrayList of Integer (or Double) objects?
(A) An array of primitive number types is more efficient to manipulate than an
ArrayList of wrapper objects that contain numbers.
(B) Insertion of new elements into a list is easier to code for an array than for
an ArrayList.
(C) Removal of elements from a list is easier to code for an array than for an
ArrayList.
(D) Accessing individual elements in the middle of a list is easier for an array
than for an ArrayList.
(E) Accessing all the elements is more efficient in an array than in an ArrayList.
(A) An array of primitive number types is more efficient to manipulate than an
ArrayList of wrapper objects that contain numbers.
Which declaration will cause an error? I List stringList = new ArrayList(); II List intList = new ArrayList(); III ArrayList compList = new ArrayList(); (A) I only (B) II only (C) III only (D) I and III only (E) II and III only
(B) II only
Consider these declarations: List strList = new ArrayList(); String ch = " "; Integer intOb = new Integer(5); Which statement will cause an error? (A) strList.add(ch); (B) strList.add(new String("handy andy")); (C) strList.add(intOb.toString()); (D) strList.add(ch + 8); (E) strList.add(intOb + 8);
(E) strList.add(intOb + 8);
Let list be an ArrayList containing these elements:
2 5 7 6 0 1
Which of the following statements would not cause an error to occur? Assume
that each statement applies to the given list, independent of the other statements.
(A) Object ob = list.get(6);
(B) Integer intOb = list.add(3.4);
(C) list.add(6, 9);
(D) Object x = list.remove(6);
(E) Object y = list.set(6, 8);
Consider the following code segment, applied to list, an ArrayList of Integer values. int len = list.size(); for (int i = 0; i < len; i++) { list.add(i + 1, new Integer(i)); Object x = list.set(i, new Integer(i + 2)); } If list is initially 6 1 8, what will it be following execution of the code segment? (A) 2 3 4 2 1 8 (B) 2 3 4 6 2 2 0 1 8 (C) 2 3 4 0 1 2 (D) 2 3 4 6 1 8 (E) 2 3 3 2
(A) 2 3 4 2 1 8
Consider the following method that will alter the matrix mat: /** @param mat the initialized matrix * @param row the row number */ public static void matStuff(int[][] mat, int row) { int numCols = mat[0].length; for (int col = 0; col < numCols; col++) mat[row][col] = row; } Suppose mat is originally 1 4 9 0 2 7 8 6 5 1 4 3 After the method call matStuff(mat,2), matrix mat will be (A) 1 4 9 0 2 7 8 6 2 2 2 2 (B) 1 4 9 0 2 2 2 2 5 1 4 3 (C) 2 2 2 2 2 2 2 2 2 2 2 2 (D) 1 4 2 0 2 7 2 6 5 1 2 3 (E) 1 2 9 0 2 2 8 6 5 2 4 3
(A) 1 4 9 0
2 7 8 6
2 2 2 2
Which options would not cause an error? considering there is a valid arrayList called val declared. a.) System.out.println(val.getLength()); b.) System.out.println(val.size()); c.) System.out.println(val.size); d.) System.out.println(val.getLength)
C would not call an error because “.size” is not a method; therefore, getting the length of the arrayList does not need “()” after calling “.size”
-v
how would one add object “op” to index 1 in a array? Considering ArrayList pope is declared
a. ) pope.add(1, op);
b. ) pope.add(op, 1);
c. ) pope[1].add(op);
d. ) pope.get(1).add(op);
A!! the way its formatted is that ArrayListName.add(index, object);
:))
-v
considering ArrayList cats is declared and initialized : (2, 3, 6, 7, 8) for (int i = 0; i < cats.size; i++){ System.out.println(cats.get(i)); } what will print out? a.) 2 3 6 7 8 b.) 2 3 6 7 8 c.) 23678 d.) nothing will compile, it will throw an error
THE ANSWER IS…. D, silly! the for loop has to be (int i=0; i < cats.size(); i++), size containing parenthesis , cats.size()
;))