Ch 9: Arrays Flashcards
what are the individual memory locations in an array called
elements
several consecutive memory locations of the same data type under one name is
an array
number of elements in an array is called
size or length of the array
program can refer to an individual element of an array using the array name followed by the element’s number which is called
[index or subscript] notice the brackets*
an index can be
any integer constant, variable, or expression
create an array
someType[] nameOfArray;
create an array with specified number of elements
int[] scores = new int [10];
what are default values for an array
numeric elements are initialized to 0
boolean to false
if array elements are of a class type then…
the array contains references to objects of that type, these are initialized to null
if array elements are references, you have to initialize each element by setting it to a valid reference before the element is used
color[0] = new Color (207, 189, 250); colors[1] = Color.BLUE;
what error do you get when you call a method of a non existing object(the reference is null)
nullpointerexception
another way to declare and create an array list is to list explicitly like
int [] scores = { 95,97,99, 100 };
String[] names = { “Vika”, “hello”, “justin” };
Color [] rainbowColors =
{
Color.Red, Color.Orange, Color.Grey, Color.Pink
};
once an array is declared and initialized, either with the new operator or with a list of values, it is not possible to ?
change its size
declare an array of 100 integer elemnts
final int MAXCOUNT = 100;
int[] a = new int [MAXCOUNT];
int[99] is the biggest since index begins at 0
how do you find the size of an array
arrayName.length
length acts as a public field that holds the size of an array
In array, length is not a method(as in the String class). It is accessed like a field