arrays ch6 arraylists ch14 Flashcards

1
Q

three ways in which [ ] are used

A

create type name
setting size - and creating new object
indexing

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

array out of bounds and null pointer

A

accessing position larger than the length of the array null pointer = create an array of e.g. size 5 and you don’t create initialize it - e.g. only put 2 instances in but you want to access object 3

  • When an index expression evaluates to some value other than those allowed by the array declaration, the index is said to be out of bounds
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what will happen if elements of the array arent initialized properly

A

they will automatically be initialized to the default value for their base type

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

is an array of characters a string

A

no - but a string is an array of characters

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

how to convert an array of characters to a string

A

E.g if array a = (a,b,c), string s = new string(a), it will join a,b,c together to form a string

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

string slicing

A

String s2 = new String(sringname,start,end(not incl));

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

what is each element in an object array

A

reference to the object in memory

A variable of an array type holds the address of where the array object is stored in memory

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

arrays are objects

A
  • collection of indexed variables

- single item whose value is a collection of values of a base type

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

the process of creating a new array

A

–An array variable names the array as a single itemdouble[] a;
–A new expression creates an array object and stores the object in memory new double[10
]–An assignment statement places a reference to the memory address of an array object in the array variablea = new double[10];

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

each element in a class type array must be..

A

instantiated and initialized

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

using arrays as parameters

A
  • array indexed variables and entire arrays can be used as arguments to methods
  • When you pass an array to a method, you pass references (because an array is an object - works as class variables (pass by reference))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how do array arguments behave

A
like objects of a class
a method can change the values stored in the indexed variables of an array arg

A method with an array parameter must specify the base type of the array onlyBaseType[]–It does not specify the length of the array

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

parameter of the main method

A

String array - args

Name of parameter is args - just standard convention, you could replace args with anything else
Java programs typically executed with command line
Command line instruction can be executed with different things - input for your programs, in command line, the file that you specify will be stored in the variable (array) args - separated by white space

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

for each loop

A

for (double element : a)

element = 0.0;

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

how does java make allowance for variable number of parameters

A
  • taking in an array as an argument

- e.g.) public static int max(int…arg)

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

how to prevent privacy leaks with array instance vars

A
  • return a reference to a deep copy of the private array object
  • public double[ ] getArray(){
    double[ ] temp = new double[count];
    for (int i = 0; i < count; i++)
    temp[i] = a[i];
    return temp
17
Q

what to do if the array is a class type and a deep copy must be returned

A
  • copies must be made of each class objecy in the array when the array is copied
  • use a for loop to create a new object for each element
18
Q

what is meant by enumerated type

A

a type where all the values are given in a typically short list
- enum TypeName {VALUE_1, VALUE_2, …, VALUE_N};

19
Q

what method is typically used with enumerated types? explain what it does

A

values()
returns an array whose values are the values of the enumerated type given in the order in which the elements are listed in the definition of the enumerated type
- base type of the array is the enumerated type

20
Q

defining a multidimensional array

A

double [ ] [ ] table = new double[10][12];

21
Q

what does .length do in a 2D array?

A

number of rows in the array

22
Q

how to reference the column in the array

A

char [ ] [ ] page = new char [30][100]; page[0].length = 100

23
Q

what is a ragged array?

A

Your rows can have different numbers of columns
Not like a table
Eg. double [ ][ ] a;
a = new double [3][ ]
Number of elements in each sub array is different

24
Q

how do arrays and array lists differ?

A

arraylists can change in length while the program is running

25
Q

how is the arraylist class implemented

A
  • using an array as a private instance variable
  • when the hidden array is full, a new larger hidden array is created and the data is transferred to this new array
  • You can create another array - add A’s elements to B and let A point to B?
26
Q

why we don’t always just use an arraylist instead of an array

A
  • less efficient than an array
  • doesn’t have the square bracket notation
  • The base type of an ArrayListmust be a class type (or other reference type): it cannot be a primitive type–This last point is less of a problem now that Java provides automatic boxing and unboxing of primitives
27
Q

how to use the Arraylist class

A
import java.util
ArrayList aList = new ArrayList();
you can specify the initial capacity but its not compulsory
28
Q

the add method

A

list.add(“something”);

adds to the first available position

29
Q

the size method

A

how many indices already have elements in them

int howMany = list.size();

30
Q

the set and get methods

A

list.set(index,”somethingelse”);

String thing = list.get(index)