0% found this document useful (0 votes)
5 views

unit-3

This document provides a comprehensive overview of arrays in Java, including their declaration, instantiation, initialization, and methods for accessing elements. It also covers multidimensional and jagged arrays, variable-length arguments, and command-line arguments in Java. Additionally, it includes practice programs for hands-on learning and understanding of these concepts.

Uploaded by

dead220905
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)
5 views

unit-3

This document provides a comprehensive overview of arrays in Java, including their declaration, instantiation, initialization, and methods for accessing elements. It also covers multidimensional and jagged arrays, variable-length arguments, and command-line arguments in Java. Additionally, it includes practice programs for hands-on learning and understanding of these concepts.

Uploaded by

dead220905
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/ 32

UNIT-2

S Points to cover:

Y • Array
• Declaring and creating array in java
L • Examples using array
• Passing arrays to method
L • Multidiamentional array
• Variable length argument list
B • Using command line argument

U
S
Array in java:
Introduction:
• Java array is an object which contains elements of a similar data type.
• Examples:

• Basically we can store only a fixed set of elements in a Java array.


• The elements of an array are stored in a contiguous memory location.
• 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.
• The variables in the array are ordered, and each has an index beginning with 0.
• If the size of an array is n, then the last element of the array will be at index n-1.
Array in java:
Declaring an array in Java?
• Declaration:

• type : it can be primitive data types like int, char, double, byte, etc. or Java objects
• Var-name it is an array name which is an identifier
For example,
both are correct…….
• If we declare array as above (int a[]), this is not sufficient to allocate memory to array.
• It merely tells the compiler that this variable (int a) will hold an array of the integer type.
• To link the array with actual, physical array of integers, you must allocate one using new
keyword and that is called as Instantiating an Array in Java
Array in java:
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: The general form of new as it applies to one-
dimensional arrays appears as follows:

• Example:

• We can also combine these both statement which is called as instantiation of an array:
Array in java:
Initialization of an array
We can initialize array in three different ways:
1. During declaration: In Java, we can initialize arrays during declaration. For example,

In this case, the Java compiler automatically specifies the size by counting the
number of elements in the array (i.e. 5).
2. Using index number: We can also initialize arrays in Java, using the index
number. For example as below

3. Using array literal: array literals can be used for array initialization as below
Array in java:
Accessing
Accessing array
arrayelements:
elements:
1. Using
Using index:
index:
• We cancanaccess
accessthe
theelement of an
element of array usingusing
an array the index
the number. Here is Here
index number. the syntax
is theforsyntax
accessing
for
elements
accessingof an array,of an array,
elements

::

2. Using for lop:


2. Using for lop:
• But as an array is a collection of many element, to access more than one element we prefer to use
• But
loopsasfor
anaccessing
array is aan
collection of many element, to access more than one element we prefer to
array elements
use loops for accessing an array elements

• Here, we are using the length property


•of the
Here, weto
array are
getusing the of
the size length property
the array.
Array in java:
3.Using for each loop:
• We can also use the for-each loop to iterate through the elements of an array. For example,

• Here a will not point to index, it will point to actual element at each index starting from first to
last element.
Array in java:

Program on array for practice


• Write java program to take size of array and elements of user from user and display the array
• Write java program to display elements with even index number
• Write java program to consider array for age of 10 people. Display the age which is greater
than 18
• Write java program to find sum and average of array elements
• Write java program to take two integer array with same size. Display addition of their
elements sequentially.
Passing array as parameter to method
• Generally, the purpose of passing an array to a function is to transfer a large amount of data
between methods.
• To pass an array to a function, just pass the array as function's parameter (as normal
variables), and when we pass an array to a function as an argument, in actual the address of
the array in the memory is passed, which is the reference.
• Thus, any changes in the array within the method will affect the actual array values.
Synatx:
• Calling function with array as a parameter:

• Method definition will be like:



Passing array as parameter to method
• Write java program to pass array as a parameter to method which will multiply each element
by 2 and display the same
Passing array as parameter to method

Program on array for practice


• Write java program pass array as parameter to method which will display the largest element
from the array
• Write java program to pass array as parameter to method which replace all even numbers by 0
and odd numbers by 1
Multidimensional Array (2D array)
• A multidimensional 2D array is an array of arrays.
• Each element of a multidimensional array is an array itself.
• Here, in following diagram we have created a multidimensional array named ‘a’ with 3 row
and 4 columns.
• It is a 2-dimensional array, which will have capacity to hold a maximum of 12 (3x4) elements,
(less than 12 can be stored)
• As per the dimensions (3x4) each row can have maximum 4 elements
• In such 2D array first element will
Be placed at a[0][0], 2nd element at
a[0][1] and so on…
Multidimensional Array (2D array)
• Declaration of 2D array: declaring variable of 2D
– Syntax:

– Example:

• Here reference of 2D array wil be created but memory will not be allocated. For allocating
memory to declared array we need to instantiate it as follows

• instantiate of 2D array: Both declaration and memory assignment together


– Example: or

• Here array a with capacity to hold maximum 25 element will be created and memory will
be given to store 2maximum 25 elements of int type
Multidimensional Array (2D array)
Initialization of an 2D array
We can initialize 2D array in three different ways:
1. During declaration: In Java, we can initialize 2D arrays during declaration. For example,

2. Using index number: We can also initialize arrays in Java, using the index
number. For example as below

3. Using array literal: array literals can be used for array initialization as below
Multidimensional Array (2D array)
Accessing 2D array elements:
1. Using index:We can access the element of an array using the index number as below

2. Using for lop: for loops for accessing an array elements as:

3. Using foreach:
• Outer for loop variable represent each row (an array) so outer for loop variable must be of
array type as shown here
Jagged Array in Java
• If we are creating odd number of columns in a 2D array, it is known as a jagged array.
• In other words, it is an array of arrays with different number of columns.
• Suppose we want an 2D array with 3 rows and max 4 columns where each row will have
different number of elements as follow,

• How memory allocation will look like??


Jagged Array in Java
Declaration of jagged array:

• First statement is indicating that the type of array is jagged array with 3 rows. Number of
column is not fixed here
• Next 3 statements are indicating the total number of element in each row(3 items in row 0, 4
items in row 1, 1 item in row 2 etc). In short number of columns will be clear to compiler in
statement 2-statement 4
Jagged Array in Java
How to access elements from jagged array:?
• We can use for loop with as follows to access the elements from jagged array:

• Here a[i].length in inner for loop is representing each row


Variable length argument list:
• Let’s suppose you are creating a Java method. However, you are not sure how many
arguments your method is going to accept. To address this problem, Java 1.5 introduced
varargs(variable arguments)
• In Java, an argument of a method can accept arbitrary number of values.
• This argument that can accept variable number of values is called varargs.
• Syntax

• In order to define vararg, ... (three dots) is used in the formal parameter of a method.
• Example:
Variable length argument list:
• The Java compiler that the method can be called with zero or more arguments.
• As a result, nums variable is implicitly declared as an array of type int[ ].
• Thus, inside the method, nums variable is accessed using the array syntax.
• In case of no arguments, the length of nums is 0.
Rules for varargs:
• While using the varargs, you must follow some rules otherwise program code won't compile.
The rules are as follows:
– There can be only one variable argument in the method.
– Variable argument (varargs) must be the last argument.
Variable length argument list:
Java program for jagged variable length argument list:
Variable length argument list:
Overloading Varargs Methods
• Similar to typical methods, you can overload a vararg methods. For example,
• Here two methods having same name test() but different parameter list. Hence it is a method
overloading with varargs parameter
Variable length argument list:
Ambiguity in Varargs Method overloading

• In the above program, the compiler gets confused if you try to invoke the test() method even
though test() methods are overloaded and accepts different number of arguments
• Suppose function call is like test(1,2,3,4,5), here the compiler doesn’t know which method to
call.
• The compiler may think, you are trying to call test(int ... vargs) with one varargs argument.
Variable length argument list:
Practice program:

• Write java program to write method str-len(---) which will display the length of each string
(passed as a parameter)
• Write java program to to define method totalArgs(---) which will display total number of
parameters passed to method
• Write java program to define method reverseItem(---) which will display the argument in
reverse order
– Input: reverseItem(11, 22, 33)
– Output: 33 22 11
– Iuput2: reverseItem(“orchid”, “engineering”, “college”, ”solapur”)
– Outpur: Solapur college engineering orchid
Command line argument:
• The java command-line argument is an argument i.e. passed at the time of running the java
program.
• As the name suggests arguments are passed through the command line.
• The arguments passed from the console can be received in the java program and it can be
used as an input.
• So, basically it provides a convenient way to check the behavior of the program for the
different values. You can pass N numbers of arguments from the command prompt of any
type
• While giving execution command we have to pass the parameters as below
java Test 11 22 33 44 55
• Here all these values will be store in main() method collection parameter i.e. String args[]
Command line argument:
• Passing String Command-Line Arguments
• In java program main() method includes an array of strings named args as its parameter.
• So when we pass arguments of string data type through command, it will be copied in args[]
array
• As per the requirement we can later access those values from args[] array using for loop
• Note that Arguments are always stored as strings and always separated by white-space.
Command line argument:
• This is the program where we are passing string values at the running time which will be
displayed in program

• Program:

Command for:
• Compilation:

• Execution
Command line argument:
Passing Numeric Command-Line Arguments
• The main() method of every Java program only accepts string arguments.
• Hence when we pass numeric arguments, it is not possible to store numeric arguments in
main() method parameter args[].
• However, we can take these numeric values as a string and later convert string arguments
into numeric values.
• So when we write command java Test 10 20 30, actually main method argument will consider
these value in string format.
• But we can use parseInt() method of the Integer class to convert the string argument into an
integer.
• Similarly, we can use the parseDouble() and parseFloat() method to convert the string into
double and float respectively.
Command line argument:
Passing Numeric Command-Line Arguments
Command line argument:
Pracice program:
• Write java program to take integer numbers as an input through command line and display
addition of only those numbers which are even
• Write java program to take an integer number through command line and display only even
digits from the number
– Input: n=1234
– Output: 2 4
– Input: 13578
– Output: 8
Thank you

You might also like