Rakshitha R’s Post

Arrays in Java 🧠 Concept: An Array in Java is a container object that holds a fixed number of elements of the same data type. Each element can be accessed using an index number, starting from 0. Arrays are static in size, meaning once created, their length cannot be changed. They can store primitive data types (like int, double, char) or objects (like String, Student, etc.). Types of Arrays in Java: 1. Single-Dimensional Array – Stores elements in a single row. 2. Multi-Dimensional Array – Stores data in rows and columns (like a matrix). 3. Jagged Array – A multi-dimensional array with rows of different lengths. 💡 Why it matters: Arrays are widely used in real-world applications such as: Student result systems (storing marks of multiple subjects) E-commerce apps (storing product prices or stock quantities) Sensor data processing (reading multiple sensor values at once) Games (storing scores, levels, or positions) They make data handling simple, structured, and efficient. Example / Snippet: 1. Single-Dimensional Array public class SingleArrayExample { public static void main(String[] args) { int[] scores = {90, 85, 88, 95, 100}; // Access and display for (int i = 0; i < scores.length; i++) { System.out.println("Score " + (i+1) + ": " + scores[i]); } } } Here, the array scores stores 5 integer values, accessible using indices 0 to 4. 2. Multi-Dimensional Array public class MultiArrayExample { public static void main(String[] args) { int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Printing matrix elements for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } } This represents a 3x3 matrix — useful in mathematical or graphical applications. 3. Jagged Array (Irregular Rows) public class JaggedArrayExample { public static void main(String[] args) { int[][] numbers = { {1, 2, 3}, {4, 5}, {6, 7, 8, 9} }; for (int i = 0; i < numbers.length; i++) { for (int j = 0; j < numbers[i].length; j++) { System.out.print(numbers[i][j] + " "); } System.out.println(); } } } Jagged arrays are used when data rows have varying lengths — for example, storing monthly sales data for stores with different transaction counts. #Java #CoreJava #JavaProgramming #LearnJava #JavaDeveloper #CodingInJava #ArraysInJava #DataStructures #ProgrammingBasics #SoftwareDevelopment #TechLearning #CodingJourney

To view or add a comment, sign in

Explore content categories