Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Java Arrays: Definition, Traversal, and Common Operations

Notes May 8 3

Arrays are containers that store multiple values of the same data type.

Array Declaration and Initialization

Static Initialization - Full Syntax

dataType[] arrayName = new dataType[]{element1, element2, element3};

Example:

int[] numbers = new int[]{11, 22, 33};

Static Initialization - Simplified Syntax

int[] numbers = {11, 22, 33};

Dynamic Initialization

Initializes array length with system-provided default values.

dataType[] arrayName = new dataType[arrayLength];

Example:

int[] values = new int[3];

Default Initialization Values

  • Integer types: 0
  • Floating-point types: 0.0
  • Character type: '\u0000' (null chaarcter)
  • Boolean type: false
  • Reference types: null

Array Travresal

public static void main(String[] args) {
    int[] data = {1, 2, 3, 4, 5};
    for (int index = 0; index < data.length; index++) {
        System.out.println(data[index]);
    }
}

Finding Maximum Value in Array

public class ArrayOperations {
    public static void main(String[] args) {
        int[] numbers = {33, 5, 22, 44, 55};
        int maximum = numbers[0];
        
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > maximum) {
                maximum = numbers[i];
            }
        }
        System.out.println("Maximum value: " + maximum);
    }
}

Array Summation and Statistical Analysis

import java.util.Random;

public class ArrayStatistics {
    public static void main(String[] args) {
        int[] randomNumbers = new int[10];
        Random generator = new Random();
        
        for (int i = 0; i < randomNumbers.length; i++) {
            randomNumbers[i] = generator.nextInt(100) + 1;
        }
        
        double total = 0;
        for (int i = 0; i < randomNumbers.length; i++) {
            System.out.print(randomNumbers[i] + " ");
            total += randomNumbers[i];
        }
        
        System.out.println("\nSum: " + total);
        
        double average = total / randomNumbers.length;
        System.out.println("Average: " + average);
        
        int belowAverageCount = 0;
        for (int i = 0; i < randomNumbers.length; i++) {
            if (randomNumbers[i] < average) {
                belowAverageCount++;
            }
        }
        System.out.println("Numbers below average: " + belowAverageCount);
    }
}

Sample Output:

46 62 85 20 10 54 43 88 3 13 
Sum: 424.0
Average: 42.4
Numbers below average: 4

Array Shuffling Algorithm

import java.util.Random;

public class ArrayShuffler {
    public static void main(String[] args) {
        int[] sequence = {1, 2, 3, 4, 5};
        Random randomizer = new Random();
        
        for (int currentIndex = 0; currentIndex < sequence.length; currentIndex++) {
            int swapIndex = randomizer.nextInt(sequence.length);
            int temporary = sequence[currentIndex];
            sequence[currentIndex] = sequence[swapIndex];
            sequence[swapIndex] = temporary;
        }
        
        for (int i = 0; i < sequence.length; i++) {
            System.out.print(sequence[i] + " ");
        }
    }
}

Sample Output:

1 3 5 2 4 
Tags: JavaArrays

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.