1. Trang chủ
  2. » Công Nghệ Thông Tin

Chapter 8 Arrays and the arraylist class 4

16 9 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Arrays And The Arraylist Class
Trường học https://www.exampleuniversity.edu
Chuyên ngành Computer Science
Thể loại Lecture Note
Năm xuất bản 2023
Định dạng
Số trang 16
Dung lượng 230,21 KB
File đính kèm Chapter 8 - Arrays and the ArrayList Class - 4.rar (213 KB)

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Chapter 8 Chapter 8 Arrays and the ArrayList Class The ArrayList Class 2 Contents I The ArrayList Class II Creating and Using an ArrayList Object III The ArrayList Classs toString method IV Removing. ngôn ngữ java lập trình java học lập trình tai lieu java lập trình code

Trang 1

Chapter 8

Arrays and the

ArrayList Class

The ArrayList Class

Trang 2

Contents

I The ArrayList Class

II Creating and Using an ArrayList Object

III The ArrayList Class's toString method

IV Removing an Item from an ArrayList

V Inserting an Item

VI Replacing an Item

VII Capacity

VIII Using a Cast Operator with the get Method

IX Using ArrayList As a Generic Data Type

Trang 3

I The ArrayList Class

The ArrayList class can be used for storing

and retrieving objects

An ArrayList object is similar to an array of

objects, but offers many advantages over an

array:

An ArrayList object automatically expands as items are added to it

In addition to adding items to an ArrayList, we can remove items as well

An ArrayList object automatically shrinks as

items are removed from it

Trang 4

II Creating and Using an

ArrayList Object

Creating an ArrayList object

ArrayList nameList = new ArrayList();

Adding items to the ArrayList object

nameList.add(“James”);

nameList.add(“Catherine”);

nameList.add(“Bill”);

The items that are stored in an ArrayList have

a corresponding index

The first item that is added to an ArrayList is stored at index 0 The next item that is added to

an ArrayList is stored at index 1 and so forth

Trang 5

II Creating and Using an

ArrayList Object

To get the number of items stored in an

ArrayList: using the size() method

The ArrayList class's get method returns the item stored at a specific index

We pass the index as an argument to the method

Trang 6

6

Trang 7

III The ArrayList Class's

toString method

The ArrayList class has a toString method that returns a string representing all of the items stored in an ArrayList object

ArrayList nameList = new ArrayList();

nameList.add(“James”);

nameList.add(“Catherine”);

nameList.add(“Bill”);

System.out.println(nameList);

[James, Catherine, Bill]

Trang 8

IV Removing an Item from an

ArrayList

The ArrayList class has a remove method

that removes an item at a specific index

Trang 9

V Inserting an Item

Trang 10

V Inserting an Item

The add method adds an item at the last position

in an ArrayList object

nameList.add(“Marry”);

The ArrayList class has an overloaded version

of the add method that allows to add an item at a specific index

nameList.add(1, “Marry”);

Trang 11

VI Replacing an Item

The ArrayList class's set method can be used

to replace an item at a specific index with another item

nameList.set(1, “Becky”);

Trang 12

VII Capacity

An ArrayList object has a capacity, which is

the number of items it can store without having to increase its size

When an ArrayList object is created, using the no-argument constructor, it has an initial capacity

of 10 items

We can specify a different starting by passing an int value to the constructor:

ArrayList nameList = new ArrayList(100);

Trang 13

VIII Using a Cast Operator with

the get Method

The get method returns a reference to the object stored at a specific index

We have to use a cast operator to convert the

reference to the correct type manually

ArrayList nameList = new ArrayList();

nameList.add(“James”);

String str = (String)nameList.get(0);

Trang 14

IX Using ArrayList As a

Generic Data Type

We can specify the type of object that an

ArrayList will store, and Java will make sure

that only objects of the specified type are stored in it

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

Trang 15

15

Ngày đăng: 27/09/2022, 22:23

TỪ KHÓA LIÊN QUAN