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: Continue reading Java How To Loop Through a HashMap

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);
    }
  }
}

Continue reading Java How To Loop Through an ArrayList

Java How To Find the Smallest Element in an Array

How To Find the Smallest Element in an Array

Create a program that finds the lowest age among different ages:

Example

// An array storing different ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};

// Create a 'lowest age' variable and assign the first array element of ages to it
int lowestAge = ages[0];

// Loop through the elements of the ages array to find the lowest age
for (int age : ages) {
  // Check if the current age is smaller than the current 'lowest age'
  if (lowestAge > age) {
    // If the smaller age is found, update 'lowest age' with that element
    lowestAge = age;
  }
}

// Output the value of the lowest age
System.out.println("The lowest age in the array is: " + lowestAge);

Continue reading Java How To Find the Smallest Element in an Array

Java How To Find the Average of Array Elements

How To Calculate the Average of Array Elements

Create a program that calculates the average of different ages:

Example

// An array storing different ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};

float avg, sum = 0;

// Get the length of the array
int length = ages.length;

// Loop through the elements of the array
for (int age : ages) {
  sum += age;
}

// Calculate the average by dividing the sum by the length
avg = sum / length;

// Print the average
System.out.println("The average age is: " + avg);

 

Continue reading Java How To Find the Average of Array Elements

Java How To Sort an Array

How To Sort an Array

You can use the sort() method, found in java.util.Arrays, to sort an array:

Example

import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    String[] cars = {"Volvo", "BMW", "Tesla", "Ford", "Fiat", "Mazda", "Audi"};
    Arrays.sort(cars);
    for (String i : cars) {
      System.out.println(i);
    }
  }
}

Continue reading Java How To Sort an Array

Java How To Convert a String to an Array

Convert a String to an Array

There are many ways to convert a string to an array. The simplest way is to use the toCharArray() method:

Example

Convert a string to a char array:

// Create a string
String myStr = "Hello";

// Convert the string to a char array
char[] myArray = myStr.toCharArray();

// Print the first element of the array
System.out.println(myArray[0]);

You can also loop through the array to print all array elements:

Example

// Create a string
String myStr = "Hello";

// Convert the string to a char array
char[] myArray = myStr.toCharArray();

// Print array elements
for (char i : myArray) {
  System.out.println(i);
}

Continue reading Java How To Convert a String to an Array

Java How To Calculate the Sum of Elements

Calculate the Sum of an Array

Get the sum of array elements:

Example

int[] myArray = {1, 5, 10, 25};
int sum = 0;
int i; 

// Loop through the array elements and store the sum in the sum variable
for (i = 0; i < myArray.length; i++) {
  sum += myArray[i];
}

System.out.println("The sum is: " + sum);

Continue reading Java How To Calculate the Sum of Elements

Java How To Reverse a String

Reverse a String

You can easily reverse a string by characters with the following example:

Example

String originalStr = "Hello";
String reversedStr = "";

for (int i = 0; i < originalStr.length(); i++) {
  reversedStr = originalStr.charAt(i) + reversedStr;
}

System.out.println("Reversed string: "+ reversedStr);

Continue reading Java How To Reverse a String

Java How To Count Words

Count Number of Words in a String

You can easily count the number of words in a string with the following example:

Example

String words = "One Two Three Four";
int countWords = words.split("\\s").length;
System.out.println(countWords);

Continue reading Java How To Count Words

Java How To Add Two Numbers

Add Two Numbers

Learn how to add two numbers in Java:

Example

int x = 5;
int y = 6;
int sum = x + y;
System.out.println(sum); // Print the sum of x + y

Continue reading Java How To Add Two Numbers