ISYS 303 - Ch. 5-7, ArrayList Flashcards

1
Q

Null Array

A

In order to declare an array that you want to resize later, then you need to assign it a null value:

String[] asNames = null;

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

Array

A

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

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

Declare Array (One-Dimensional)

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Creation of an Array

A

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];

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

Find MIN/MAX

A
min = max = sample[0]
for(iCount = 1; iCount  max) max = sample[iCount];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

index

A

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";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

.length

A

One attribute is length and it tells you the size of the array or the number of elements in the array

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

Initialized arrays

A

Arrays can be initialized when they are created. The general form for initializing arrays is:

type array-name[] = {value1, value2, value 3,…, valueN}

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

Sorting an Array

A

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.

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

Bubble Sort Code

A
//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;         
                }
            }
        }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Strings in Arrays

A
  • Strings are really just an array of characters

- They also have attributes and methods

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

String Array Methods

A

-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”))

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

Two/Multi-Dimensional Array

A

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

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

Irregular(Ragged) Multidimensional Arrays

A

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.

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

Arrays of 3+ Dimensions

A

type name[][]…[] = new type[size1][size2]…[sizeN]

Ex:
int multidim[][][] = new int[3][10][4]

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

Initializing Multidimensional Arrays

A
type array-name[][]...[] = {
{value, value,..., value}, 
{value, value,..., value};
.
.
.
{value, value,..., value}
};
17
Q

Alternative Array Declaration

A

int[] nums, nums2, nums3; //creates 3 arrays

int nums[], nums2[], nums3[]; //also creates 3 arrays

18
Q

Assigning Array References

A

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.

19
Q

Length Member

A

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.

20
Q

Length Member for Multidimensional Arrays

A

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

21
Q

Operating on String

A
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.
22
Q

Java’s Access Modifiers (Scope)

A
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"
23
Q

Inheritance

A
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.
24
Q

Inheritance Example

A
//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

25
Q

Super

A
They keyword super gives you access to the parent class attribute, method, or constructor
When both the parent class and the subclass define constructors, both must be executed. 
1. The first calls a parent class constructor. 
2. Used to access a member of the parent class that has been hidden by a member of a subclass
26
Q

Using “SUPER” to call parent class constructors

A

super(parameter-list)

  • **parameter list specifies any parameters needed by the constructor in the parent class
  • **super() must always be the first statement executed inside a subclass constructor
EX: 
class Person
{
   String Firstname;
   String Lastname;
   Person(String sFirst, String sLast)
   {
      this.Firstname = sFirst;
      this.Lastname = sLast;
   }
}
class Student extends Person
{
   double GPA;
   Student(String sFirst, String sLast, double dGPA)
   {
      super(sFirst, sLast);
      this.GPA = dGPA;
   }
}

Herw, Student() calls super() with the parameters sFirst, sLast. This causes the Person() constructor to be called, which initializes Firstname and Lastname using these values. Student() no longer initializes these values itself. It only need initialize the value unique to it: GPA.

27
Q

Object Class

A
All classes in Java are built upon the Object class. IOW, as you proceed up through the inheritance line (like genealogy), the top class (Adam or Eve) is called Object
Object is the base class from which all objects are derived. IOW, it is the top base class or parent class for all java classes
28
Q

Method Overriding

A
You can override a method in the parent class when a method in the subclass is the same return type and signature as a parent method
A parent class reference can refer to a subclass object. Why is this important? When an overridden method is called through a parent class it is the type of object being referred to that determines which version of the method is called.
29
Q

Final

A

final prevents inheritance and overriding. In other words, if you declare a class as final, it cannot be subclassed

30
Q

Final Example

A
final class Person
{
   Code here...
}
This means you could not create a subclass called student which extends from person
Object is the base class from which all objects are derived. IOW, it is the top base class or parent class for all java classes
31
Q

ArrayList Notes

A

An arraylist differs from an array in that it acts like an according which can shrink and grow
An array is used when you know the number of elements
An arraylist is used when you do not know the number of elements of if the size will change
You create an arraylist by first importing the arraylist class
import java.util.ArrayList;

32
Q

Create an ArrayList

A

ArrayList alStudents = new ArrayList();

OR if you wanted an arraylist of Student objects:

ArrayList alStudents = new ArrayList();

However, once you make the arraylist, you would still have to fill each element using the add method and creating an object

33
Q

Create Object in ArrayList

A

ArrayList myTeam = new ArrayList();

myTeam.add(new Team());
myTeam.add(new Team());
myTeam.add(new Team());
You can use the get method to access the object and then access the attribute or method associated with the object
34
Q

ArrayList Size Method

A

The size method returns the number of elements in the array list (0 based)

//set team wins and losses to 0
for (int i = 0; i
35
Q

You can use the get method to access the object and then access the attribute or method associated with the object
The size method returns the number of elements in the array list (0 based)

A
//set team wins and losses to 0
        for (int i = 0; i
36
Q

Getting the size (ArrayList)

A

ArrayVariableName.size

// Getting the size of the list
int size = list.size();
System.out.println("The size of the list is: " + size);
37
Q

Setting the ArrayList

A

ArrayVariableName.set()

// Replacing an element
list.set(1, "NewItem");
System.out.println("The arraylist after the replacement is: " + list);
38
Q

Clear

A

ArrayVariableName.clear();

39
Q

Remove

A

ArrayVariableName.remove();