You are on page 1of 23

12 Feb 2024

Radford Burger
Agenda for today
• Netbeans project naming convention
• Re-cap of Static methods
• JOptionPane class
• Prac 2: Math class
• Arraylists
Netbeans naming
convention
Right-click on the package name to create more classes
Static methods
• JOptionpane class
o Any questions??
• Math class
o Prac 2
Java API

https://docs.oracle.com/javase/7/docs/api/
Introduction to ArrayLists

Standard Java arrays are of a fixed length.


You must know in advance how many elements an
array will hold.
When an element is removed from an array, the size of
the array remains the same.
Introduction to ArrayLists

Arraylist is a part of the set of standard collection


classes (Java Collections Framework) that
implements Collection interfaces.

There are other classes in the set:


https://www.tutorialspoint.com/java/java_collections.htm
Introduction to ArrayLists

 When an element is added to an ArrayList, the


collection is automatically enlarged. When objects
are removed, the arraylist will shrink.
Introduction to ArrayLists

• Defined in package java.util


• A pre-defined data structure to store groups of related
objects
• Provides methods to organize, store and retrieve data
• Only non-primitive types can be stored
• Index starts at zero – same as arrays
ArrayLists: declaration syntax

ArrayList<String> items = new ArrayList<String>( );


OR
ArrayList items = new ArrayList( );

There are overloaded constructors available.


Use the API docs to find out more.
ArrayLists: methods

items.add (“red”);
items.add (0, “blue”);
items.remove (1);

Use the API docs for other methods


ArrayLists: example
import java.util.*;
public class ArrayListDemo {
public static void main(String args[]) {
// create an array list
ArrayList al = new ArrayList();
System.out.println("Initial size of al: " + al.size());
// add elements to the array list
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " + al.size());
System.out.println("Contents of al: " + al);
ArrayLists: example output

Initial size of al: 0


Size of al after additions: 7
C al.add("C");
al.add("A");
C A al.add("E");
al.add("B");
C A E
al.add("D");
C A E B al.add("F");
al.add(1, "A2");
C A E B D

C A E B D F

C A2 A E B D F

Contents of al: [C, A2, A, E, B, D, F]


ArrayLists: example continued
// Remove elements from the array list
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " + al.size());
System.out.println("Contents of al: " + al);
}
} C

C A

C A E

C A E B

C A E B D

C A E B D F

C A2 A E B D F
ArrayLists: example output
C A2 A E B D F

C A2 A E B D
al.remove("F");
C A2 E B D al.remove(2);

Size of al after deletions: 5


Contents of al: [C, A2, E, B, D]
Preparation for next class
 Try completing the solution for Prac 3, which is
posted on BB.
 Can someone please volunteer to share their
attempt in the next class, even if it is incomplete
or does not work properly.
Conclusion

Please check BB regularly for any


announcements
If you have any questions/queries please
contact me via email:
burgerr@cput.ac.za
Please use your cput email for all
correspondence with your lecturers

You might also like