Docsity

Arrays and ArrayLists in Java: A Comprehensive Guide, Study notes of Computer Programming

Toggle sidebar
bg1
ARRAYS AND ARRAYLISTS
Chapter 7
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Arrays and ArrayLists in Java: A Comprehensive Guide and more Study notes Computer Programming in PDF only on Docsity!

ARRAYS AND ARRAYLISTS

Chapter 7

Array

 Sequence of values of the same type

 Primitive types

 Objects

 Create an Array

 double[] values = new double[10]

 int[] values = {2,4,5,6,7,8,9}

 BankAccount[] accounts=new BankAccount[10]

Differences Between Java and Visual Logic

 Visual Logic

 Do not have to define type of array

 Use ( ) to surround index number

 Java

 Have to define type of array

 Must use the new operator when creating the array

 Use [ ] to surround index number

Default Initialization of Array Elements

 Array of numbers (int or double) = 0

 Array of boolean = false

 Array of objects = null

Add Value

values
double[]
Values[1] = 10.

More Definitions

 Index values – range from 0 to length- 1

 Bounds error

 Accessing a non existent elements

 Program terminate

 values.length() – method to get the length of the

array named values

 Parallel arrays

 2 or more arrays used to describe one thing

Major Problem With Array

 Length is fixed

 Array can develop “holes in delete” or “add”

 Won’t know if array is full

Array List

 Allows you to collect objects just like arrays.

 Can grow and shrink as needed

 Has methods for inserting and deleting objects.

 Will not work on primitive types

names.add(“Kathy”); Add elements to end

System.out.println(names) Prints [Kathy]

names.add(1,”Bob”) Inserts Bob before Kathy

names.remove(0) removes first element - Bob

names.set(0,”Bill”) removes Kathy

puts Bill in Kathy’s place

String name = names.get(0) gets the first element

String name =

namew.get(names.size()-1)

gets last element

How To Use Array Lists

Wrapper Classes

 The object class for a

corresponding primitive

type

 Can convert from

primitive to wrapper

 Can store Wrapper in

ArrayList

 Convert int to Integer

 Use Array List of type

Integer

Primitive Wrapper

byte Byte

boolean Boolean

char Character

double Double

float Float

int Integer

long Long

short Short

Enhanced for Loop

 Shortcut

 Traverses all elements of a collection

double [] values = ………….;

double sum = 0;

for (double element : values)

sum = sum+ element ;

 Loop variable contains an element not index.

Partially Filled Array

 arrayName.length() gives number of elements

 Does not give how many are used

 Keep a companion value to track how many

elements are used.

Inserting An Element

 Array List

 If order doesn’t matter simply use

◼ arrayListName.add(element)

 If order does matter use

◼ arrayListName.add(position, element)

 Array

 If order doesn’t matter

◼ use index of next available opening

 If order does matter

◼ must shift to create opening

Copying an Array

 An array variable stores a reference to the array.

 Copying yields a second reference to the same

array.

 to create a true copy use copyOf