0% found this document useful (0 votes)
10 views10 pages

3 Arrays

This document provides an overview of arrays in Java, covering their types, creation, and manipulation. It includes details on one and multi-dimensional arrays, iteration using loops, and the use of varargs. Additionally, it highlights the functionalities of the java.util.Arrays class for array operations.

Uploaded by

swamiajoba.manju
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)
10 views10 pages

3 Arrays

This document provides an overview of arrays in Java, covering their types, creation, and manipulation. It includes details on one and multi-dimensional arrays, iteration using loops, and the use of varargs. Additionally, it highlights the functionalities of the java.util.Arrays class for array operations.

Uploaded by

swamiajoba.manju
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/ 10

Arrays

By
Manjiri Tatke
Objectives
• After completing this lesson, participants
will be able to
• Understand the different types of Arrays
• Implement one and multi dimensional arrays
• Iterate arrays using loops
• Use varargs
• Work with java.util.Arrays
Arrays
• Arrays are used to group elements of either of primitive or reference types
• Array in java is created as Object:
• This object will help developers to find size of array
• Using this object developers can manipulate array
• Can be compared with null
• All elements of array of same type
• Array is a fixed-length data structure having zero-based indexing
Arrays
• A group of like-typed variables referred by a common name
• Array declaration and initialization:
• int arr [];
arr = new int[10];
• int arr[] = {2,3,4,5};
• int twoDim [][] = new int[4][5];
Creating Array Objects
• Arrays of objects too can be created:
Box barr[] = new Box[3];
• Example 1: barr[0] = new Box();
barr[1] = new Box();
barr[2] = new Box();

String[] Words = new String[2];


Words[0]=new String(“Bombay”);
Words[1]=new String(“Pune”);
• Example 2:
Enhanced for Loop (foreach)
• New feature introduced in Java 5
• Iterate through a collection or array
for (variable : collection)
{ //code}
• Syntax:

• Example int sum(int[] intArray)


{
int result = 0;
for (int index : intArray)
result += index;
return result;
}
Variable Argument List
• New feature added in J2SE5.0
• Allows methods to receive unspecified number of arguments
• An argument type followed by ellipsis(…) indicates variable number of
arguments of a particular type
• Variable-length argument can take from zero to n arguments
• Ellipsis can be used only once in the parameter list
• Ellipsis must be placed at the end of the parameter list
Variable Argument List (contd..)
• The above print function can be invoked using any of the invocations:
• print(1,1,”XYZ”)
• print(2,5)
• print(5,6,”A”,”B”)

//Valid Code //Invalid Code


void print(int a,int b,String...c) void print(int a, int b…,float c)
{ {
//code //code
} }

Varargs can be used only in the


final argument position.
Using java.util.Arrays Class
• This class contains lots of useful methods to manipulate contents of array

Method Name Use

asList Creates a new List from array

binarySearch Use to search an element in an array

copyOf(array,n) Creates new array of n size and copy all


elements from array to new one
copyOfRange(array,n,from,to) Creates new array of n size and copy specified
elements from array to new one

sort Sort elements of an array

equals Compare two array elements

fill Inserts specified value to each element of an


array
stream(array) Creates stream from an array
Summary
• In this lesson, you have learnt about:
• Creating and using array
• Manipulating array
• Iterating array
• Varargs
• Using java.util.Arrays class

You might also like