Tag java 17

Java How To Generate Random Numbers

How To Generate a Random Number You can use Math.random() method to generate a random number. Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive):

Java How To Get the Area of a Rectangle

Area of Rectangle The area of a rectangle can be found by multiplying the length of the rectangle by the width: Example int length = 5; int width = 2; int area = length * width; System.out.println(“Area of rectangle: “…

Java How To Loop Through an Enum

Loop Through an Enum The enum type has a values() method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum: Example enum Level { LOW, MEDIUM,…

Java How To Loop Through a HashMap

Loop Through a HashMap Loop through the items of a HashMap with a for-each loop. Note: Use the keySet() method if you only want the keys, and use the values() method if you only want the values:

Java How To Loop Through an ArrayList

Loop Through an ArrayList Loop through the elements of an ArrayList: Example public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add(“Volvo”); cars.add(“BMW”); cars.add(“Ford”); cars.add(“Mazda”); for (String i : cars) { System.out.println(i); } }…