Miscellaneous Flashcards
What is the ‘instanceof’ keyword for?
You can use it to check the type of objects, like this:
if (object instanceof String)
System.out.print(“object is a string”);
How can you read the input: “10 2 4 5 10” ?
Can you use input.nextInt(); ?
You can’t use .nextInt(), it will only get the first integer.
You CAN use input.nextLine(), which will read everything into a single string. You can later convert into integers if you wish
How do you convert from String to int?
Integer.parseInt(str). For example:
int number = Integer.parseInt(“333”);
What is the syntax of a ‘for-each’ loop?
for (double element: myList) {
System.out.println(element);
}
Iterating through a list like this is easier if all you need is the elements in the list.