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


For a String Array List

// Declare array list for Strings
ArrayList <String> strWordsArrLst = new ArrayList();

For an Integer Array List

// Declare array list for Integers
ArrayList <Integer> intNumbersArrLst = new ArrayList();


Accessing Elements of an ArrayList


Adding Elements to 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

e can be any element - chr, string, int, etc. and can also be a variable

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.


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

Same as Add except remove the string from the arraylist

List Button using an Iterator (Or Printing the Array List)

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


The code below will print the array list in the text area,

if you are not including a list button make this a method and call it every time you want to print.


Sort Button

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)



This assignment is now ABOVE AND BEYOND (20%)
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.



Sample Application Output