- String.format and printf both produce a format string
- A format string is a string with placeholders inserted, the placeholders indicate what type
of value should be inserted into the format string
- Example: %s is a placeholder for a String
- Format strings can also be used to control decimal place formatting, notice that the
placeholder for floating point numbers is %f
IndexOf and lastIndexOf:
1. Parameters: ch a character
Returns: the index of the first occurrence of the character in the character sequence
represented by this object, or -1 if the character does not occur
Arrays Java
- An array is a set of variables with the same name and same data type
- It is located contiguously in the memory
- Each element can be accessed by the name and index
- First element is always at 0
- Error runs out of bounds
- C++ does not do bound checking
- Arrays are continuous blocks of memory
- Syntax declaration: <data_type>[] <array_name>;
- Syntax instantiation: new <data_type>[<size>];
- Example: double[] highTemps = new double [31];
Int [] values = {1, 2, 3, 4, 5};
CSE 205 Lecture Notes
- Syntax 2D declaration: <data_type> [] [] <array_name>;
- Syntax 2D instantiation: new <data_type> [<size_1>][<size_2>];
- Example code:
int[] arr = {{1,2}, {3,4}, {5,6}, {7,8}};
for (int r = 0; r < arr.length; r++) {
for (int c = 0; c<arr[r].length; c++){
System.out.printf(“%d”, arr[r][c]);
}
System.out.println();
}
}
}
- Go over notes/zybook because memorizing syntax is very helpful
- Go over ArrayList methods in lecture notes
Section 4 Zybook
- Parameter variables: A variable of a method that is initialized with a value when the
method is called
- Body : all statements of a method or block
- Static method declaration: public static returnType methodName(parameterType
parameterName) {
method body
}
- Example: public static double cubeVolume(double sideLength)
Section 7-
-Chapter Goals: to collect elements using arrays and array lists, to use enhanced for loops
for traversing arrays and array lists, to learn common algorithms for processing arrays
and array lists, to work with 2D arrays
-The number of elements in an array is called your length
-The new operator constructs your array
-Example: double[] values = new double[10]
-In the above example, values is now initialized with an array of 10 numbers
-When you declare an array, you can specify your values, for example: double values[] =
{32, 47, 29, 35, 115};
-When you supply your initial values, you do not use the new operator
-To access a value within your array, you specify which slot with the [] operator
-For example: values [3] = 35
-Your slot is considered your index
-if values has ten elements, you are not allowed to access values[20]
-Bounds error: trying to access an array element that is outside the legal range
-This following code ensures that you can only access the array when the index variable i
is within legal bounds
- if (0 <= i && i < values.length) { values[i] = value; }
-For example, this loop displays all elements within the array
- for (int i = 0; i < 10; i++){
System.out.println(values[i]);}