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

Chapter 8 Arrays and the arraylist class 2

18 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

Định dạng
Số trang 18
Dung lượng 231,63 KB
File đính kèm Chapter 8 - Arrays and the ArrayList Class - 2.rar (218 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 Arrays of Objects 2 Contents I String Arrays II Arrays of Objects III Command Line Arguments and Variable Length Argument Lists 3 I String Arrays 1 C.

Trang 1

Chapter 8

Arrays and the

ArrayList Class

Arrays of Objects

Trang 2

I String Arrays

II Arrays of Objects

III Command-Line Arguments and

Variable-Length Argument Lists

Trang 3

I String Arrays

1 Creating String Arrays

2 Calling String Methods from an Array

Element

Trang 4

I.1 Creating String Arrays

In memory, an array of String objects is

arranged differently than an array of a primitive

data type

An array of String objects is really an array of references to String objects

Creating an array of String objects initialized

with values:

String[] names = {“Bill”, “Susan”, “Steven” };

Trang 5

I.1 Creating String Arrays

address address

“Bill” “Susan” “Steven”

The names variable holds the

address of a String array

The String array is

an array of references

to String objects

Trang 6

I.1 Creating String Arrays

Creating an initialized array of String objects:

String[] names = new String[3];

null null

The names variable holds the address of a String array

The String array is

an array of references

to String objects

Trang 7

I.1 Creating String Arrays

When we create an initialized array of String

objects, we must assign a value to each element

in the array that we intend to use

String[] names = new String[3];

names[0] = “Bill”;

names[1] = “Susan”;

names[2] = “Steven”;

Trang 8

I.2 Calling String Methods from

an Array Element

Each element of a String array is a String

object, we can use an element to call a String

method

 //Declare a char variable named letter

char letter;

//Assign the first character in names[3] to letter.

An element of the names array

Trang 9

I.2 Calling String Methods from

an Array Element

The following loop displays the length of each

string held in names:

for(int i = 0; i < names.length; i++)

System.out.println(names[i].length());

Trang 10

8.15

a Write a statement that declares a String array initialized with the following strings: “Mercury”,

“Venus”, “Earth”, and “Mars”

b Write a loop that displays the contents of each element in the array you declared in a

c Write a loop that displays the first character of the strings stored in each element of the array you declared in a

Trang 11

II Arrays of Objects

Like any other data type, we can create arrays of class objects

BankAccount[] accounts = new BankAccount[5];

The variable that references the array is named accounts

As with String arrays, each element in this

array is a reference variable

Trang 12

II Arrays of Objects

We must individually create the objects that each element will reference

for(int index = 0; index < accounts.length; index++)

accounts[index] = new BankAccount();

Trang 13

8.16 Write a Rectangle class Write code that declares a Rectangle array with five elements Instantiate each element with a Rectangle

object Use constructor to initialize each object

with values for the length and width fields

Trang 14

III Command-Line Arguments and Variable-Length Argument Lists

1 Command-Line Arguments

2 Variable-Length Argument Lists

Trang 15

III.1 Command-Line Arguments

Consider the main method's header:

public static void main(String[] args)

The parameter args is an reference to an arry of String

The array that is passed into the args parameter comes from the operating system command-line

Trang 16

III.1 Command-Line Arguments

/**

This program displays the arguments passed to it from the operating system command line.

*/

public class CommandLine

{

public static void main(String[] args)

{

for(int index; index < args.length; index++)

System.out.println(args[index]);

}

}

 Compile and the execute with the following command:

java CommandLine Good morning

Trang 17

III.2 Variable-Length Argument

Lists

Java provides a mechanism known as

variable-length argument lists.

We can write a method that takes a variable

number of arguments

The method that accepts any number of

arguments when it is called

The parameter is known as a vararg parameter.

int sum( int numbers )

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