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

Array&Jagged Array

The document provides an overview of arrays in Java, explaining their dynamic allocation, declaration, and initialization. It covers one-dimensional, multidimensional, and jagged arrays, along with examples of how to create and access them. Additionally, it introduces the for-each loop as a method for traversing arrays in Java.

Uploaded by

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

Array&Jagged Array

The document provides an overview of arrays in Java, explaining their dynamic allocation, declaration, and initialization. It covers one-dimensional, multidimensional, and jagged arrays, along with examples of how to create and access them. Additionally, it introduces the for-each loop as a method for traversing arrays in Java.

Uploaded by

vpn12981298
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Arrays in Java

An array is a group of like-typed variables that are referred to by a


common name. Arrays in Java work differently than they do in C/C++.
Following is some important point about Java arrays.
 In Java all arrays are dynamically allocated.
 Since arrays are objects in Java, we can find their length using
member length. This is different from C/C++ where we find length
using sizeof.
 A Java array variable can also be declared like other variables with
[] after the data type.
 The variables in the array are ordered and each have an index
beginning from 0.
 Java array can be also be used as a static field, a local variable or a
method parameter.
 The size of an array must be specified by an int value and not long
or short.
 The direct superclass of an array type is Object.

Array can contain primitives’ data types as well as objects of a


class depending on the definition of array. In case of primitives
data types, the actual values are stored in contiguous memory
locations. In case of objects of a class, the actual objects are stored
in heap segment.
Creating, Initializing, and Accessing an Array
One-Dimensional Arrays :
The general form of a one-dimensional array declaration is
Syntax
dataType[] arrayRefVar; // preferred way.
or
dataType arrayRefVar[]; // works but not preferred way.
Example
The following code snippets are examples of this syntax −
int[] myList; // preferred way.
or
int myList[]; // works but not preferred way.

Although the above first declaration establishes the fact that myList is an
array variable, no array actually exists. It simply tells to the compiler that
this (myList) variable will hold an array of the integer type. To link myList
with an actual, physical array of integers, you must allocate one
using new and assign it to myList.

Instantiating an Array in Java

When an array is declared, only a reference of array is created. To actually


create or give memory to array, you create an array like this: The general
form of new as it applies to one-dimensional arrays appears as follows:

var-name = new type [size];


Example:
int myList[]; //declaring array
myList = new int[20]; // allocating memory to ar-
ray

Note
The elements in the array allocated by new will automatically be
initialized to zero(for numeric types), false (for boolean),
or null (for reference types).
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
dataType[] arrayRefVar = new dataType[arraySize];

int [] myList = new int[20];


or

dataType arrayRefVar[] = new dataType[arraySize];

int myList[] = new int[20];

Alternatively you can create arrays as follows −


dataType[] arrayRefVar = {value0, value1, ..., valuek};
Example:

int [] myList ={10,20,30,40,50};


Example
import [Link];
public class ArrayDemo1
{
public static void main(String[] args)
{
Scanner sr =new Scanner([Link]);
int []num=new int[10];
int max=0,i;
for(i=0;i<[Link];i++)
{
[Link]("Enter Value Into array ");
num[i]=[Link]();
}
max=num[0];
for(i=0;i<[Link];i++)
{
[Link](num[i]);
if(max<num[i])
{
max=num[i];
}
}
[Link]("Maximum= "+max);
}
}

Multidimensional array in java


In such case, data is stored in row and column-based index
(also known as matrix form).
Syntax to Declare Multidimensional Array in java

[Link][][] arrayRefVar;
[Link] arrayRefVar[][];
Example:

int num[][];
or
int [][] num;
num=new int[3][4];

Example:

Data_Type[][] Array_Name = new int[Row_Size][Column_Size];

int [][] num=new int[3][4];

import [Link];
public class ArrayDemo1
{
public static void main(String[] args)
{
Scanner sr =new Scanner([Link]);
int num[][]=new int[3][4];
int i,j;
for(i=0;i<[Link];i++)
{

for(j=0;j<num[i].length;j++)
{
[Link]("Enter Value Into array ");
num[i][j]=[Link]();
}
[Link]();
}

for(i=0;i<[Link];i++)
{

for(j=0;j<num[i].length;j++)
{
[Link](num[i][j]);
}
[Link]();
}
}
}

Jagged Array in Java


Jagged array is array of arrays such that member arrays can be of different
sizes, i.e., we can create a 2-D arrays but with variable number of columns
in each row. These type of arrays are also known as Jagged arrays.

Following are Java programs to demonstrate the above concept.

Example:
import [Link];
public class DigitSum {
public static void main(String[] args)
{
Scanner sr =new Scanner([Link]);
// Declaring 2-D array with 4 rows
int num[][] = new int[3][];
int i,j,colsize;
for(i=0;i<[Link];i++)
{
[Link]("Enter Column size for Row "+ (i+1)+"
:");
colsize=[Link]();
num[i]=new int[colsize];
}
for(i=0;i<[Link];i++)
{
for(j=0;j<num[i].length;j++)
{
[Link]("Enter Value Into array ");
num[i][j]=[Link]();
}
[Link]();
}

for(i=0;i<[Link];i++)
{
for(j=0;j<num[i].length;j++)
{
[Link]("["+num[i][j]+"]"+" ");
}
[Link]();
}
}
}

Enter Column size for Row 1 :


5
Enter Column size for Row 2 :
4
Enter Column size for Row 3 :
6
Enter Value Into array
1
Enter Value Into array
2
Enter Value Into array
3
Enter Value Into array
4
Enter Value Into array
5

Enter Value Into array


9
Enter Value Into array
8
Enter Value Into array
7
Enter Value Into array
6

Enter Value Into array


4
Enter Value Into array
5
Enter Value Into array
6
Enter Value Into array
7
Enter Value Into array
8
Enter Value Into array
9

[1] [2] [3] [4] [5]


[9] [8] [7] [6]
[4] [5] [6] [7] [8] [9]

For-each loop in Java


For-each is another array traversing technique like for loop,
while loop, do-while loop introduced in Java5.

 It starts with the keyword for like a normal for-loop.


 Instead of declaring and initializing a loop counter variable, you
declare a variable that is the same type as the base type of the
array, followed by a colon, which is then followed by the array
name.
 In the loop body, you can use the loop variable you created rather
than using an indexed array element.
 It’s commonly used to iterate over an array or a Collections class
(eg, ArrayList)

Syntax:
for (type var : array)
{
statements using var;
}

Example1
public class ForEachDemo
{
public static void main(String[] args)
{
int num[]={156,925,630,956,498,89,567,452};
int max=num[0];
for( int n:num)
{
if(max<n)
{
max=n;
}
[Link]("["+n+"]"+" ");
}
[Link]("Maximum Number= "+max);
}
}

Output:

[156] [925] [630] [956] [498] [89] [567] [452] Maximum Number= 956
Example2
public class ForEachDemo
{
public static void main(String[] args)
{
int num[][]={{1,2,3},{4,5,6},{7,8,9}};
for( int n[]:num)
{
for(int arr:n)
{
[Link]("["+arr+"]");
}
[Link]();
}

}
}

Output:
[1][2][3]
[4][5][6]
[7][8][9]

Example3
public class ForEachDemo
{
public static void main(String[] args)
{
String str="KUKRETI";
char tch[]=[Link]();
for( char ch:tch)
{
[Link](ch);
}
}
}

Output:

K
U
K
R
E
T
I

Example4
public class ForEachDemo
{
public static void main(String[] args)
{

String str[]={"SANJEEV","KUKRETI","WORLD"};
for(String nstr:str)
{
char ch[]=[Link]();
for( char nch:ch)
{
[Link](nch);
}
[Link]();
}

}
}

Output:
SANJEEV
KUKRETI
WORLD

You might also like