exam Flashcards
What is a return type in a method?
Example: int, double, String, boolean, etc. If the method does not return any value, the return type is void.
syntax of declaring a method:
public void methodName(parameters) { // method body }
.length method:
The .length method is used to get the length of arrays and strings in Java.
.substring() method:
The .substring() method is used to extract a substring from a string.
.toUpperCase() method:
The .toUpperCase() method is used to convert all characters in a string to uppercase.
.toLowerCase() method:
The .toLowerCase() method is used to convert all characters in a string to lowercase.
.equals() method:
The .equals() method is used to compare the contents of two strings.
.indexOf() method:
The .indexOf() method is used to find the index of a specified character or substring within a string.
How to create an ArrayList?
ArrayList<Type> listName = new ArrayList<Type>();</Type></Type>
Adding elements to an ArrayList:
Elements can be added to an ArrayList using the add() method:
Getting the size of an ArrayList:
the size() method:
Accessing elements in an ArrayList:
get() method and specifying the index:
Checking if an ArrayList contains an element:
using the contains() method:
Syntax of a for loop:
for (initialization; condition; update) {
Nested for loops:
You can have one or more for loops inside another for loop.
Useful for iterating over multi-dimensional arrays or performing repetitive tasks.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
What is a while loop in Java?
A while loop in Java is a control flow statement that repeatedly executes a block of code as long as a condition is true.
while loops syntax
while (condition) {
Difference between for and while loops:
For loops are used when the number of iterations is known.
While loops are used when the number of iterations is not known beforehand or when looping based on a condition.
When to use while loops:
While loops are often used when you want to repeat a block of code until a specific condition is met, such as reading input until the user enters a certain value.
Arrays:
An array in Java is a fixed-size data structure that stores elements of the same type sequentially in memory.
arrays example
int[] numbers = new int[5];
Accessing Elements:
Elements in an array are accessed using an index.
Accessing elements using a for loop for array
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
ArrayList:
ArrayList is a dynamic array implementation of the List interface in Java that allows resizing and manipulation of elements.
array list of Declaration:
ArrayList<Integer> list = new ArrayList<>();</Integer>