Jump to content

Programming Fundamentals/Array Examples Java

From Wikibooks, open books for an open world

Arrays

[edit | edit source]
// This program demonstrates array processing, including:
// display, total, max, min, parallel arrays, sort,
// fixed arrays, dynamic arrays, and multidimensional arrays.

import java.util.*;

class Main {
    public static void main(String[] args) {
        String[] names = {"Lisa", "Michael", "Ashley", "Jacob", "Emily"};
        int[] ages = {49, 48, 26, 19, 16};

        displayArray(ages);

        int total = sum(ages);
        int maximum = max(ages);
        int minimum = min(ages);

        System.out.println("total: " + total);
        System.out.println("maximum: " + maximum);
        System.out.println("minimum: " + minimum);

        displayParallel(names, ages);

        Arrays.sort(ages);
        displayArray(ages);

        fixedArray();
        dynamicArray();
        displayMultidimensional();
    }

    public static void displayArray(int[] array) {
        for (int index = 0; index < array.length; index++) {
            System.out.println("array[" + index + "] = " + array[index]);
        }
    }

    public static int sum(int[] array) {
        int total = 0;
        for (int index = 0; index < array.length; index++) {
            total += array[index];
        }
        return total;
    }

    public static int max(int[] array) {
        int maximum = array[0];
        for (int index = 1; index < array.length; index++) {
            if (maximum < array[index]) {
                maximum = array[index];
            }
        }
        return maximum;
    }

    public static int min(int[] array) {
        int minimum = array[0];
        for (int index = 1; index  array[index]) {
                minimum = array[index];
            }
        }
        return minimum;
    }

    public static void displayParallel(String[] names, int[] ages) {
        for (int index = 0; index < names.length; index++) {
            System.out.println(names[index] + " is " + 
                ages[index] + " years old");
        }
    }

    public static void fixedArray() {
        int[] array = new int[5];

        for (int index = 0; index < array.length; index++) {
            int number = (int) Math.floor(Math.random() * 100);
            array[index] = number;
        }
        displayArray(array);
    }

    public static void dynamicArray() {
        ArrayList array = new ArrayList();

        for (int index = 0; index < 5; index++) {
            int number = (int) Math.floor(Math.random() * 100);
            array.add(number);
        }
        System.out.println(array);
    }

    public static void displayMultidimensional() {
        String[][] game = {
            {"X", "O", "X"}, 
            {"O", "O", "O"}, 
            {"X", "O", "X"} };

        for (int row = 0; row < 3; row++) {
            for (int column = 0; column < 3; column++) {
                System.out.print(game[row][column]);
                if (column < 2) {
                    System.out.print(" | ");
                }
            }
            System.out.println();
        }
    }
}

Output

[edit | edit source]
array[0] = 49
array[1] = 48
array[2] = 26
array[3] = 19
array[4] = 16
total: 158
maximum: 49
minimum: 16
Lisa is 49 years old
Michael is 48 years old
Ashley is 26 years old
Jacob is 19 years old
Emily is 16 years old
array[0] = 16
array[1] = 19
array[2] = 26
array[3] = 48
array[4] = 49
array[0] = 28
array[1] = 30
array[2] = 28
array[3] = 75
array[4] = 21
[56, 50, 63, 82, 15]
X | O | X
O | O | O
X | O | X

References

[edit | edit source]