ArrayLists Array Before we have only looked at single variables An array can hold a number of variables int anIntArray declares an array of integers anIntArray new int 10 allocates memory for 10 ID: 625567
Download Presentation The PPT/PDF document "Arrays and" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Arrays and ArrayListsSlide2
Array
Before we have only looked at single variables.
An array can hold a number of variables.
int
[]
anIntArray
; // declares an array of integers
anIntArray
=
new
int
[10]; // allocates memory for 10
integers
Can hold primitive data types and objects. Slide3
Length of an array
To the
the
“size” of an array use .length
Is this a method?
Useful when iterating through an array
for(int i = 0; i < anIntArray.length; i++) {
System.
out.println
("Element at index "+
i
+": "
+
anIntArray
[
i
]);
}
Common error “Out of bounds exception”. Slide4
Arrays – benefits
Arrays are good when you know how many elements you will have to deal with.
However, if the number of elements is not known, or may change we have to manage an array.
Deleting elements in an array and shuffling elements can be tricky and error prone.
Sorted/Unsorted Arrays. Slide5
ArrayList
An
A
rrayList
can be thought of as a variable length array.
Elements/items can be added and deleted, and inserted at a given position.
import
java.util.ArrayList
;Slide6
Declare an arrayList
ArrayList
<String>
stringList
=
new
ArrayList
<String
>();
//note that we must state the data type of the variables we are going to store.
In this case the data type is StringSlide7
Adding an element
Here we add two strings
stringList.add
("John");
stringList.add
("David");
Note that we cannot add something of a different data type – we will get a compile time errorSlide8
add(int index, E element)
Inserts
the specified element at the specified position in this list
.
It also shifts all elements up by one position
And of course the size of the
ArrayList
has increased by one. Slide9
Contains
We can ask if the
arrayList
stringList
contains a given String
The method will return true or false
E.g.
stringList.contains
("John")Slide10
Is it Empty
We could check if an
arrayList
is empty like this
if
(
stringList.size
() == 0){
System
.out.println
("
ArrayList
is empty");
}
isEmpty
() method returns true
if this
ArrayList
contains no elements.Slide11
Removing an Item at an Index
stringList.remove
(0);
This will remove the item at position/index 0
It will also shuffle all the elements down by one position.
So the
arrayList
is now shorter by one elements
Check this with the size() method. Slide12
Replacing an element
use
set (
int
index, E element) method of java
ArrayList
to replace any element from a particular index.
Below
code will replace first element of
stringList
with “AAA".
stringList.set
(0,“AAA");
Set index 0 to “AAA”Slide13
Clearing an ArrayList
To “reset” an
ArrayList
just call the method
c
lear()
This will remove all the data, and set the size to zero
Confirm by calling method size(). Slide14
indexOf and lastIndexOf
indexOf
Returns
the index of the first occurrence of the specified element in this list,
or
-1 if this list does not contain the element
.
lastIndexOf
Returns the index of the last occurrence of the specified element in this list,
or -1 if this list does not contain the element.Slide15
addAll
So far we have only added/deleted single elements
addAll
(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.
addAll
(
int
index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list, starting at the specified position.Slide16
removeRange
removeRange
(
int
fromIndex
,
int
toIndex
)
Removes from this list all of the elements whose index is between
fromIndex
,
inclusive
, and
toIndex
,
exclusive
.
BE CAREFUL WITH “off by one errors”!!!
We (computer scientists) start counting at zero (most computer languages), which means the last elements of
ArrayList
size n is n-1, and the first (1st) has index zero.