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)