arrays ch6 arraylists ch14 Flashcards
three ways in which [ ] are used
create type name
setting size - and creating new object
indexing
array out of bounds and null pointer
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
what will happen if elements of the array arent initialized properly
they will automatically be initialized to the default value for their base type
is an array of characters a string
no - but a string is an array of characters
how to convert an array of characters to a string
E.g if array a = (a,b,c), string s = new string(a), it will join a,b,c together to form a string
string slicing
String s2 = new String(sringname,start,end(not incl));
what is each element in an object array
reference to the object in memory
A variable of an array type holds the address of where the array object is stored in memory
arrays are objects
- collection of indexed variables
- single item whose value is a collection of values of a base type
the process of creating a new array
–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];
each element in a class type array must be..
instantiated and initialized
using arrays as parameters
- 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 do array arguments behave
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
parameter of the main method
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
for each loop
for (double element : a)
element = 0.0;
how does java make allowance for variable number of parameters
- taking in an array as an argument
- e.g.) public static int max(int…arg)
how to prevent privacy leaks with array instance vars
- 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
what to do if the array is a class type and a deep copy must be returned
- 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
what is meant by enumerated type
a type where all the values are given in a typically short list
- enum TypeName {VALUE_1, VALUE_2, …, VALUE_N};
what method is typically used with enumerated types? explain what it does
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
defining a multidimensional array
double [ ] [ ] table = new double[10][12];
what does .length do in a 2D array?
number of rows in the array
how to reference the column in the array
char [ ] [ ] page = new char [30][100]; page[0].length = 100
what is a ragged array?
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
how do arrays and array lists differ?
arraylists can change in length while the program is running
how is the arraylist class implemented
- 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?
why we don’t always just use an arraylist instead of an array
- 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
how to use the Arraylist class
import java.util ArrayList aList = new ArrayList(); you can specify the initial capacity but its not compulsory
the add method
list.add(“something”);
adds to the first available position
the size method
how many indices already have elements in them
int howMany = list.size();
the set and get methods
list.set(index,”somethingelse”);
String thing = list.get(index)