Introduction To Java Programming: Week 5
Introduction To Java Programming: Week 5
Chapter 7
Week 5
More Than Two Dimensions
• Java does not limit the number of dimensions that an array may have.
• More than three dimensions is hard to visualize, but can be useful in
some programming problems.
More Than Two Dimensions - Example
• String[ ][ ][ ][ ][ ] courses=new String [numUniv] [numColleges] [numDept]
[numFac] [numCourses];
• courses[3][0][1][5][2] is course number 2 taught by instructor number 5 in
department number 1 in college number 0 of university number 3 in the state
The ArrayList Class
• Similar to an array, an ArrayList allows object storage
• Unlike an array, an ArrayList object:
• Automatically expands when a new item is added
• Automatically shrinks when items are removed
• Requires:
• import java.util.ArrayList;
Creating and Using a Generic ArrayList
• Create an ArrayList object with no-args constructor
Type of Data stored in ArrayList
In this statement 3 is the index of the item to get. The index starts
with 0 as with ordinary arrays.
• Example: ArrayListDemo1.java
Using an ArrayList
• The ArrayList class's toString method returns a string
representing all items in the ArrayList
System.out.println(nameList);
This statement yields :
[ James, Catherine ]