ISYS 303 - Ch. 5-7, ArrayList Flashcards
Null Array
In order to declare an array that you want to resize later, then you need to assign it a null value:
String[] asNames = null;
Array
Collection of variables of the same type, referred to by a common name.
An array is a set of contiguous memory locations, Once you declare the array you cannot change the size, An array is an object and it has attributes
Declare Array (One-Dimensional)
When you declare the array you have to use the “new” keyword and give it a size (unless you plan on resizing it later - null array)
type array-name[] = new type[size]
- **type declares the element type of the array, and determines the data type of each element contained in the array
- **the number of elements that the array will hold is determined by size
Creation of an Array
Since arrays are implemented as objects, the creation of an array is a two-step process. First you declare an array reference variable. Second, you allocate memory for the array, assigning a reference to that memory to the array variable.
int sample[] = new int[10]
***sample holds a reference to the memory allocated by new
int sample[]; //no physical object
sample = new int[10]; //now created an object and linked to an array
The [] can go on the data type or on the variable name:
String[] asNames = new String[3];
IS THE SAME AS
String asNames[] = new String[3];
Find MIN/MAX
min = max = sample[0] for(iCount = 1; iCount max) max = sample[iCount];
index
An individual element within an array is accessed by use of an index. An index describes the position of an element within an array. All arrays have zero as the index of their first element.
You access each element of the array by using the index.
The index is 0 based meaning that if you create an array that is 3 long, the locations or indexes are 0, 1, and 2
asNames[0] = "Greg"; asNames[1] = "Joe";
.length
One attribute is length and it tells you the size of the array or the number of elements in the array
Initialized arrays
Arrays can be initialized when they are created. The general form for initializing arrays is:
type array-name[] = {value1, value2, value 3,…, valueN}
Sorting an Array
Bubble sort uses the repeated comparison and, if necessary, exchange of adjacent elements in the array. Small values move toward one end and vice versa. Operates by making several passes through the array, exchanging out of place elements when necessary. The number of passes required to ensure the array is sorted is equal to one less than the number of elements in the array.
Bubble Sort Code
//this is the bubble sort for(a = 1; a = a; b--) { if(nums[b-1] > nums[b]) { t = nums[b-1]; nums[b-1] = nums[b]; nums[b] = t; } } }
Strings in Arrays
- Strings are really just an array of characters
- They also have attributes and methods
String Array Methods
-One of them is length which tells you how many characters is in the string:
asNames[1].length
-Another method is equals which does a comparison to another string:
if (asNames[1].equals(“Greg”))
-Another method is equalsIgnoreCase which does a comparison to another string but ignores the case:
if (asNames[1].equalsIgnoreCase(“joe”))
Two/Multi-Dimensional Array
To declare a two-dimensional integer array table of size 10, 20 you would write:
int table[][] = new int[10][20];
EX:
int t, i
int table[][] = new int[3][4];
for(t = 0; t
Irregular(Ragged) Multidimensional Arrays
When you allocate memory for MULTIDIMENSIONAL array, you need to specify ONLY the memory for the FIRST (leftmost) dimension. You can ALLOCATE the REMAINING dimensions separately.
EX: int table[][] = new int[3][]; table[0] = new int[4]; table[1] = new int[4] table[2] = new int[2]
Since multidimensional arrays are implemented as arrays of arrays, the length of each array is under your control.
Arrays of 3+ Dimensions
type name[][]…[] = new type[size1][size2]…[sizeN]
Ex:
int multidim[][][] = new int[3][10][4]
Initializing Multidimensional Arrays
type array-name[][]...[] = { {value, value,..., value}, {value, value,..., value}; . . . {value, value,..., value} };
Alternative Array Declaration
int[] nums, nums2, nums3; //creates 3 arrays
int nums[], nums2[], nums3[]; //also creates 3 arrays
Assigning Array References
When you assign one array reference variable to another, you are changing what object that variable refers to. You are NOT causing a copy of the array to be made nor are you causing the contents of one array to be copied to the other.
EX: int i; int nums1[] = new int[10] int nums2[] = new int[10] nums2 = nums1;
After the assignment of num1 to nums2, both array reference variables refer to the same object.
Length Member
Because arrays are implemented as objects, each array has associated with it a length instance variable that contains the number of elements that the array can hold. (In other words, length contains the size of the array)
The value of length has nothing to do with the number of elements that are actually in use. It contains the number of elements that the array is CAPABLE of HOLDING.
Length Member for Multidimensional Arrays
EX:
int table[][] = new int{{1, 2, 3}, {4, 5}, {6, 7, 8, 9}};
table.length //obtains the # of arrays stored in table
table[0].length //obtain length of any individual array
table[1].length
table[2].length
Operating on String
Boolean equals(str) - returns TRUE if the invoking string contains the same sequence as str. int length() - obtains the length of the string char charAt(index) - obtains the character at the index specified by index int compareTo(str) - Returns less than zero if the invoking string is less than str, greater than zero if the invoking string is greater than zero, and zero if the strings are equal int indexOf(str) - Searches the invoking string for the substring specified by str. Returns the index of the first match or -1 on failure int lastIndexOf(str) - Searches the invoking string for the substring specified by str. Returns the index of the last match or -1 pm failure.
Java’s Access Modifiers (Scope)
Private - Visible in the class (but not the subclass) Protected - Visible in the class, subclass, and same package it's in, no other package outside its package Public - Visible everywhere(the entire project aka the classes, packages, and subclasses) No Modifier - Visible in the class and package, not the subclass or "world"
Inheritance
Inheritance means that one class is built upon another class Using inheritance you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes. The class that does the inheriting is called a subclass. A subclass is a specialized version of the parent class. It inherits all of the variables and methods defined by the parent class and adds its own unique elements.
Inheritance Example
//a subclass of TwoDShape for triangles class Triangle extends TwoDshape { code here... }
**inheritance is allowed by using the word “extends”
**subclass name + “extends” + parent class name
**Inheritance is done through the keyword extends
The top class is called the parent.
The child class is called the subclass
**Inheritance DOES NOT overrule private