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

module4

Java arrays are objects that store elements of the same data type in contiguous memory locations, allowing for fixed-size storage and index-based access. They can be single-dimensional or multi-dimensional, and Java provides features like anonymous arrays and jagged arrays, which allow for variable column sizes in 2D arrays. While arrays offer advantages like code optimization and random access, they have the limitation of fixed size, which can be addressed using Java's collection framework.

Uploaded by

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

module4

Java arrays are objects that store elements of the same data type in contiguous memory locations, allowing for fixed-size storage and index-based access. They can be single-dimensional or multi-dimensional, and Java provides features like anonymous arrays and jagged arrays, which allow for variable column sizes in 2D arrays. While arrays offer advantages like code optimization and random access, they have the limitation of fixed size, which can be addressed using Java's collection framework.

Uploaded by

mrpctechnologies
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
You are on page 1/ 11

Java Arrays

Normally, an array is a collection of similar type of elements which has contiguous


memory location.

Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure
where we store similar elements. We can store only a fixed set of elements in a Java
array.

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.

Unlike C/C++, we can get the length of the array using the length member. In C/C++, we
need to use the sizeof operator.

In Java, array is an object of a dynamically generated class. Java array inherits the Object
class, and implements the Serializable as well as Cloneable interfaces. We can store
primitive values or objects in an array in Java. Like C/C++, we can also create single
dimentional or multidimentional arrays in Java.

Moreover, Java provides the feature of anonymous arrays which is not available in C/C+
+.

Advantages
o Code Optimization: It makes the code optimized, we can retrieve or sort the
data efficiently.
o Random access: We can get any data located at an index position.

Disadvantages
o Size Limit: We can store only the fixed size of elements in the array. It doesn't
grow its size at runtime. To solve this problem, collection framework is used in
Java which grows automatically.

https://www.javatpoint.com/array-in-java

Array Declarations in Java (Single and


Multidimensional)
Arrays in Java | Introduction
One Dimensional Array :
It is a collection of variables of same type which is used by a common name.
Examples:
One dimensional array declaration of variable:

import java.io.*;

class GFG {

public static void main(String[] args)

int[] a; // valid declaration

int b[]; // valid declaration

int[] c; // valid declaration

We can write it in any way.

Now, if you declare your array like below:

import java.io.*;

class GFG {

public static void main(String[] args)

// invalid declaration -- If we want to assign

// size of array at the declaration time, it

// gives compile time error.

int a[5];

// valid declaration

int b[];

}
Now, suppose we want to write multiple declaration of array variable then we can
use it like this.

import java.io.*;

class GFG {

public static void main(String[] args)

// valid declaration, both arrays are

// one dimensional array.

int a[], b[];

// invalid declaration

int c[], [] d;

// invalid declaration

int[] e, [] f;

When we are declaring multiple variable of same time at a time, we have to write
variable first then declare that variable except first variable declaration. There is no
restriction for the first variable.
Now, when we are creating array it is mandatory to pass the size of array; otherwise
we will get compile time error.
You can use new operator for creating an array.

import java.io.*;

class GFG {

public static void main(String[] args)

// invalid, here size of array is not given

int[] a = new int[];

// valid, here creating 'b' array of size 5


int[] b = new int[5];

// valid

int[] c = new int[0];

// gives runtime error

int[] d = new int[-1];

Printing array :

/* A complete Java program to demonstrate working

of one dimensional arrays */

class oneDimensionalArray {

public static void main(String args[])

int[] a; // one dimensional array declaration

a = new int[3]; // creating array of size 3

for (int i = 0; i < 3; i++) {

a[i] = 100;

System.out.println(a[i]);

Output:
100
100
100
Two Dimensional Array
Suppose, you want to create two dimensional array of int type data. So you can
declare two dimensional array in many of the following ways:
// Java program to demonstrate different ways

// to create two dimensional array.

import java.io.*;

class GFG {

public static void main(String[] args)

int a[][]; // valid

int[][] b; // valid

int[][] c; // valid

int[] d[]; // valid

int[][] e; // valid

int[] f[]; // valid

[][] int g; // invalid

[] int[] h; // invalid

Now, Suppose we want to write multiple declarations of array variable then you can
use it like this.

// Java program to demonstrate multiple declarations

// of array variable

import java.io.*;

class GFG {

public static void main(String[] args)

// Here, 'a' is two dimensional array, 'b'

// is two dimensional array

int[] a[], b[];

// Here, 'c' is two dimensional array, 'd'

// is two dimensional array

int[] c[], d[];


// Here, 'e' is two dimensional array, 'f'

// is three dimensional array

int[][] e, f[];

// Here, 'g' is two dimensional array,

// 'h' is one dimensional array

int[] g[], h;

https://www.google.com/search?ei=Suf6XqfpJdmF4-
EPy4G3qAw&q=one+dimensional+array+in+java&oq=one+dimensional+array+in+&gs_lcp
=CgZwc3ktYWIQARgBMgQIABBDMgIIADICCAAyAggAMgIIADICCAAyAggAMgIIA
DICCAAyAggAOgQIABBHUIsFWIsFYPwUaABwAXgAgAGjAYgBowGSAQMwLjGYA
QCgAQGqAQdnd3Mtd2l6&sclient=psy-ab
How 3D Arrays are Defined in Java?
Java uses a very simple way to define the arrays. Square brackets

(‘[ ]’) are used to define the array object after the data type of

array. One needs to define the size at the time of the declaration

of the array. 3D arrays are defined with three brackets. Below

given is the syntax of defining the 3D arrays in Java:

Data_type array_name[ ] [ ] [ ] = new array_name[a][b]

[c];

 Here data_type: data type of elements that will be stored in

the array. array_name: name of the array

 new: keyword to create an object in Java

 a, b, c: holds the numeric values for the various dimensions.


Syntax:

int [ ] [ ] [ ] arr = new int [10][4][3];

In the above example, there can be a maximum of 10x4x3 = 120

elements stored by the array ‘arr’.

How to Create 3D Arrays and Insert


values in them in Java?
Creating 3D arrays in Java is as simple as creating 1D and 2D

arrays. As mentioned above, it is important to define the size of

an array at the time of declaration. Creating 3D arrays involves

one more step of passing/ entering values in them in the form of

an array of 2D arrays. We can define the size of an array and can

insert/ enter the values afterward or we can directly pass the

values in an array. So the manner of value defined in 3D arrays is

given below:

Syntax

data_type[][][] arr_name =

{
{Array1Row1Col1,Array1Row1Col2,....},

{Array1Row2Col1, Array1Row2Col2, ....}

},

{Array2Row1Col1, Array2Row1Col2, ....},

{Array2Row2Col1, Array2Row2Col2, ....}

Code

int num_array [ ] [ ] [ ] = {

{10 ,20 ,99},

{30 ,40 ,88}

},

{50 ,60 ,77},

{80 ,70 ,66}


},

};

https://www.educba.com/3d-arrays-in-java/
A command-line argument is an information that directly follows the program's name
on the command line when it is executed. To access the command-line arguments
inside a Java program is quite easy. They are stored as strings in the String array
passed to main( ).

Example
The following program displays all of the command-line arguments that it is called
with -

public class CommandLine {

public static void main(String args[]) {

for(int i = 0; i<args.length; i++) {

System.out.println("args[" + i + "]: " + args[i]);

Try executing this program as shown here -

$java CommandLine this is a command line 200 -100


Output
This will produce the following result -

args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100
http://tutorialspoint.com/Java-command-line-arguments#:~:text=A%20command-line
%20argument%20is,array%20passed%20to%20main(%20).
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.

// Program to demonstrate 2-D jagged array in Java

class Main

public static void main(String[] args)

// Declaring 2-D array with 2 rows

int arr[][] = new int[2][];

// Making the above array Jagged

// First row has 3 columns

arr[0] = new int[3];

// Second row has 2 columns

arr[1] = new int[2];

// Initializing array

int count = 0;

for (int i=0; i<arr.length; i++)

for(int j=0; j<arr[i].length; j++)

arr[i][j] = count++;

// Displaying the values of 2D Jagged array

System.out.println("Contents of 2D Jagged Array");

for (int i=0; i<arr.length; i++)

for (int j=0; j<arr[i].length; j++)

System.out.print(arr[i][j] + " ");

System.out.println();
}

Output:
Contents of 2D Jagged Array
0 1 2
3 4
https://www.geeksforgeeks.org/jagged-array-in-java/

You might also like