Arrays and Array Lists Array Before we have
Arrays and Array. Lists
Array • Before we have only looked at single variables. • An array can hold a number of variables. • int[] an. Int. Array; // declares an array of integers • an. Int. Array = new int[10]; // allocates memory for 10 integers • Can hold primitive data types and objects.
Length of an array • • To the “size” of an array use. length Is this a method? Useful when iterating through an array for(int i = 0; i < an. Int. Array. length; i++) { System. out. println("Element at index "+i+": " + an. Int. Array[i]); } Common error “Out of bounds exception”.
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.
Array. List • An Array. List 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. Array. List;
Declare an array. List • Array. List<String> string. List = new Array. List<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
Adding an element • • Here we add two strings string. List. add("John"); string. List. add("David"); Note that we cannot add something of a different data type – we will get a compile time error
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 Array. List has increased by one.
Contains • We can ask if the array. List string. List contains a given String • The method will return true or false • E. g. string. List. contains("John")
Is it Empty • We could check if an array. List is empty like this • if(string. List. size() == 0){ System. out. println("Array. List is empty"); } • is. Empty() method returns true if this Array. List contains no elements.
Removing an Item at an Index • string. List. remove(0); • This will remove the item at position/index 0 • It will also shuffle all the elements down by one position. • So the array. List is now shorter by one elements • Check this with the size() method.
Replacing an element • use set (int index, E element) method of java Array. List to replace any element from a particular index. • Below code will replace first element of string. List with “AAA". string. List. set(0, “AAA"); Set index 0 to “AAA” •
Clearing an Array. List • To “reset” an Array. List just call the method • clear() • This will remove all the data, and set the size to zero • Confirm by calling method size().
index. Of and last. Index. Of • index. Of • Returns the index of the first occurrence of the specified element in this list, • or -1 if this list does not contain the element. • last. Index. Of • Returns the index of the last occurrence of the specified element in this list, • or -1 if this list does not contain the element.
add. All • So far we have only added/deleted single elements • add. All(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. • add. All(int index, Collection<? extends E> c) • Inserts all of the elements in the specified collection into this list, starting at the specified position.
remove. Range • remove. Range(int from. Index, int to. Index) • Removes from this list all of the elements whose index is between from. Index, inclusive, and to. Index, exclusive. • BE CAREFUL WITH “off by one errors”!!! • We (computer scientists) start counting at zero (most computer languages), which means the last elements of Array. List size n is n-1, and the first (1 st) has index zero.
- Slides: 16