Arrays Flashcards

1
Q

What are arrays?

A

A type of object in Java used to store many values in one variable. Created to hold a specific number of values (any non-negative integer)

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

What is the length of an array?

A

An array always knows how many values it contains: its length. The length can never change.

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

What is an element?

A

An individual item stored in an array

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

What is a subscript?

A

aka an index: each element has an index/subscript. The subscripts are always from 0 to length-1

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

How do you create an array?

A

There are two parts:
1. declare the reference variable for the array. like int[] someInts;
2. instantiate the array and assign it to the reference variable. like someInts = new int[7];

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

What are some things to be aware of when declaring an array?

A
  • there is never a number inside the []
  • This type can hold a reference to any array of that type of data
  • declaring an array does not create an array object
  • at first, the variable contains null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are some things to be aware of when instantiating an array?

A
  • must use the ‘new’ keyword
  • this allocates memory for an array object that can hold a specific number of elements
  • instantiation gives you a reference to the newborn array
  • the length can never change but the elements can change
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does it mean for an array to be an Object?

A
  • array reference variables are stored on the stack, array objects are stored on the heap
  • the stack contains all primitive types and references to other data
  • the heap contains all non-primitive data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the ‘null’ reference mean?

A

“no object”. A new reference variable will always contain null if you don’t create an object and assign the object to the reference variable. You can set object reference variables to null, meaning that there is no address stored and the variable does not point to an object on the heap

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

What are some things to keep in mind when literally instantiating an array?

A
  • Literals do not require the new keyword to create the array but you can use it
  • if you are using literal initialization but it’s declared on a different line you need the new
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you access the length of the array?

A

using myData.length. There is no (). You cannot change the length, only find its value

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

How can you access the elements of the array?

A

Using myData[expression]; The expression must result in an integer from 0 to myData.length-1. You can freely access or change the elements anywhere you can use any other variable of that type

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

How must you go about copying an array?

A

If you have two array variables of the same type, you can use assignment to ‘copy’, but this will be a shallow copy as it just copies the reference. For a deep copy that creates a new separate array you have to instantiate a new array of the same size, and loop through the original and copy each element into the second array

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

What gets passed or returns when passing arrays?

A

Only the reference. The function gets a shallow copy. That means that any changes made to the elements of an array parameter inside a function will affect the original. However, if you overwrite the array parameter, it will now point to a new array and not change the address in the original array

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

What are aliases?

A

Two array variables that point to the exact same array object (have the same reference)

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

What are partially filled arrays?

A

Has data in some bins, but not all. Use a separate int variable to keep track of the current size. You can’t use literal array creation to create a partially filled array

17
Q

What are the two fundamental algorithms for searching an array?

A

Linear search and binary search

18
Q

What is a linear search?

A
  • looks through the array one element at a time
  • slow
  • can handle any list
19
Q

What is the binary search?

A
  • divides the list in half repeatedly
  • fast
  • requires the list to be sorted
  • pick the middle of the list. If the value is larger, go left, and right if the value is smaller.
  • mid = (lo+hi)/2
  • change hi/lo value to ‘get rid’ of half the list
  • you’ll know when you’ve run out of places to look when lo>hi
20
Q

How do you know if you should use a binary search?

A
  • if you happen to have a sorted list already
  • if you plan to do a lot of searching but the list doesn’t change often, so keeping it sorted is easy
  • if you have lots of time to sort, but when they happen, the searches need to be fast
21
Q

What is ordered insertion?

A

One way to get a sorted list: add in order as you go. Work from the end of the list to the front, moving items one spot to the right, until the new item is bigger than the current item, then insert it

22
Q

What are impossible value partially filled arrays?

A

Partially filled arrays where instead of having all the empty bins at the end, they are marked with a constant defined to identify the impossible value. To insert an item, find the first empty bin.