Title: Chapter 6: Arrays and Strings
1Chapter 6 Arrays and Strings
- Introducing Arrays
- Declaring Arrays
- Creating Arrays
- Initializing Arrays
- Array of Objects
- Copying Arrays
- Multidimensional Arrays
- String , StringBuffer, and StringTokenizer
- Command-Line Parameters
2Introducing Arrays
- Array is a data structure that represents a
collection of the same types of data. - Java treats these arrays as objects.
- An Array of 10 Elementsof type double
double myList new double10
3Declaring Arrays
- datatype arrayname
- Example
- int myList
- datatype arrayname
- Example
- int myList
4Creating Arrays
- arrayName new datatypearraySize
- Example
- myList new double10
5Declaring and Creatingin One Step
- datatype arrayname new datatypearraySize
- double myList new double10
- datatype arrayname new datatypearraySize
- double myList new double10
6Initializing Arrays
- Using a loop
- for (int i 0 i lt myList.length i)
- myListi (double)i
- Declaring, creating, initializing in one step
- double myList 1.9, 2.9, 3.4, 3.5
7Example 6.2Using Arrays in Sorting
- Objective Use the selectionSort() method to
write a program that will sort a list of double
floating-point numbers.
TestSelectionSort
Run
8Example 6.3Testing Linear Search
- Objective Implement and test the linear search
method by creating an array of 10 elements of int
type randomly and then displaying this array.
Prompt the user to enter a key for testing the
linear search.
TestLinearSearch
Run
9Array of Objects
- Declaring and creating
- Circle circleArray new Circle10
- Initializing
- for (int i0 iltcircleArray.length i)
-
- circleArrayi new Circle()
-
10Copying Arrays
- Using a loop
- int sourceArray 2, 3, 1, 5, 10
- int targetArray new intsourceArray.length
- for (int i 0 i lt sourceArrays.length i)
- targetArrayi sourceArrayi
11The arraycopy Utility
- arraycopy(sourceArray, src_pos, targetArray,
tar_pos, length) - Example
- System.arraycopy(sourceArray, 0, targetArray, 0,
sourceArray.length)
12Multidimensional Arrays
- int matrix new int1010
- or
- int matrix new int1010
- for (int i0 iltmatrix.length i)
- for (int j0 jltmatrixi.length j)
-
- matrixij (int)(Math.random()1000)
-
13Example 6.7Testing Multidimensional Arrays
- Objective Use two-dimensional arrays to create
two matrices, and then add the two matrices.
TestMatrixAddition
Run
14The String Class
- Declaring a String
- String message "Welcome to Java!"
- String s new String()
- String Comparisons
- String Concatenation
- Substrings
- String Length
- Retrieving Individual Charactersin a String
15String Comparisons
- equals
- String s1 "Welcome"
- String s2 "welcome"
- if (s1.equals(s2))
- ...
16Substrings
- String is an immutable class its valuescannot
be changed individually. - String s1 "Welcome to Java"
- String s2 s1.substring(0,10) "HTML"
17Finding String Length
- Finding string length using the length() method
- message "Welcome"
- message.length() (returns 7)
18Retrieving Individual Characters in a String
- Do not use message0
- Use message.charAt(index)
- Index starts from 0
19The StringBuffer Class
- The StringBuffer class is an alternative to the
String class. In general, a string buffer can be
used wherever a string is used.StringBuffer is
more flexible than String. You can add, insert,
or append new contentsinto a string buffer.
However, the value ofa string is fixed once the
string is created.
20StringBuffer Constructors
- public StringBuffer()
- No characters, initial capacity 16 characters.
- public StringBuffer(int length)
- No characters, initial capacity specified by the
length argument. - public StringBuffer(String str)
- Represents the same sequence of charactersas
the string argument. Initial capacity 16plus the
length of the string argument.
21Appending New Contentsinto a String Buffer
- StringBuffer strBuf new StringBuffer()
- strBuf.append("Welcome")
- strBuf.append(' ')
- strBuf.append("to")
- strBuf.append(' ')
- strBuf.append("Java")
22The StringTokenizer Class Constructors
- StringTokenizer(String s, String delim, boolean
returnTokens) - StringTokenizer(String s, String delim)
- StringTokenizer(String s)
23The StringTokenizer Class Methods
- boolean hasMoreTokens()
- String nextToken()
- String nextToken(String delim)
24Example 6.9Testing StringTokenizer
- Objective Using a string tokenizer, retrieve
words from a string and display them on the
console.
TestStringTokenizer
Run
25Command-Line Parameters
- class TestMain
-
- public static void main(String args)
- ...
-
- java TestMain arg0, arg1, arg2, ..., argn
26ProcessingCommand-Line Parameters
- In the main method, get the arguments from
args0, args1, ..., argsn, which corresponds
to arg0, arg1, ..., argn in the command line.
27Example 6.10Using Command-Line Parameters
- Objective Write a program that will perform
binary operations on integers. The program
receives three parameters an operator and two
integers.
TestCommandParameters
Run
Java TestCommandParameters 2 3
Run
Java TestCommandParameters - 2 3
Run
Java TestCommandParameters / 2 3