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

Java p6

This document discusses arrays in Java. It defines an array as a collection of similar data types that can be referred to by a common name. Arrays allow storing multiple values of a single type. The document provides examples of declaring different types of one-dimensional and two-dimensional arrays in Java, including arrays of primitives, objects, and generic types. It also demonstrates initializing and accessing array elements.

Uploaded by

Khan.ali
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)
17 views

Java p6

This document discusses arrays in Java. It defines an array as a collection of similar data types that can be referred to by a common name. Arrays allow storing multiple values of a single type. The document provides examples of declaring different types of one-dimensional and two-dimensional arrays in Java, including arrays of primitives, objects, and generic types. It also demonstrates initializing and accessing array elements.

Uploaded by

Khan.ali
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/ 4

Practical 06:- Write a program to illustrate the type of array in java.

Introduction

Array in java:
An array is a collection of similar types of data. It is a group of like-typed variables
referred to by a common name. Arrays in Java work differently than they do in C/C++.
For example, if we want to store the names of 100 people then we can create an array of
the string type that can store 100 names.
String[] array = new String[100];
Here, the above array cannot store more than 100 names. The number of values in a Java
array is always fixed.

In Java, here is how we can declare an array.


dataType[] arrayName;
• dataType – it can be primitive data types like int, char, double, byte, etc. or Java
objects
• arrayName – it is an identifier

For example,
Double[] data;
Here, data is an array that can hold values of type double.
Here are Some of key features of array in Java:-
• In Java, all arrays are dynamically allocated.
• Arrays are stored in contiguous memory [consecutive memory locations].
• Since arrays are objects in Java, we can find their length using the object property
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 has an index beginning with 0.
• Java array can also be used as a static field, a local variable, or a method parameter.
• The size of an array must be specified by int or short value and not long.
//Java program to implement array.
class ImplementArray{
int a;
int b;
ImplementArray(int a, int b){
this.a=a;
this.b=b;
}
}
public class Array{ //main class~.java file
public static void main(String[] args){
int i,j;
int[] b =new int[5];
b[0]=1;
b[1]=2;
int[] ar={2,3,5,7,8};
short[] ars={7,6,4,8};
float[] arf={1.3f,0.3f,3.14f,1.6f};
char[] arc={'J','A','V','A'};
String[] arst={"Programming","in","Java","is Fun."};
int[][] ar2d={{1,2,3},{4,5,6},{7,8,9}};

System.out.println("\n1-D array of integer type.");


for(i=0;i<ar.length;i++){
System.out.println(ar[i]);
}
System.out.println("\n1-D array of Short type.");
for(i=0;i<ars.length;i++){
System.out.println(ars[i]);
}
System.out.println("\n1-D array of float type.");
for(i=0;i<arf.length;i++){
System.out.println(arf[i]);
}
System.out.println("\n1-D array of char type.");
for(i=0;i<arc.length;i++){
System.out.println(arc[i]);
}
System.out.println("\n1-D array of String type.");
for(i=0;i<arst.length;i++){
System.out.print(arst[i]+" ");
}
System.out.println("\n\n2-D array of integer type.");
for(i=0;i<ar2d.length;i++){
for(j=0;j<ar2d.length;j++){
System.out.print(ar2d[i][j]);
}
System.out.println(" ");
}
//Array of Objects
ImplementArray obj1=new ImplementArray(4,5);
ImplementArray obj2=new ImplementArray(7,8);
ImplementArray[] s=new ImplementArray[2];
s[0]=obj1; //assigning one object to array of Objects
s[1]=obj2;

System.out.println("Element of 1st obj="+s[0].a+" and "+s[0].b);


System.out.println("Element of 2st obj="+s[1].a+" and "+s[1].b);
}
}
//Output
1-D array of integer type.
2
3
5
7
8
1-D array of Short type.
7
6
4
8
1-D array of float type.
1.3
0.3
3.14
1.6
1-D array of char type.
J
A
V
A
1-D array of String type.
Programming in Java is Fun.
2-D array of integer type.
123
456
789
Element of 1st obj=4 and 5
Element of 2nd obj=7 and 8
*/

You might also like