0% found this document useful (0 votes)
3 views

Array 01a

Uploaded by

vamsi.d124
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Array 01a

Uploaded by

vamsi.d124
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Java Arrays

We will learn to declare, initialize, and


access array elements with the help of
examples.
An array is a collection of similar types of
data.
For example, if we want to store the names
of 100 people then we can create an array of
the string type that can store 100 names.

String[] array = new String[100];


Here, the above array cannot store more
than 100 names. The number of values in a
Java array is always fixed.
How to declare an array in Java?
In Java, here is how we can declare an
array.

dataType[] arrayName;
 dataType - it can be primitive data
types like int , char , double , byte , etc.
or Java objects
 arrayName - it is an identifier
For example,

double[] data;
Here, data is an array that can hold values
of type double .
But, how many elements can array this
hold?
Good question! To define the number of
elements that an array can hold, we have to
allocate memory for the array in Java. For
example,

// declare an array
double[] data;

// allocate memory
data = new double[10];
Here, the array can store 10 elements. We
can also say that the size or length of the
array is 10.
In Java, we can declare and allocate the
memory of an array in one single statement.
For example,

double[] data = new double[10];

How to Initialize Arrays in Java?


In Java, we can initialize arrays during
declaration. For example,

//declare and initialize and array


int[] age = {12, 4, 5, 2, 5};
Here, we have created an array named age
and initialized it with the values inside the
curly brackets.
Note that we have not provided the size of
the array. In this case, the Java compiler
automatically specifies the size by counting
the number of elements in the array (i.e. 5).
In the Java array, each memory location is
associated with a number. The number is
known as an array index. We can also
initialize arrays in Java, using the index
number. For example,

// declare an array
int[] age = new int[5];

// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;
..

Note:
 Array indices always start from 0. That is,
the first element of an array is at index 0.
 If the size of an array is n , then the last
element of the array will
be at index n-1 .

How to Access Elements of an Array in Java?


We can access the element of an array using the
index number. Here is the syntax for accessing
elements of an array,

// access array elements


array[index]
Let's see an example of accessing array elements
using index numbers.
Example: Access Array Elements
class Main {
public static void main(String[]
args) {
// create an array
int[] age = {12, 4, 5, 2, 5};

// access each array elements


System.out.println("Accessing
Elements of Array:");
System.out.println("First Element:
" + age[0]);
System.out.println("Second
Element: " + age[1]);
System.out.println("Third Element:
" + age[2]);
System.out.println("Fourth
Element: " + age[3]);
System.out.println("Fifth Element:
" + age[4]);
}
}

we are using the index number to access


each element of the array.
We can use loops to access all the elements
of the array at once.

Looping Through Array Elements


In Java, we can also loop through each
element of the array. For example,
Example: Using For Loop
class Main {
public static void main(String[]
args) {
// create an array
int[] age = {12, 4, 5};

// loop through the array


// using for loop
System.out.println("Using for
Loop:");
for(int i = 0; i < age.length; i+
+) {
System.out.println(age[i]);
}
}
}

Here, we are using the length property of the


array to get the size of the array.
We can also use the for-each loop to iterate
through the elements of an array. For example,
Example: Using the for-each Loop
class Main {
public static void main(String[]
args) {
// create an array
int[] age = {12, 4, 5};

// loop through the array


// using for loop
System.out.println("Using for-each
Loop:");
for(int a : age) {
System.out.println(a);
}
}
}
Compute Sum and Average of Array
Elements
class Main {
public static void main(String[]
args) {
int[] numbers = {2, -9, 0, 5, 12,
-25, 22, 9, 8, 12};
int sum = 0;
Double average;
// access all elements using for each
loop , add each element in sum
for (int number: numbers) {
sum += number;
}
// get the total number of
elements
int arrayLength = numbers.length;

// calculate the average


// convert the average from int to
double
average = ((double)sum /
(double)arrayLength);
System.out.println("Sum = " +
sum);
System.out.println("Average = " +
average);
}
}
Run Code
Output:
Sum = 36
Average = 3.6

This is a Java Program to Search Key Elements


in an Array.
Enter the size of array and then enter all the
elements of that array. Now enter the element
you want to search for. With the help of for loop
we can find out the location of the element
easily.
Here is the source code of the Java Program to
Search Key Elements in an Array. The Java
program is successfully compiled and run on a
Windows system. The program output is also
shown below.

1. import java.util.Scanner;
2. public class
Search_Element
3. {
4. public static void
main(String[] args)
5. {
6. int n, x, flag =
0, i = 0;
7. Scanner s = new
Scanner(System.in);
8.
System.out.print("Enter no.
of elements you want in
array:");
9. n = s.nextInt();
10. int a[] = new
int[n];
11.
System.out.println("Enter
all the elements:");
12. for(i = 0; i < n;
i++)
13. {
14. a[i] =
s.nextInt();
15. }
16.
System.out.print("Enter the
element you want to
find:");
17. x = s.nextInt();
18. for(i = 0; i < n;
i++)
19. {
20. if(a[i] == x)
21. {
22. flag = 1;
23. break;
24. }
25. else
26. {
27. flag = 0;
28. }
29. }
30. if(flag == 1)
31. {
32.
System.out.println("Element
found at position:"+(i +
1));
33. }
34. else
35. {
36.
System.out.println("Element
not found");
37. }
38. }
39. }

You might also like