Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Introduction To Array

The document discusses Java arrays, including that arrays allow storing a fixed number of similar elements in contiguous memory locations and are indexed starting from 0. It covers declaring, initializing, and traversing arrays using for loops as well as computing sums and averages of array elements. The document also provides an example of user input to populate an array and calculate the total marks.

Uploaded by

earl bagain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Introduction To Array

The document discusses Java arrays, including that arrays allow storing a fixed number of similar elements in contiguous memory locations and are indexed starting from 0. It covers declaring, initializing, and traversing arrays using for loops as well as computing sums and averages of array elements. The document also provides an example of user input to populate an array and calculate the total marks.

Uploaded by

earl bagain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Java Arrays

Java array
• an object which contains elements of a similar data type. Additionally,
The elements of an array are stored in a contiguous memory location.
It is a data structure where we store similar elements. We can store
only a fixed set of elements in a Java array.
• Array in Java is index-based, the first element of the array is stored at
the 0th index, 2nd element is stored on 1st index and so on.
• Unlike C/C++, we can get the length of the array using the length
member. In C/C++, we need to use the sizeof operator.
• Java provides the feature of anonymous arrays which is not available
in C/C++.

anonymous arrays- An array in Java without any name


Advantages

• Code Optimization: It makes the code optimized, we can


retrieve or sort the data efficiently.
• Random access: We can get any data located at an index
position.
Disadvantages

• Size Limit: We can store only the fixed size of


elements in the array. It doesn't grow its size at
runtime. To solve this problem, collection framework
is used in Java which grows automatically.
Types of Array in java

• Single Dimensional Array


• Multidimensional Array
Single Dimensional Array in Java

Syntax to Declare an Array in Java

• dataType[] arr; (or)


• dataType []arr; (or)
• dataType arr[];

Instantiation of an Array in Java

• arrayRefVar=new datatype[size];
Example of Java Array
//Java Program to illustrate how to declare, instantiate, initialize
//and traverse the Java array.
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
Declaration, Instantiation and Initialization of Java Array

We can declare, instantiate and initialize the java array


together by:

int a[]={33,3,4,5};//declaration, instantiation and initialization


Simple example to print this array
//Java Program to illustrate the use of declaration, instantiation
//and initialization of Java array in a single line
class Testarray1{
public static void main(String args[]){
int a[]={33,3,4,5};//declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
Output:
System.out.println(a[i]);
}} 33
3
4
5
For-each Loop for Java Array

We can also print the Java array using for-each loop


. The Java for-each loop prints the array elements one by one. It holds
an array element in a variable, then executes the body of the loop.
The syntax of the for-each loop is given below:

for(data_type variable:array){
//body of the loop
}
Example:
//Java Program to print the array elements using for-each loop
class Testarray1{
public static void main(String args[]){
int arr[]={33,3,4,5};
//printing array using for-each loop
Output:
for(int i:arr)
33
System.out.println(i); 3
4
}} 5
Example: Compute Sum and Average of Array Elements

class Main {
average = ((double)sum / (double)arrayLength);
public static void main(String[] args) {
int[] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12}; System.out.println("Sum = " + sum);
int sum = 0; System.out.println("Average = " + average);
}
double average;
}
// access all elements using for each loop
// add each element in sum
for (int number: numbers) {
sum += number; Output:
}
Sum = 36
// get the total number of elements
Average = 3.6
int arrayLength = numbers.length;
// calculate the average
// convert the average from int to double
In the example, we have created an array of named numbers. We have used the for...each
loop to access each element of the array.

Inside the loop, we are calculating the sum of each element. Notice the line,

int arrayLength = number.length;


Here, we are using the length attribute of the array to calculate the size of the array. We
then calculate the average using:

average = ((double)sum / (double)arrayLength);


As you can see, we are converting the int value into double. This is called type casting in
Java.
Entering Data of An Array
package array2;
import java.util.Scanner;
public class Array2 {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);

int[] marks = new int[3];


int totalMarks=0;
System.out.println("Enter the marks in all your subjects:");
for (int i=0; i<marks.length;i++){

marks[i]=input.nextInt();
totalMarks=totalMarks+marks[i];
}

System.out.println("Total Marks: "+ totalMarks);

}
}

You might also like