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

Lec4 Java Arrays

Here are Java programs demonstrating use of methods from the Arrays class: import java.util.Arrays; public class ArrayMethodsDemo { public static void main(String[] args) { int[] array = {5,3,6,2,10}; //1. binarySearch() int index = Arrays.binarySearch(array, 6); System.out.println("Index of element 6 is: " + index); //2. equals() int[] array2 = {5,3,6,2,10}; boolean equal = Arrays.equals(array, array2); System.out.println("Arrays are equal: " + equal); //

Uploaded by

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

Lec4 Java Arrays

Here are Java programs demonstrating use of methods from the Arrays class: import java.util.Arrays; public class ArrayMethodsDemo { public static void main(String[] args) { int[] array = {5,3,6,2,10}; //1. binarySearch() int index = Arrays.binarySearch(array, 6); System.out.println("Index of element 6 is: " + index); //2. equals() int[] array2 = {5,3,6,2,10}; boolean equal = Arrays.equals(array, array2); System.out.println("Arrays are equal: " + equal); //

Uploaded by

Abdul Rehman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

MCS

Object Oriented Programming

Lecture 04
Java Arrays
MCS
Arrays
• An array is a container object that holds a fixed
number of values of a same data type.
• The length of an array is established when it is
created. After creation, its length is fixed.
• Each item in an array is called an element, and each
element is accessed by its numerical index.
• As shown in the following illustration, numbering
begins with 0. The 9th element, for example, would
therefore be accessed at index 8.
MCS
Declaring Array Variables
• Like declarations for variables of other types, an
array declaration has two components: the
array's type and the array's name. The syntax
for declaring an array variable:-
// preferred way
type[] arrayRefVar;
OR
// works but not preferred way
type arrayRefVar[];
• An array's type is written as type[], where
type is the data type of the contained elements;
the brackets are special symbols indicating that
this variable holds an array.
MCS
Declaring Array Variables
• Although, Java allows to place the brackets after
the array's name, however, convention
discourages this form; because the brackets
identify the array type and should appear with
the type designation.
• The size of the array is not part of its type (which
is why the brackets are empty).
• An array's name can be any valid identifier.
// preferred way
int[] myList;
OR
// works but not preferred way
int myList[];
MCS
Creating Arrays
• As with variables of other types, the declaration
does not actually create an array; it simply tells the
compiler that this variable will hold an array of the
specified type.
• One way to create an array is with the new operator.
The following syntax is used to create an array of
specified size & type and assign its reference to a
array variable:-
arrayRefVar = new type[arrySize];
• For example, following statement allocates an
array with enough memory for 10 integer
elements and assigns the array to the myList
variable:-
myList = new int[10];
MCS
Creating Arrays
• Alternatively, we can use the shortcut syntax to
create and initialize an array:
int[] myList = {
100, 200, 300, 400, 500,
600, 700, 800, 900, 1000 };
• Here the length of the array is determined by the
number of values provided between braces and
separated by commas.
• Declaring an array variable, creating an array, and
assigning the reference of the array to the variable
can be combined in one statement, as shown below
int[] myList = new int[10];
MCS
Creating Arrays
MCS
Printing Arrays
COMMAND OUTPUT
MCS
Sample Program
public class ArrayDemo1 {
public static void main(String[] args) {
// declares an array of integers
int[] myList;
// allocates memory for 10 integers
myList = new int[10];
// initialize array elements
for(int i = 0; i < myList.length; i++) {
myList[i] = (i + 1) * 100;
}
// access array elements
for(int i = 0; i < myList.length; i++) {
System.out.printf("Element at index %d:
%d\n",
i, myList[i]);
}
}
MCS
Sample Program
public class ArrayDemo2 {
public static void main(String[] args) {
// create and initialize an array
int[] myList = {
100, 200, 300,
400, 500, 600,
700, 800, 1000 };
// access array elements
for(int i = 0; i < myList.length; i++) {
System.out.printf("Element %d: %d\n",
i + 1, myList[i]);
}
}
}
MCS
Sample Program
MCS
Multidimensional Arrays
• In the Java a multidimensional array is an array whose components
are themselves arrays.
• Unlike arrays in C or Fortran, the rows are allowed to vary in length.
For Example:
public class Demo2DArray {
public static void main(String[] args) {
int[][] twoDim = {
{ 11, 12, 13 },
{ 21, 22, 23, 24, 25 },
{ 31, 32 },
{ 41, 42, 43, 44 }
};
for(int row = 0; row < twoDim.length; row++) {
for(int col = 0; col < twoDim[row].length; col++)
{
System.out.printf("%d, ", twoDim[row][col]);
// System.out.printf(twoDim[row][col] + ", ");
}
System.out.println("\b\b ");
}
}
MCS
Copying Arrays
• The System class has an arraycopy() method that can be used to
efficiently copy data from one array into another:
public static void arraycopy(
Object src, // the source array
int srcPos, // starting position in source array
Object dest, // the destination array
int destPos, // starting position in destination
array
int length) // the number of array elements to
copy
• For Example:
public class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'i', 'n', 'e', 'x',
'p', 'e', 'n', 's', 'i', 'v', 'e' };
char[] copyTo = new char[9];
System.arraycopy(copyFrom, 2, copyTo, 0, 9);
System.out.println(new String(copyTo));
}
MCS
Copying Arrays
MCS
Array Manipulations
• Java provides several methods for performing array
manipulations (such as copying, sorting and searching
arrays) in the java.util.Arrays class. For Example:-
import java.util.Arrays;
public class ArrayCopyDemo2 {
public static void main(String[] args) {
char[] copyFrom = { 'i', 'n', 'e', 'x',
'p', 'e', 'n', 's', 'i', 'v', 'e' };
char[] copyTo = Arrays.copyOfRange(copyFrom, 2,
11);
System.out.println(new String(copyTo));
}
}
• Note that the second parameter of the copyOfRange()
method is the initial index of the range to be copied,
inclusively, while the third parameter is the final index of
the range to be copied, exclusively.
MCS
Array Manipulations
• Some other useful operations provided by
methods in the java.util.Arrays class, are:
– Searching an array for a specific value to get the index
at which it is placed (the binarySearch() method).
– Comparing two arrays to determine if they are equal
or not (the equals() method).
– Filling an array to place a specific value at each index
(the fill() method).
– Sorting an array into ascending order. This can be
done either sequentially, using the sort() method, or
concurrently, using the parallelSort() method.
Parallel sorting of large arrays on multiprocessor
systems is faster than sequential array sorting.
MCS
Assignment - 1
BT-Level CLO Marks
C-2 01 10

• Write Java programs to demonstrate use


of following methods of Arrays class:
– binarySearch()
– equals()
– fill()
– sort()
– parallelSort()

You might also like