Presentation is loading. Please wait.

Presentation is loading. Please wait.

Switch, Strings, and ArrayLists in Java

Similar presentations


Presentation on theme: "Switch, Strings, and ArrayLists in Java"— Presentation transcript:

1 Switch, Strings, and ArrayLists in Java
CS102 Written by Robert Carver Edited by Dean Zeller

2 Switch vs Else-If Statements
These are equivalent statements:

3 Switch General Form: switch(variable) case (value): (statements) break
A switch statement includes cases based on the value of a specific variable. Each case is assigned to a different value and has statements it will execute as well as a break command. In most switch statements there is a default case that executes if none of the other cases execute. switch(variable) case (value): (statements) break default:

4 Strings – defining, +, length
Defining Strings: A String variable is defined as String (name). The string does not need content upon being defined. String concatenation: String objects can be concatenated to form other String objects. String string3 = string1 + string 2; String length: string3.length() returns the amount of characters in string3 as an int value.

5 Strings – Escape Characters
Common Escape Characters: \b Inserts a backspace \t Inserts a horizontal tab \n Inserts a newline \f Inserts a form \r Inserts a carriage return \” Inserts a double quote (“) \’ Inserts a single quote (‘) \\ Inserts a backslash (\) Escape characters are symbols that we cannot normally type into a string because of formatting reasons.

6 Strings – charAt, compareTo
charAt method: Returns a character at the supplied string index. Format: string.charAt(i) compareTo method: Compares strings based on the Unicode value of each character of the string, and returns a value. if s1 > s2, it returns a positive number   if s1 < s2, it returns a negative number   if s1 == s2, it returns 0   Format: s1.compareTo(s2)

7 Strings – concat and equals
concat method: The concat method concatenates two strings together. Format: string1 = string1.concat(string2) equals method: The equals method compares two strings and returns a Boolean value. Is case sensitive, equalsIgnoreCase is not. Format: Boolean value = string1.equals(string2)

8 Strings – indexOf, toUpper, toLower
indexOf method: Returns the index of the first occurrence of a character in a string as an int. Format: string.indexOf(char) lastIndexOf method: Returns the index of the last occurrence of a character in a string as an int. Format: string.lastIndexOf(char) toUpper and toLower methods: Changes the characters of a string to upper or lower case Format: string.toUpperCase(), string.toLowerCase()

9 Strings – replace and subString
replace method: Replaces the selected chars, or char sequence, in a string with another char or char sequence. Format: string.replace(char1,char2) subString method: Selects chars past a give index and returns the new substring. Format: string.substring(index) Creates a new substring selection in the given range. Format: string.substring(index1, index2)

10 Strings – trim trim method
The trim method removes any trailing or leading whitespaces from a string. Format: string.trim()

11 ArrayList – defining, get, and set
See example file for implementations of these methods with arrays. Declaration: List<type> name = new ArrayList<type>() Item retrieval: name.get(index) , returns item from supplied index. Item reassignment: name.set(index, content), assigns item at index to new content.

12 ArrayList – remove, size, contains
See example file for implementations of these methods with arrays. Item removal: name.remove(index), removes item at supplied index. List Size: name.size(), returns int value of size of list. Contains method: name.contains(search), searches the list for the supplied item and returns a Boolean value.

13 ArrayList – indexOf, isEmpty
See example file for implementations of these methods with arrays. indexOf: name.indexOf(search), searches the list for the supplied item and returns the index of the item as an int. isEmpty: name.isEmpty(), determines if list contains any elements. Returns a Boolean value.

14 Credits Content and visual organization by Robert Carver
Concept and final editing by Dean Zeller Reference: Java An Introduction to Problem Solving, (8th edition) by Walter Savitch


Download ppt "Switch, Strings, and ArrayLists in Java"

Similar presentations


Ads by Google