Java Arrays: Real-Life Examples

Understanding how arrays are used in practical applications can help solidify the concept. Below are examples demonstrating real-life use cases of arrays.

1. Managing Student Grades

public class StudentGrades {
    public static void main(String[] args) {
        String[] students = {"Alice", "Bob", "Charlie"};
        int[] grades = {85, 92, 78};

        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i] + " scored " + grades[i]);
        }
    }
}

2. Inventory Management

public class Inventory {
    public static void main(String[] args) {
        String[] products = {"Laptop", "Smartphone", "Tablet"};
        int[] stock = {50, 100, 75};

        for (int i = 0; i < products.length; i++) {
            System.out.println("Product: " + products[i] + ", Stock: " + stock[i]);
        }
    }
}

3. Weather Data Analysis

public class WeatherAnalysis {
    public static void main(String[] args) {
        double[] temperatures = {72.5, 75.0, 68.4, 70.2, 69.8};
        double sum = 0;

        for (double temp : temperatures) {
            sum += temp;
        }

        double average = sum / temperatures.length;
        System.out.println("Average Temperature: " + average);
    }
}

Key Takeaways

  • Arrays are essential for managing collections of related data.
  • They are widely used in various applications like data analysis, inventory management, and more.
  • Understanding practical examples helps in applying arrays effectively.