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

Arrays in Java

Java arrays allow storing of multiple elements of the same type. Elements in an array are stored in contiguous memory locations. Arrays can be declared statically by specifying the element types and sizes, or dynamically by specifying just the type and letting the program determine the size. Multidimensional arrays, known as 2D arrays, allow storing of elements in a tabular format with rows and columns. 2D arrays can have a variable number of columns for each row. Arrays can be passed to and returned from methods in Java.

Uploaded by

Vani Mittal
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)
37 views

Arrays in Java

Java arrays allow storing of multiple elements of the same type. Elements in an array are stored in contiguous memory locations. Arrays can be declared statically by specifying the element types and sizes, or dynamically by specifying just the type and letting the program determine the size. Multidimensional arrays, known as 2D arrays, allow storing of elements in a tabular format with rows and columns. 2D arrays can have a variable number of columns for each row. Arrays can be passed to and returned from methods in Java.

Uploaded by

Vani Mittal
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/ 6

Arrays in Java

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.

Static array declaration

int arr [ ] = { 12, 34, 56 }

To find the length of an array. We use - arr. length. It will return value 3. Because a number of
elements in array arr is 3.

Another way to declare static array.

Int arr[] = new int[3]; --- integer array of size = 3.


arr[0] = 3;
arr[1] = 23; ………..
Example of assigning elements to an array by user input and then traversing elements of an array

import java.util.Scanner;
public class arraysdemo {
Scanner sc;
void input()
{ sc = new Scanner(System.in);
System.out.println("enter number of ages want to insert-->");
int size = sc.nextInt();
sc.nextLine();
int age[] = new int[size]; Dynamic size array
for(int i=0;i<age.length;i++) declares
{System.out.println("enter age-->");
age[i] = sc.nextInt(); Elements inserted into an array
sc.nextLine();
}
System.out.println("show ages-->");
for(int x: age) // for-each loop
{ System.out.println(x); Display elements of an array
} using for – each loop.
} Output –
public static void main(String args[]) enter number of ages want to insert-->
{ arraysdemo ob = new arraysdemo(); 2
ob.input(); enter age-->
}} 23
enter age-->
34
show ages-->
23
34
Example of passing of an array to a function and returning an array from
function
public class passingarray {
String output[];
String[] showArray(String names[])
{
output = new String[names.length];

int count=0;
for(String i : names)
{ Fetch one by one element of an array “names”
output[count] = i + " ADMIN"; and add “ADMIN” at the end of the each of the
count ++; element and store the result in the array name
} “output”.
return output;
} Array “output” returned.
public static void main(String args[])
{
String names[] = {"mohan", "ram", "Gopal"};
passingarray ref = new passingarray();
String show[] = ref.showArray(names); Passing array “names” to function “showArray()

for(String i: show)
{
System.out.println(i); Output –
}
} mohanADMIN
} ramADMIN
GopalADMIN
2D array Static declaration
Int arr [][] = new int [2][]; Comment –
Static declaration For a 2D matrix. Number of
arr [0] = new int [3]; columns may be vary for each row.
Int arr[][] = { {2,3,4}, arr [1] = new int [4];
{5,6,7}
}; row cols Int arr [][] = new int [][3]; Wrong
s declaration
Int arr [][] = new int [2][3];

arr [0][0] = 10; Tip


arr [0][1] = 12;
arr [0][2] = 22;
arr [1][0] = 11; Calculate no. of columns in 1st row?
arr [1][1] = 13;
arr [1][2] = 23; arr[0].length

Calculate no. of columns in 2nd row?

arr[1].length
import java.util.Scanner;
public class jaggyarray { Example of 2D matrix with variable no.’s of columns for each
Scanner s;
void input()
row
{
s = new Scanner(System.in);
int a[][] = new int[2][]; Dynamic array declares
a[0] = new int[3];
for(int i=0;i<a[0].length;i++) 3 columns declared for 1st
{ System.out.println("Enter value"); row
a[0][i] = s.nextInt(); Insert elements of 1st row.
}
a[1] = new int[5];
for(int i=0;i<a[1].length;i++) 5 columns declared for 2nd
{ System.out.println("Enter value"); row
a[1][i] = s.nextInt(); Insert elements of 2nd row.
}
System.out.println("Display elements of 2D array");
for(int i=0;i<a.length;i++)
{ for(int j=0;j<a[i].length;j++)
{
Traverse the elements of the
System.out.print(a[i][j]+" ");
2D array
}
System.out.println();
}
}
public static void main(String args[])
{ jaggyarray ob = new jaggyarray();
ob.input();
}
}

You might also like