0% found this document useful (0 votes)
9 views13 pages

Lec 5 Arrays

The document provides an overview of Java programming concepts related to arrays, including declaration, accessing elements, modifying values, and working with multidimensional arrays. It also includes examples of array manipulation and a list of assignments for practice. The content is intended for educational purposes, specifically for students learning Java programming.

Uploaded by

mswindow00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

Lec 5 Arrays

The document provides an overview of Java programming concepts related to arrays, including declaration, accessing elements, modifying values, and working with multidimensional arrays. It also includes examples of array manipulation and a list of assignments for practice. The content is intended for educational purposes, specifically for students learning Java programming.

Uploaded by

mswindow00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Java Programming

By:
Asad Khan
Lecturer in CS GPGC Lakki Marwat

07/30/2025
Array

• Arrays are used to store multiple values in a single variable,


instead of declaring separate variables for each value.
• To declare an array, define the variable type with square brackets:
• String[] cars;
• We have now declared a variable that holds an array of strings.
• To insert values to it, you can place the values in a comma-
separated list, inside curly braces:

• String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


• To create an array of integers, you could write:

• int[] myNum = {10, 20, 30, 40};

07/30/2025
Access the Elements of an Array
• You can access an array element by referring to the
index number.
• This statement accesses the value of the first element in
cars:
• Example:
• String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
• System.out.println(cars[2]);
• // Outputs Ford

07/30/2025
Change an Array Element
• To change the value of a specific element, refer to the index
number:
• public class Main {
• public static void main(String[] args) {
• String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
• cars[0] = “Volkswagen";
• cars[2] = “Toyota";
• System.out.println(cars[0],cars[2]);
• }
• }

07/30/2025
Array Length

• To find out how many elements an array has, use the


length property:
• public class Main {
• public static void main(String[] args) {
• String[] cars = {"Volvo", "BMW", "Ford",
"Mazda“,”suzuki”};
• System.out.println(cars.length);
• }
• }

07/30/2025
Loop Through an Array
• You can loop through the array elements with the for loop, and
use the length property to specify how many times the loop
should run.
• public class Main {
• public static void main(String[] args) {
• String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
• for (int i = 0; i < cars.length; i++) {
• System.out.println(cars[i]);
• }
• }}
• The example above can be read like this: for each String
element (called i - as in index) in cars, print out the value of i.
• If you compare the for loop and for-each loop, you will see
that the for-each method is easier to write, it does not require a
counter (using the length property), and it is more readable.
07/30/2025
Two dimensional Arrays in Java

• Multidimensional arrays are arrays of arrays with each element


of the array holding the reference of other arrays. These are
also known as Jagged Arrays.
• A multidimensional array is created by appending one set of
square brackets ([]) per dimension.
• Multidimensional arrays are useful when you want to store
data as a tabular form, like a table with rows and columns.
• To create a two-dimensional array, add each array within its
own set of curly braces:
• Example:
• int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
• myNumbers is now an array with two arrays as its elements.

07/30/2025
Access Elements of an array

• To access the elements of the myNumbers array, specify two


indexes: one for the array, and one for the element inside that
array.
• This example accesses the third element (2) in the second
array (1) of myNumbers:

• Example
• int[ ][ ] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
• System.out.println(myNumbers[1][2]);

07/30/2025
Cont…
public class twoDimensional {
// main function
public static void main(String args[])
{
// declaring and initializing 2D array
int arr[][]
= { { 2, 7, 9 }, { 3, 6, 1 }, { 7, 4, 2 } };

// printing 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
}
07/30/2025
Cont…
public class Test {
public static void main(String args[])
{
int arr[ ] = { 3, 1, 2, 5, 4 };

// passing array to method m1


sum(arr);
}

public static void sum(int[] arr)


{
// getting sum of array values
int sum = 0;

for (int i = 0; i < arr.length; i++)


sum += arr[i];

System.out.println("sum of array values : " + sum);


}
07/30/2025
}
Cont…
public class TwoDArray {
public static void main(String[] args) {
int rows = 3;
int columns = 3;

int[ ][ ] array = new int[rows][columns];

int value = 1;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
array[i][j] = value;
value++;
}
}
System.out.println("The 2D array is: ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}}}
07/30/2025
Assignment
1. String Reverse Program in Java.
2. Factorial Program using Recursion.
3. Calculator Program in Java.
4. Swapping Two Numbers In Java.
5. Find the Transpose of a given Matrix.
6. A Program to perform basic Calculator operations.
7. Check Vowel or Consonant in Java.
8. Java program to find occurrence of a character in a String.
9. Calculate average of numbers using Array.
10. Java Program to sort an array in ascending order.
11. Check Even or Odd Number.
12. Check Leap Year in Java.

Note: Assignment is hand written.


Deadline: 01-09-2025

07/30/2025
Thank you

07/30/2025

You might also like