Han

Han Ferik

Home | Projects | AP CSA

Project Memory Card

Tools: VSCode | Source Code: GitHub

The ArrayProject class demonstrates various operations on two-dimensional arrays in Java, focusing on both String and int arrays. It includes methods for printing array contents, modifying specific elements, counting and summing values based on conditions, and traversing a matrix in a spiral order. The program allows users to manipulate data stored in arrays efficiently while ensuring proper boundary checks and structured iteration.

The printArray method prints the contents of a two-dimensional String array. It iterates through each row, accessing each element and printing it in a structured format that maintains the array's appearance. This method provides a clear representation of the data, making it useful for checking values before and after modifications.

The changeName method allows modifying a specific element in a two-dimensional String array by specifying the row and column indices along with the new value. Before making the change, it checks if the indices are within valid bounds. If they are, the method updates the value at the given position; otherwise, it prints an error message. This ensures that modifications do not cause runtime errors due to invalid indices.

The countItems method processes a two-dimensional integer array and counts how many numbers are greater than a given threshold. By iterating through each element, it keeps a counter that increments when a number exceeds the specified maximum value. This function is helpful for determining how many elements meet a certain condition, making it applicable in data filtering scenarios.

The totalItems method follows a similar approach but instead sums all numbers that are greater than the specified maximum value. Instead of counting occurrences, it accumulates the values of the qualifying elements. This method is useful in cases where the total of specific elements is more important than their count.

The spiralOrder method performs a structured traversal of a two-dimensional integer array, following a spiral pattern. It starts from the top-left corner and moves to the right across the first row. Then, it moves downward along the rightmost column, followed by a leftward movement across the bottom row. Finally, it moves upward along the leftmost column before repeating the process inward until all elements are visited. This technique is commonly used in matrix-related problems and has applications in computer graphics and data processing.

The main method initializes example arrays and demonstrates each method's functionality. It first creates a two-dimensional String array representing names and an integer array representing ages. The printArray method is called to display the names before and after modifying one value using changeName. The program then tests the integer-based methods by applying countItems and totalItems on a sample matrix, showing how many numbers exceed a given threshold and their sum. Finally, the spiralOrder method is used to print the numbers in the matrix following a spiral traversal.

This program effectively demonstrates essential array operations, including searching, modifying, filtering, and traversal techniques. By applying these methods, users can gain a better understanding of working with two-dimensional arrays in Java, which are commonly used in fields such as data analysis, image processing, and algorithm development.


ArrayProject Java Code

ArrayProject Java Code


    public class ArrayProject {
        public void printArray(String[][] arrayExample) {
            System.out.println("2D Array contents:");
            for (String[] row : arrayExample) { 
                for (String value : row) {  
                    System.out.print(value + " ");  
                }
                System.out.println();
            }
        }
    
        public static void changeName(String[][] array, int row, int col, String newName) {
            if ((row >= 0 && row < array.length) && (col >= 0 && col < array[row].length)) {
                array[row][col] = newName;
            } else {
                System.out.println("Invalid index.");
            }
        }
    
        public static int countItems(int[][] arr, int max) {
            int count = 0;
            for (int[] row : arr) {
                for (int num : row) {
                    if (num > max) {
                        count++;
                    }
                }
            }
            return count;
        }
    
        public static int totalItems(int[][] arr, int max) {
            int sum = 0;
            for (int[] row : arr) {
                for (int num : row) {
                    if (num > max) {
                        sum += num;
                    }
                }
            }
            return sum;
        }
    
        public static int[] spiralOrder(int[][] arr) {
            if (arr == null || arr.length == 0) return new int[0];
    
            int rows = arr.length;
            int cols = arr[0].length;
            int[] result = new int[rows * cols];
    
            // Spiral traversal implementation
            int left = 0, right = cols - 1;
            int top = 0, bottom = rows - 1;
            int index = 0;
    
            while (left <= right && top <= bottom) {
                // Traversing in a spiral pattern
            }
    
            return result;
        }
    
        public static void main(String[] args) {
            ArrayProject project = new ArrayProject();
            // Initializing arrays and demonstrating methods
        }
    }
        

Find me on the interwebs!

Github LinkedIn Instagram Facebook