1.
Arrays in Java
An array is a collection of elements of the same data type stored in contiguous memory
locations. In Java, arrays are objects, so they are created dynamically using the new keyword.
Types of Arrays:
1. One-dimensional Array: A single row of elements.
2. Multi-dimensional Array: An array with more than one row or column, such as a 2D
array.
2. One-Dimensional Array
Syntax of Array Declaration and Initialization:
// Declaration and Initialization
int[] arr = new int[5]; // array of 5 integers
arr[0] = 10; // Assigning value to the first element
Alternatively, arrays can be initialized when declared:
int[] arr = {10, 20, 30, 40, 50}; // Direct initialization
Accessing Elements of an Array:
Array elements are accessed using their index, starting from 0.
Example:
int[] arr = {10, 20, 30, 40, 50};
System.out.println(arr[0]); // Output: 10
System.out.println(arr[2]); // Output: 30
Traversing an Array:
You can use a loop to access or manipulate each element in an array.
Example: Traversing an array using a for loop.
public class Main {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
3. Multi-Dimensional Array
A multi-dimensional array is an array of arrays. The most commonly used multi-
dimensional array is a 2D array, which stores data in a tabular (rows and columns) format.
Syntax of a 2D Array:
int[][] matrix = new int[3][3]; // A 3x3 matrix
matrix[0][0] = 1; // First element of the array
You can also initialize a 2D array directly:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Accessing 2D Array Elements:
System.out.println(matrix[0][1]); // Output: 2 (Element at first row, second column)
Traversing a 2D Array:
You can use nested loops to iterate over a 2D array.
Example:
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
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(); // New line after each row
4. Strings in Java
A String is a sequence of characters. In Java, strings are objects of the String class, which
provides many methods to manipulate them.
Creating a String:
String str = "Hello World";
String Methods:
Some commonly used String methods:
length(): Returns the length of the string.
charAt(index): Returns the character at the specified index.
substring(start, end): Returns a substring from start index to end index
(excluding end index).
equals(): Compares two strings for equality.
Example of String Manipulation:
public class Main {
public static void main(String[] args) {
String str = "Hello World";
System.out.println("Length: " + str.length()); // Output: 11
System.out.println("Character at index 1: " + str.charAt(1)); // Output: e
System.out.println("Substring: " + str.substring(0, 5)); // Output: Hello
System.out.println("Equality check: " + str.equals("Hello World")); // Output: true
Concatenation of Strings:
Strings can be concatenated using the + operator.
String s1 = "Hello";
String s2 = "World";
String s3 = s1 + " " + s2; // Output: "Hello World"
5. Pattern Printing in Java
Pattern printing involves using loops to print a sequence of numbers, symbols, or characters
in a structured format.
Example 1: Printing a Right-Angle Triangle of Numbers:
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
System.out.println();
}
Output:
12
123
1234
12345
Example 2: Inverted Number Pyramid:
public class Main {
public static void main(String[] args) {
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
System.out.println();
public class Main {
public static void main(String[] args) {
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
System.out.println();
}
Output:
12345
1234
123
12
Example 3: Printing a Star Pattern:
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
System.out.println();
Output:
**
***
****
*****
6. Practice Problems (Day 9–10)
Problem 1: Find the Largest Element in an Array
Write a program to find the largest element in an array.
public class Main {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 5, 15};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
System.out.println("Largest element: " + max);
Problem 2: Reverse a String
Write a program to reverse a given string.
public class Main {
public static void main(String[] args) {
String str = "Hello";
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
System.out.println("Reversed String: " + reversed);
Problem 3: Print Fibonacci Series Using an Array
Write a program to print the Fibonacci series up to a given number using an array.
public class Main {
public static void main(String[] args) {
int n = 10; // Number of terms
int[] fib = new int[n];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
for (int i = 0; i < n; i++) {
System.out.print(fib[i] + " ");
Problem 4: Find the Sum of Elements in a 2D Array
Write a program to find the sum of all elements in a 2D array.
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
sum += matrix[i][j];
}
System.out.println("Sum of elements: " + sum);
7. Important Points to Remember
Arrays: Use arrays to store and manipulate collections of data. Arrays can be one-
dimensional or multi-dimensional.
Strings: Strings are objects in Java and are immutable. You can use various methods
provided by the String class to manipulate strings.
Pattern Printing: You can print patterns using loops (nested for loops are commonly used).
Day 9–10 Goals
Learn to declare, initialize, and manipulate arrays in Java.
Master string manipulation using the built-in methods of the String class.
Get familiar with pattern printing by using nested loops to create various shapes and
patterns.