Chapter 6: Arrays and Strings - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Chapter 6: Arrays and Strings

Description:

You can add, insert, or append new contents. into a string buffer. However, the value of ... Appending New Contents. into a String Buffer. StringBuffer strBuf ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 28
Provided by: yda1
Category:

less

Transcript and Presenter's Notes

Title: Chapter 6: Arrays and Strings


1
Chapter 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

2
Introducing 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
3
Declaring Arrays
  • datatype arrayname
  • Example
  • int myList
  • datatype arrayname
  • Example
  • int myList

4
Creating Arrays
  • arrayName new datatypearraySize
  • Example
  • myList new double10

5
Declaring and Creatingin One Step
  • datatype arrayname new datatypearraySize
  • double myList new double10
  • datatype arrayname new datatypearraySize
  • double myList new double10

6
Initializing 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

7
Example 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
8
Example 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
9
Array of Objects
  • Declaring and creating
  • Circle circleArray new Circle10
  • Initializing
  • for (int i0 iltcircleArray.length i)
  • circleArrayi new Circle()

10
Copying 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

11
The arraycopy Utility
  • arraycopy(sourceArray, src_pos, targetArray,
    tar_pos, length)
  • Example
  • System.arraycopy(sourceArray, 0, targetArray, 0,
    sourceArray.length)

12
Multidimensional 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)

13
Example 6.7Testing Multidimensional Arrays
  • Objective Use two-dimensional arrays to create
    two matrices, and then add the two matrices.

TestMatrixAddition
Run
14
The 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

15
String Comparisons
  • equals
  • String s1 "Welcome"
  • String s2 "welcome"
  • if (s1.equals(s2))
  • ...

16
Substrings
  • String is an immutable class its valuescannot
    be changed individually.
  • String s1 "Welcome to Java"
  • String s2 s1.substring(0,10) "HTML"

17
Finding String Length
  • Finding string length using the length() method
  • message "Welcome"
  • message.length() (returns 7)

18
Retrieving Individual Characters in a String
  • Do not use message0
  • Use message.charAt(index)
  • Index starts from 0

19
The 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.

20
StringBuffer 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.

21
Appending New Contentsinto a String Buffer
  • StringBuffer strBuf new StringBuffer()
  • strBuf.append("Welcome")
  • strBuf.append(' ')
  • strBuf.append("to")
  • strBuf.append(' ')
  • strBuf.append("Java")

22
The StringTokenizer Class Constructors
  • StringTokenizer(String s, String delim, boolean
    returnTokens)
  • StringTokenizer(String s, String delim)
  • StringTokenizer(String s)

23
The StringTokenizer Class Methods
  • boolean hasMoreTokens()
  • String nextToken()
  • String nextToken(String delim)

24
Example 6.9Testing StringTokenizer
  • Objective Using a string tokenizer, retrieve
    words from a string and display them on the
    console.

TestStringTokenizer
Run
25
Command-Line Parameters
  • class TestMain
  • public static void main(String args)
  • ...
  • java TestMain arg0, arg1, arg2, ..., argn

26
ProcessingCommand-Line Parameters
  • In the main method, get the arguments from
    args0, args1, ..., argsn, which corresponds
    to arg0, arg1, ..., argn in the command line.

27
Example 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
Write a Comment
User Comments (0)
About PowerShow.com