Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and ArrayLists.

Similar presentations


Presentation on theme: "Arrays and ArrayLists."— Presentation transcript:

1 Arrays and ArrayLists

2 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.

3 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”.

4 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.

5 ArrayList An ArrayList 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;

6 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 String

7 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 error

8 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.

9 Contains We can ask if the arrayList stringList contains a given String The method will return true or false E.g. stringList.contains("John")

10 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.

11 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.

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

13 Clearing an ArrayList To “reset” an ArrayList just call the method
This will remove all the data, and set the size to zero Confirm by calling method size().

14 indexOf and lastIndexOf
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,

15 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.

16 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.


Download ppt "Arrays and ArrayLists."

Similar presentations


Ads by Google