Upload
phuong-le
View
234
Download
0
Embed Size (px)
Citation preview
7/30/2019 5. JAVA Arrays
1/33
Arrays, String and Other String class
7/30/2019 5. JAVA Arrays
2/33
Module Objective
Arrays
Define an array.
Explain single - dimension array
Describe two dimension array
String
Other String class
FPT- APTECH 2/33FPT- APTECH 2/33
7/30/2019 5. JAVA Arrays
3/33
#1 - Array
Introduction to Arrays
Array declaration
Initializing Array Elements
Accessing Array Elements
FPT- APTECH 3/33
7/30/2019 5. JAVA Arrays
4/33
Introduction to Arrays
Special data store that can hold several items
of a single data type in contiguous memory
location.
All the elements within an array must belongto the same data type.
Arrays are zero indexed
An array with n elements is indexed from 0 to
n-1.
Array in Java is Object
FPT- APTECH 4/33
7/30/2019 5. JAVA Arrays
5/33
Array declaration
Declaring an array variable does not create an array!
You must use new to explicitly create the array instance
The default value of numeric array elements are set to zero, and
reference elements are set to null.
FPT- APTECH 5/33
7/30/2019 5. JAVA Arrays
6/33
Array dimension
The number of indexes associated with each element
FPT- APTECH 6/33
7/30/2019 5. JAVA Arrays
7/33
Initializing Array Elements
The elements of an array can be explicitly initialized
You can use a convenient shorthand
FPT- APTECH 7/33
7/30/2019 5. JAVA Arrays
8/33
Accessing Array Elements
Supply an integer index for each dimension
Indexes are zero-based
FPT- APTECH 8/33
7/30/2019 5. JAVA Arrays
9/33
Types of Arrays
Single - Dimensional Arrays
Multi - Dimensional Arrays (matrix, arrays of arrays)
FPT- APTECH 9/33
7/30/2019 5. JAVA Arrays
10/33
Single-Dimensional Arrays
Have only one dimension and is visually
represented as having several row but a single
column of data.
FPT- APTECH 10/33
7/30/2019 5. JAVA Arrays
11/33
Working with Single-Dimensional Arrays
Store elements :
Initialize individual array elements
Use Loop to initialize
Access elements:
Using Loop
for..each loop (JDK 1.5)
FPT- APTECH 11/33
7/30/2019 5. JAVA Arrays
12/33
Example of Working with Single-Dimensional Arrays
FPT- APTECH 12/33
7/30/2019 5. JAVA Arrays
13/33
Multidimensional Arrays, (matr ix, arrays o f arrays)
[ ] [ ] = new [ rows][cols]
FPT- APTECH 13/33
7/30/2019 5. JAVA Arrays
14/33
Working with Multidimensional Arrays
FPT- APTECH 14/33
7/30/2019 5. JAVA Arrays
15/33
#2 - String class
Describe String
Methods of String class
Describe String array and its use
Describe command line arguments in Java and its use
FPT- APTECH 15/33
7/30/2019 5. JAVA Arrays
16/33
String class
The String class represents character strings.
All string literals in Java programs, such as "abc", are
implemented as instances of String class.
Strings are constant
Values cannot be changed after they are created.
String objects are immutable they can be shared.
FPT- APTECH 16/33
7/30/2019 5. JAVA Arrays
17/33
Create a String Object
FPT- APTECH 17/33
7/30/2019 5. JAVA Arrays
18/33
Example of Using String class
FPT- APTECH 18/33
7/30/2019 5. JAVA Arrays
19/33
Common-used methods
FPT- APTECH 19/33
7/30/2019 5. JAVA Arrays
20/33
String Arrays
FPT- APTECH 20/33
7/30/2019 5. JAVA Arrays
21/33
String arguments
The parameter of the main() method represents the
command-line arguments.
FPT- APTECH 21/33
7/30/2019 5. JAVA Arrays
22/33
String Command Line Arguments
A Java application can accept any number of arguments from
the operating system command line.
The purpose is to specify the configuration information for the
application.
The parameter of the main() method is a String array that
represents the command-line arguments.
FPT- APTECH 22/33
7/30/2019 5. JAVA Arrays
23/33
Example of String Command Line Arguments
FPT- APTECH 23/33
7/30/2019 5. JAVA Arrays
24/33
#3 - Other String class
StringBuilder class
StringTokenizer class
FPT- APTECH 24/33
7/30/2019 5. JAVA Arrays
25/33
StringBuilderclass
A mutable sequence of characters.
String objects are immutable sequence of characters
Character or String can be inserted in the StringBuilder object
and they can also be appended at the end.
FPT- APTECH 25/33
7/30/2019 5. JAVA Arrays
26/33
Constructors ofStringBuilderclass
FPT- APTECH 26/33
7/30/2019 5. JAVA Arrays
27/33
Methods ofStringBuilderclass
FPT- APTECH 27/33
7/30/2019 5. JAVA Arrays
28/33
Example ofStringBuilderclass
FPT- APTECH 28/33
7/30/2019 5. JAVA Arrays
29/33
StringTokenizerclass
Allows an application to break a string into tokens
The set of delimiters (the characters that separate tokens) may
be specified either at creation time or on a per-token basis.
FPT- APTECH 29/33
7/30/2019 5. JAVA Arrays
30/33
Constructor ofStringTokenizerclass
FPT- APTECH 30/33
7/30/2019 5. JAVA Arrays
31/33
Method ofStringTokenizerclass
FPT- APTECH 31/33
7/30/2019 5. JAVA Arrays
32/33
Example of Using StringTokenizerclass
FPT- APTECH 32/33
7/30/2019 5. JAVA Arrays
33/33
Thats about all for today!
Array
String class
StringBuilder class
StringTokenizer class
Thank you al l for yo ur at tent ion and pat ient!