Unit 7 - ArrayList Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

ArrayList object

A
Mutable and contains object references
Dynamic in size
Resizable length
Not designed to store primitives
Slightly slower
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

ArrayList can only be used when

A

imported

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

ArrayList class is implemented using

A

Arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

framework

A

prewritten, high-performing and efficient code that can handle and use objects of groups of data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

import statement

A

import.java.util.ArrayList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Declaring a variable to reference an ArrayList object

A

ArrayList variableName;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Instantiate an ArrayList object

A

Stores only elements of the same, non-primitive DataType

new ArrayList  ();
OR
new ArrayList  (n);
n= initial # of elements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

DataType must equal

A

any non-primitive data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Although ArrayList objects are designed to store references to objects, using

A

Wrapper class allows primitive values to be stored in ArrayList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What ArrayList method returns the number of element in the list?

A

int size ( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What ArrayList method appends obj to end of list and returns true?

A

boolean (E obj)

ducks. add(Duck1);
* adding Duck1 to the end of an array named ducks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What ArrayList method inserts obj at position index, moving elements at position index and higher to the right and adds 1 to the list size?

A

void add (int index, E obj)

symbols. add(3, flowers)
* adding “flowers” to index 3 of an array named symbols, moving all original items at 3 or above 1 index higher and adding 1 to ArrayList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What ArrayList method removes element from position index, moving elements at position index and higher to the left and subtracts 1 to the list size while returning the elements formerly stored at position index?

A

E remove (int index)

Star s1 = stars. remove(2);
*removing element at index 2 in an array named stars & storing it as s1

System.out.print(s1);
*former element will be printed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What ArrayList method replaces element at position index with obj and returns element formerly at position index?

A

E set (int index, E obj)

Stars s1 = stars.set(1, green star)
*setting element at index 1 in an array named stars & storing it as s1

System.out.print(s1);
*former element will be printed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What ArrayList method returns element at position index in list?

A

E get (int index)

Star s2 = stars.get(0);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

ArrayList is preferred over ArrayList because

A

It allows compiler to find errors that would otherwise be found at run-time

17
Q

Java allows the generic ArrayList when the generic type E

A

specifies the type of elements

18
Q

When ArrayList is specified,

A

types of references parameters and return type when using the methods are type E

19
Q

When passed as a parameter in a method, ArrayList are passed as

A

References to address and not copies of their values

20
Q

When a method updates elements of a passed ArrayList, the ArrayList’s elements are

A

Updated automatically

21
Q

If a method with an ArrayList as a parameter only involves accessing elements of ArrayList without assigning or updating the elements from the ArrayList, then

A

the myMethod method signature is sufficient

myMethod(questions);

connects to method used with array name inside parentheses

22
Q

If the method involves adding, updating, or storing elements of the passed ArrayList, then

A

you need to specify the data type of elements stored in ArrayList

myMethod1(questions);
*call to method

public static void myMethod1(ArrayList arr) {}
*method being called

23
Q

In order to return an ArrayList, it is preferred that you

A

specify the type of data of elements that the ArrayList stores

24
Q

Algorithm for summing all elements in an ArrayList

A

1 - create an int sum and set to 0
2 - length of element at index i will be added to sum
3 - loop will repeat until all elements has been traversed
4- display sum

25
Q

Algorithm for removing elements in an ArrayList

A

Needs to start on the right end of an ArrayList and move down in order to captures all specified element

26
Q

Structure of enhanced for loop for ArrayList

A
for (DataType item: nameOfArrayList)
{
      //statement one
      //statement two
      // ...
}
27
Q

In enhanced for loops with ArrayList,

A

No index is needed so no set() or get()

Enhanced for loops make a copy of each entry

28
Q

During the iterations of an enhanced for loop, ArrayList elements

A

Can’t be modified, remove from, or added to ArrayList

29
Q

Common Mistakes with ArrayList

A

1- Forgetting to include import statement
2- Declaring or instantiating an ArrayList with primitive data type
ArrayList myList = new ArrayList ();
3- Forgetting to include the () at end of the ArrayList constructor call
ArrayList myList = new ArrayList ;
4- Not specifying the element type that the ArrayList references
ArrayList myList = new ArrayList();
5- Trying to update ArrayList values while using an enhanced for loop
6- Changing size of an ArrayList while traversing with an enhanced for loop
7- Removing an element from an ArrayList at the wrong time

30
Q

Searching

A

Process incorporates iteration and selection
Specify each element, one at a time, and do not need to track index after execution
Compare value retrieved with value being searched for

31
Q

Linear search algorithms

A

Best used when we don’t know any idea about order of data

Look at each element to determine if what we are looking for is in fact inside the array/ ArrayList

32
Q

Different data types requires

A

Different comparisons
int values -> == operator
double values -> close enough(math)
object instance -> .equals(otherThing)

33
Q

Selection sort

A

Identifies either max/min of compared values and iterates over structure checking if item store matches condition
If so, replaces value stored with that value
Implementation requires helper method to perform swap operation

34
Q

Insertion sort

A

Building a sorted structure as it proceed

Inserts each value it finds at appropriate location in data structure using a while loop as inner loop

35
Q

Efficiency

A

One of main way to compare programs

How many times does a particular statement get executed?

36
Q

When collecting data about users, try to

A

Remove data collected
Minimize data collection
Anonymizing personal data