0% found this document useful (0 votes)
65 views13 pages

Topics: Arrays Class in Java

The Arrays class in Java contains methods for manipulating arrays such as sorting and searching. It is defined in the java.util package. All methods in the Arrays class are static and can be called using the class name. Important methods include binarySearch(), equals(), fill(), and sort() for performing common operations on arrays like searching, comparing, initializing values, and sorting.

Uploaded by

Shubhankar Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views13 pages

Topics: Arrays Class in Java

The Arrays class in Java contains methods for manipulating arrays such as sorting and searching. It is defined in the java.util package. All methods in the Arrays class are static and can be called using the class name. Important methods include binarySearch(), equals(), fill(), and sort() for performing common operations on arrays like searching, comparing, initializing values, and sorting.

Uploaded by

Shubhankar Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Topics

• Arrays class in Java

1 Object-Oriented Programming Using Java


Arrays class in Java

• Arrays class in <<java.util>> package is defined as


follows
• This class contains the required methods for
manipulating arrays such as sorting and searching
• All methods are <<static>> and hence can be called via
class name.
• Arrays class is part of Java’s Collection Framework

2 Object-Oriented Programming Using Java


Arrays class: Important
Methods
binarySearch()
static int binarySearch(byte[] a, byte key)
static int binarySearch(char[] a, char key)
static int binarySearch(double[] a, double key)
static int binarySearch(float[] a, float key)
static int binarySearch(int[] a, int key)

static int binarySearch(long[] a, long key)


static int binarySearch(Object[] a, Object key)
static int binarySearch(Object[] a, Object key, Comparator c)
static int binarySearch(short[] a, short key)

3 Object-Oriented Programming Using Java


Arrays class: Important
Methods ….
equals()
static boolean equals(boolean[] a, boolean[] a2)
static boolean equals(byte[] a, byte[] a2)
static boolean
equals(char[] a, char[] a2)
static boolean equals(double[] a, double[] a2)
static boolean equals(float[] a, float[] a2)
static boolean equals(int[] a, int[] a2)
static boolean equals(long[] a, long[] a2)
static boolean equals(Object[] a, Object[] a2)
static boolean equals(short[] a, short[] a2)

4 Object-Oriented Programming Using Java


Arrays class: Important
Methods ….
fill()
static void fill(boolean[] a, boolean val)
static void fill(boolean[] a, int fromIndex, int toIndex,
boolean val)
static void fill(byte[] a, byte val)
static void fill(byte[] a, int fromIndex, int toIndex, byte val)
static void fill(char[] a, char val)
static void fill(char[] a, int fromIndex, int toIndex,
char val)
static void fill(double[] a, double val)
static void fill(double[] a, int fromIndex, int toIndex,
double val)
static void fill(float[] a, float val)
5 Object-Oriented Programming Using Java
Arrays class: Important
Methods ….
fill() ….
static void fill(float[] a, int fromIndex, int toIndex, float val)
static void fill(int[] a, int val)
static void fill(int[] a, int fromIndex, int toIndex, int val)
static void fill(long[] a, int fromIndex, int toIndex,
long val)
static void fill(long[] a, long val)
static void fill(Object[] a, int fromIndex, int toIndex,
Object val)
static void fill(Object[] a, Object val)
static void fill(short[] a, int fromIndex, int toIndex,
short val)
static void fill(short[] a, short val)
6 Object-Oriented Programming Using Java
Arrays class: Important
Methods ….
sort()

static void sort(byte[] a)


static void sort(byte[] a, int fromIndex, int toIndex)
static void sort(char[] a)
static void sort(char[] a, int fromIndex, int toIndex)
static void sort(double[] a)
static void sort(double[] a, int fromIndex, int toIndex)
static void sort(float[] a)
static void sort(float[] a, int fromIndex, int toIndex)
static void sort(int[] a)
static void sort(int[] a, int fromIndex, int toIndex)

7 Object-Oriented Programming Using Java


Arrays class: Important
Methods ….
sort()….

static void sort(long[] a)


static void sort(long[] a, int fromIndex, int toIndex)

static void sort(Object[] a)


static void sort(Object[] a, Comparator c)
static void sort(Object[] a, int fromIndex, int toIndex)
static void sort(Object[] a, int fromIndex, int toIndex, Comparator c)
static void sort(short[] a)
static void sort(short[] a, int fromIndex, int toIndex)

8 Object-Oriented Programming Using Java


Arrays class: Example
Import java.util package to use
import java.util.*;
Arrays class
class ArrayExample
{
public static void main(String args[])
{
int x[] = {10,6,8,20}; int array Size = 4 LB=0 UB =3

double data[] = { 12.5,34.6,90.56,12.34,12.56};


double array Size = 5 LB=0 UB =4
float values[] = { 10.45f,23.56f,12.67f};
float arrays Size = 3 LB=0 UB =2
double data1[] = new double[10];
double array Size = 10 LB=0
boolean flags[] = new boolean[5]; UB =9
boolean array Size = 5 LB=0 UB =4
int x1[] = {10,6,8,20}; int array Size = 4 LB=0 UB =3
9 Object-Oriented Programming Using Java
Arrays class: Example ….
System.out.println(Arrays.binarySearch(x,20));

Arrays.sort(x); Prints index of 20 in x 3


Sorts elements of x
for(int i=0;i<x.length;i++)
System.out.print(x[i]+" "); Prints Elements of x
System.out.println(); 6 8 10 20
Arrays.fill(data1,12.56); Fills a single value
12.56 in all indexes of
for(int i=0;i<data1.length;i++) data1
System.out.print(data1[i]+" "); Prints Elements of
System.out.println(); data1

12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56

10 Object-Oriented Programming Using Java


Arrays class: Example ….
Fills true value in boolean array
Arrays.fill(flags,2,5,true); flags from index 2 to 4
//Arrays.fill(flags,2,6,true);
for(int i=0;i<flags.length;i++) ArrayIndexOutofBoundsException
System.out.print(flags[i]+" ");
System.out.println();
false false true true true
System.out.println(Arrays.equals(x,x1)); Prints true or false whether x
and x1 equals
Arrays.sort(values);
Sorts the elements of arrays
for(int i=0;i<values.length;i++)
System.out.print(values[i]+" ");
System.out.println(); 10.45 12.67 23.56
}
} // End of class

11 Object-Oriented Programming Using Java


Array : Examples
int a[] = { 10,8,6}; 10
for(int i=0;i<a.length;i++) 8
System.out.println(a[i]); 6

']' expected
int a[3] = { 10,8,6};
int a[3] = { 10,8,6};
for(int i=0;i<a.length;i++)
^
System.out.println(a[i]);
1 error
//int table[][]={0,0,0,1,1,1};
//int table[2][3]={0,0,0,1,1,1}; WRONG
int table[ ][ ]={{0,0,0},{1,1,1}};
for(int i=0; i<table.length;i++) 000
{ 111
for(int j=0;j<table[i].length;j++)
{
System.out.print(table[i][j]);}
System.out.println();
}
12 Object-Oriented Programming Using Java
Thank You

13 Object-Oriented Programming Using Java

You might also like