intro to arrays Flashcards

1
Q

array

A

> Collection of values that can be treated as a unit or individually
a very useful data structure provided by Java and other programming languages
sequence of variables of the same type
objects (hence allocated on heap) with a reference on the stack

(visualize an array as a set of variables one after another)

int[] a = new int[5];

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

data structure

A

mechanism for storing data in a structured way

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

what does “sequence of variables of the same type” mean?

A

> Homogeneous data structure (values of the same type (“base type”) )
Size (quantity) fixed when space is allocated
Ordered

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

do arrays have default values?

A

Yes!
“instance variables” of array = cells in array
are assigned default values (0 / null / etc.) when array is created

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

array variable declaration

A

int[] a or int a[];

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

array object creation

A

new int[5]
or combo: int[] a = new int[1];
(creates an array of 5 cells in the heap)

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

array indexing

A

mechanism for accessing individual elements of an array a[0]

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

to modify arrays:

A

o modify contents of cell #2 to 6 and cell #1 to 74:
a[2] = 6;
a[1] = 74;

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

to use arrays

A

System.out.println(“value = “ + (a[1]-a[2]));

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

why are cells are just like variables

A

They may be read: x = a[3];

They may be written: a[2] = 7;

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

to process all elements in an array

A

for (int i = 0; i < a.length; i++){
…process the one element at a[i]…
}

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

what does this do in heap?
int[] a = new int[5];
int[] b = a;

A

aliasing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
what does this do in heap?
int[] a = new int[5];
int[] b = new int[a.length]; 
for (int i = 0; i < a.length; i++){ 
b[i] = a[i]; 
}
A

create a new object copy

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

is an array’s length mutable or immutable?

A

a.length is immutable!

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

explain how to enlarge an array

A
  1. Create a new larger array object
  2. Copy old array contents into new object
  3. Assign address of new object to variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to enlarge an array int[]a

A
int[] a = new int[5]; 
int[] temp = new int[a.length + 1]; 
for (int i = 0; i < a.length; i++){ 
temp[i] = a[i]; 
} 
a = temp;

(New variabletempcreated to hold copy and previous contents of a become garbage)

17
Q

components of an array

A

Arrays= objects
Array variables= references
Array cells= variables of the base type (reference or primitive)

18
Q

how to pass arrays into methods

A

pass-by-value

19
Q

Arrays may be initialized
at declaration time
by using values enclosed in { }

(curly brackets NOT [ ])

A
Counts elements, creates array of correct size and copies elements into the array
examples:
int[] a = {5, 10, 15};
String[] b = {"John", "Mary"}; 
String[] c = {new String("Mike"), new String("Kyle")}; 
int[] d = new int[]{5, 10, 15};
20
Q

reference copy array code

A

publicstaticvoidmain(String[]args)throwsException{
Student[]array1_1=newStudent[10];
//somecodeherewhichfillsinthearraywithdata
Student[]array1_2=array1_1;
}

21
Q

shallow copy array code

A
Student[]array2_1=newStudent[10]; 
//somecodeherewhichfillsinthearraywithdata  Student[]array2_2=newStudent[array2_1.length]; 
for(intiterator=0;iterator
22
Q

deep copy array code

A
Student[]array3_1=newStudent[10];
//somecodeherewhichfillsinthearraywithdata Student[]array3_2=newStudent[array3_1.length]; for(intiterator=0;iterator