Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Introduction To Java Programming: Week 5

The document discusses arrays with more than two dimensions in Java and introduces the ArrayList class as an alternative to arrays that can dynamically expand and shrink as elements are added or removed. It provides examples of creating and using ArrayLists to store different data types, including custom objects, and methods for accessing, adding, removing and replacing elements in an ArrayList.

Uploaded by

Shabana Tahir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Introduction To Java Programming: Week 5

The document discusses arrays with more than two dimensions in Java and introduces the ArrayList class as an alternative to arrays that can dynamically expand and shrink as elements are added or removed. It provides examples of creating and using ArrayLists to store different data types, including custom objects, and methods for accessing, adding, removing and replacing elements in an ArrayList.

Uploaded by

Shabana Tahir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Introduction to Java Programming

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

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

• To populate the ArrayList, use the add method:


nameList.add("James");
nameList.add("Catherine");
• To get the current size, call the size method
nameList.size(); // returns 2
Creating and Using an ArrayList
• To access items in an ArrayList, use the get method
nameList.get(3);

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 ]

• The ArrayList class's remove method removes designated item


from the ArrayList
nameList.remove(1);
This statement removes the second item.
• See example: ArrayListDemo3.java
Using an ArrayList
• The ArrayList class's add method with one argument adds new
items to the end of the ArrayList
• To insert items at a location of choice, use the add
method with two arguments:
nameList.add(1, "Mary");
This statement inserts the String "Mary" at index 1
• To replace an existing item, use the set method:
nameList.set(1, "Becky");
This statement replaces “Mary” with “Becky”
• See example: ArrayListDemo4.java
Using an ArrayList
• An ArrayList has a capacity, which is the number of items it
can hold without increasing its size.
• The default capacity of an ArrayList is 10 items.
• To designate a different initial capacity, use a
parameterized constructor:
ArrayList<String> list = new ArrayList<String>(100);
Using an ArrayList to hold other objects
• ArrayList can be used to hold any type of objects
Exercise
• Write a class named PhoneBookEntry that has fields for a person’s
name and phone number. The class should have a constructor and
appropriate accessor and mutator methods. Then write a program
that creates at least five PhoneBookEntry objects and stores them
in an ArrayList. Use a loop to display the contents of each object
in the ArrayList.

You might also like