Introduction to Java using NetBeans

Section 7 : Arrays


Introduction to Java using NetBeans

A Java ArrayList holds an ordered sequence of items like an array, but there are differences:

  • an ArrayList has no fixed size but can be extended by adding elements later;
  • an ArrayList can only hold objects;
  • ArrayList elements are addressed by methods and not by square bracket notation.



Creating an Array List

import java.util.ArrayList



Accessing Elements of an ArrayList


Removing Elements of an ArrayList

Your turn..... Figure out how to replace one element of a list with another



Common Methods with ArrayLists

name.add(e); - adds "e" to the end of the ArrayList;
name.add(i, e); - adds "e" at index "I" of the ArrayList;
name.set(i, e); - sets the element at index "i" to "e";
name.get(i); - returns the object at index "i";
name.contains(e); - returns True if the ArrayList contains "e";
name.indexOf(e); - returns index of the first occurrence of "e", or -1 if not there;
name.lastIndexOf(e); - returns index of the last occurrence of "e", or -1 if not there;
name.clear(); -removes all elements in the ArrayList;
name.remove(i); - removes the element at position "i";
name.subList(i,j).clear(); - removes elements from positions i thru j;
name.size(); - returns the number of elements in the ArrayList.


Practice Assignment 7.4 - String ArrayLists and GUI

Create a Form similar to the one below

This is an image of the Student Record GUI with add, remove, sort, list and exit buttons. It also contains a name field and an output box.


Program Code Hints

import java.util.ArrayList;

Define the ArrayList at the start of the main program. This will allow it to be used throughout the program as a global variable.

ArrayList <String> names = new ArrayList();

Exit Button

Add Button

This code retrieves the information from the textbox, then adds the information to the end of the ArrayList.


Remove Button

This code removes the data from the list and renumbers the remaining entries.

List Button using an Iterator

Often, you will want to cycle through the elements in a collection. For example, you might want to display each element.

The easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface.

Iterator enables you to cycle through a collection, obtaining or removing elements

This code creates the output string composed of each of the entries.

Import java.util.Iterator

Sort Button (Bonus if you can figure out how to sort by lastname)

Sorting an Arraylist

import java.util.Collections;




Your turn ...

1. Add a find button that determines if an item exists and it's position.
2. Add a clear array button

3. Add the option to remove a range of elements.
4. Add a replace button that replaces a string with another string. (one specific occurrence or all)
     You must use remove and add to complete this

Make sure that your program checks for invalid input and takes care of all possible errors.

How to check if a string is an integer using parseInt

int num = Integer.parseInt(str);
// is an integer!
} catch (NumberFormatException e) {
// not an integer!
1. Figure out how to sort  by lastname
2. Add upgrades to this program



Assignment 7.5 - Sum Elements Program

You can use a String ArrayList

or an Integer ArrayList

Integer Array List Format ArrayList<Integer[]> list = new ArrayList<>();

Program Requirements:

Create a program that has the ability to store and display integer values in an array.

  • Add additional elements to the array using screen input.
  • Remove array elements based on screen input.
  • List all of the elements in the array in numeric order and compute the sum of all the elements.
  • List the even numbers in the array and compute the sum of the even elements.
  • List the odd numbers in the array and compute the sum of the odd elements.
  • Assume that all numbers are zero or positive.



Sample Application Output