0% found this document useful (0 votes)
4 views19 pages

Day 12

The document provides an overview of Java arrays, including how to declare, access, and modify array elements. It includes examples of integer and string arrays, as well as methods for looping through arrays and performing operations like summing elements. Additionally, it presents tasks and solutions for common array-related programming exercises.
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)
4 views19 pages

Day 12

The document provides an overview of Java arrays, including how to declare, access, and modify array elements. It includes examples of integer and string arrays, as well as methods for looping through arrays and performing operations like summing elements. Additionally, it presents tasks and solutions for common array-related programming exercises.
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/ 19

Array

Java 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:
int num = 69;
int player1 = 56;
int player2 =60;
Int player3=90;

int[] players={56,67,89,23,45,68,2,34,78,89,100}
Int[] roollnos={1,2,3,4,5,6}
String[] StudensName={“Sidhu”, “Krishna”,
“Aniket”}
Array
String[] cars; int[] nums={1,2,4,6,8,9};
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"};
Array
To create an array of integers, you could write:
int[] myNum = {10, 20, 30, 40};
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:
• String[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
• System.out.println(cars[0]);
Change an Array Element

• To change the value of a specific element,


refer to the index number:
• cars[0] = "Opel";
String[] cars = {"Volvo", "BMW","Ford","Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);

0 1 2 3
Volvo BMW Ford Mazda

Opel BMW Ford Mazda


Length of array
public class Main{
public static void main(String[] args) {
int[] nums = {1,2,3,4,5};
System.out.println(nums.length);
}
}
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.
• The following example outputs all elements in
the cars array:
Task
String[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
Array
• Declaration of array

int[] nums={1,2,3,4,5};
Array
• Write a program to declare 5 elements inter
array
Program to print all elements of array

public class Main{


public static void main(String[] args) {
int[] nums={1,2,3,4,5};
for (int i=0;i<=4; i++){
System.out.println(nums[i]);
}
}
}
Task
• Write a program to print sum of elements of
an integer array
Solution
public class Main{
public static void main(String[] args) {
int[] num={1,2,3,4,5};
int sum=0;
for(int i=0;i<5;i++){
sum=sum+num[i];
}
System.out.println("Sum of array = "+sum);
}
}
Task
• Program to find particular element in array
Solution
public class Main{
public static void main(String[] args) {
int[] num={1,2,3,4,5};
for (int i=0; i<4; i++){
if(num[i] == 2){
System.out.println("Number Found");
}
}
}

You might also like