0% found this document useful (0 votes)
3 views27 pages

Java Arrays 1

The document provides an overview of arrays in Java, describing them as objects that store similar data types in contiguous memory locations. It covers array declaration, instantiation, and manipulation methods such as sorting and binary search. Additionally, it highlights the advantages of using arrays, including code optimization and random access to elements.

Uploaded by

Eshu Jain
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)
3 views27 pages

Java Arrays 1

The document provides an overview of arrays in Java, describing them as objects that store similar data types in contiguous memory locations. It covers array declaration, instantiation, and manipulation methods such as sorting and binary search. Additionally, it highlights the advantages of using arrays, including code optimization and random access to elements.

Uploaded by

Eshu Jain
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/ 27

JAVA -ARRAYS

WHAT ARE ARRAYS


• Normally, an array is a collection of similar type of elements which
has contiguous memory location.
• Java array is 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.
• Arrays are stored in contagious memory [consecutive memory locations].
• Since arrays are objects in Java, we can find their length using the object
property length. This is different from C/C++, where we find length using
sizeof.
• A Java array variable can be declared like other variables with [] after the
data type.
• The variables in the array are ordered, and each has an index beginning
from 0.
oreover, Java provides the feature of anonymous
rays which is not available in C/C++.
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.
Types of Array in java

There are two types of array.

• Single Dimensional Array


• Multidimensional Array
Single Dimensional Array in Java

ARRAY DECLARATION
An array declaration has two components: the type and the name.
Syntax to Declare an Array in Java EXAMPLES

dataType[] arr;
(or)
dataType arr[];
-Instantiating an Array in Java
When an array is declared, only a reference of an array is created. To create or
give memory to the array, you create an array like this:

var-name = new type [size];

Example:
int intArray[]; //declaring array
intArray = new int[20]; // allocating memory to array

OR

int[] intArray = new int[20]; // combining both statements in one


• Array Length
• To find out how many elements an array has, use the length property:

• Example
• String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
• System.out.println(cars.length);
• // Outputs 4
• 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.
//Program to illustrate how to declare
and initialize array and traverse the Java array.
class Testarray{
public static void main(String args[]){
int a[]=new int[5];
a[0]=10;
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;

for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}}
Example-2
class Testarray1{
public static void main(String
args[]){
int a[]={33,3,4,5};
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}}
ARRAY DEMO PROGRAM PG public class student_result_array {
122 public static void main(String[] args) {

double [] marks={346,144,103,256.5,387.5};
double total_marks=400;
System.out.println("\tCLASS REPORT");

System.out.println("----------------------------");
System.out.println("ROLLNO\tMARKS\tPERCENTAGE\tRESULT");
System.out.println("====================================");
for (int i=0;i<marks.length;i++)
{
double percentage=(marks[i]/total_marks)*100;
String result;
if (percentage>=40)
result="passed";
else
result="failed";
System.out.print((i+1)+"\t");
System.out.print((marks[i])+"\t");
System.out.print(percentage+"\t\t");
System.out.println(result);
}
System.out.println("__________________________");
}}
Array Manipulation

Sorting
Binary Search
Finding length
Array Manipulation

The Arrays class has a number of useful methods. Let us start by using the
sort()method
To sort an array of integers in ascending and descending order
sort()method is used
First we import java.util.Arrays class.
Then in the main() method, we invoke the Arrays.sort() method on the
array we need to sort.
double[] marks = {103, 144, 256.5,346, 387.5};
Arrays.sort(marks);
print the lowest marks, we can now write System.out.println(marks[0]); To
print the highest marks,we can write
System.out.println(marks[marks.length-1]);
The same method can be used to sort an array of
Strings in alphabetic order.

String[] names = {"Sarthk","Saumya","Mayank","Mudit","Shiva","Anju",


"Savita"};
Arrays.sort(names);
sorting
Arrange in ascending order
Arrange in descending order
// Java Program to Sort the Elements in Descending Order
import java.util.*;

class GFG {
public static void main(String[] args)
{
Integer array[] = { 1, 2, 3, 4, 5 };

Arrays.sort(array, Collections.reverseOrder());

System.out.println(Arrays.toString(array));
}
}
The binarySearch() method of the Arrays class helps us search for a specific
element in the array. The parameters it needs are the array to be searched
and the key element to be searched. The method returns the index of the
array where the key is present.
If the key is not found in the array, the binarySearch method returns -1.
double[] marks = {103, 144, 256.5, 346, 387.5};
int key = 346;
int index = Arrays.binarySearch(marks,key);
Note that the array must be sorted before invoking binarySearch().
If it is not sorted, the results are undefined
BINARY SEARCH PROGRAM
SORT CODE WITHOUT FUNCTION
BINARY SEARCH PROGRAM

You might also like