Tutoriallearn.com
Easy Learning
Search Tutoriallearn.com :
Note:
1. There is a difference in creating an array in Java than C/C++ language.
2. Difference is:
i. Memory allocation is needed after declaring an array in Java. Java new operator is used in allocating the memory.
ii. However, memory allocation may be done by initializing the array at the time of decelerating an array. In this case, no need of new operator. We shall discuss this case later.
public class arraydemo1 {
public static void main (String args[])
{
int roomno []; // No memory allocation
int i;
roomno = new int[3]; // memory is allocated to array
// Following, giving values to array
roomno[0] = 20;
roomno[1] = 30;
roomno[2] = 35;
// Following, displaying value of array elements
for (i=0;i<3;i++)
{
System.out.println("Room No. is "+ roomno[i]);
}
}
}
Room No. is 20 Room No. is 30 Room No. is 35
import java.util.Scanner;
public class arraydemo2 {
public static void main (String args[])
{
int roomno []; // No memory allocation
int i;
roomno = new int[3]; // memory is allocated to array
Scanner sc = new Scanner(System.in);
// Taking values from user
for(i=0;i<3;i++)
{
System.out.println("Enter your number: ");
int num = sc.nextInt();
roomno[i] = num;
}
// Following, displaying value of array elements
for (i=0;i<3;i++)
{
System.out.println("Room No. is "+ roomno[i]);
}
}
}
Enter your number: 22 Enter your number: 34 Enter your number: 56 Room No. is 22 Room No. is 34 Room No. is 56
import java.util.Scanner;
public class arraydemo3 {
public static void main (String args[])
{
float avar [][]; // No memory allocation
int i,j;
float num;
avar = new float [2][3]; // memory is allocated to array
Scanner sc = new Scanner(System.in);
// Taking values from user
for(i=0;i<2;i++)
{
for (j=0;j<3;j++)
{
System.out.println("Enter your number: ");
num = sc.nextFloat();
avar[i][j] = num;
}
}
// Displaying value of array elements
System.out.println("values of avar ");
for (i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
System.out.print(" "+ avar[i][j]);
}
System.out.println("");
}
}
}
Enter your number:10.5 Enter your number:11.3 Enter your number:23.0 Enter your number:45.5 Enter your number:22.7 Enter your number:6.3 values of avar 10.5 11.3 23.0 45.5 22.7 6.3
public class arraydemo4 {
public static void main (String args[])
{
int monthnos [] = {1,2,3,4,5,6,7,8,9,10,11,12}; // memory is allocated according to number of elements
int i;
// Displaying value of array elements
for (i=0;i<12;i++)
{
System.out.println("Month number is "+ monthnos[i]);
}
}
}
Month number is 1 Month number is 2 Month number is 3 Month number is 4 Month number is 5 Month number is 6 Month number is 7 Month number is 8 Month number is 9 Month number is 10 Month number is 11 Month number is 12
public class arraydemo4m {
public static void main (String args[])
{
// Multidimensional initialization
int avar[][] = {
{2, 3, 2*3},
{10, 23, 7},
{34, 23, 18},
};
int i,j;
// Displaying value of array elements
for (i=0;i<3;i++)
{
for (j=0;j<3;j++){
System.out.print(avar[i][j]+" ");
}
System.out.println();
}
}
}
2 3 6 10 23 7 34 23 18
import java.util.Scanner;
public class arraydemo5 {
public static void main (String args[])
{
char [] avar; // No memory allocation
int i; char symb;
avar = new char [3]; // memory is allocated to array
Scanner sc = new Scanner(System.in);
// Taking values from user
for(i=0;i<3;i++)
{
System.out.println("Enter your character: ");
symb = sc.next().charAt(0);
avar[i] = symb;
}
// Displaying value of array elements
for (i=0;i<3;i++)
{
System.out.println("Your entered character is "+ avar[i]);
}
}
}
Enter your character: a Enter your character: b Enter your character: c Your entered character is a Your entered character is b Your entered character is c
import java.util.Scanner;
public class arraydemo6 {
public static void main (String args[])
{
int avar[][] = new int [3][]; // 3 rows, and no value in second dimension
avar[0]= new int [2]; // 2 columns are allocated to first row
avar[1]= new int [3]; // 3 columns are allocated to second row
avar[2]= new int [4]; // 4 columns are allocated to third row
int i, num;
Scanner sc = new Scanner(System.in);
// Taking values from user
// For first row
for(i=0;i<2;i++)
{
System.out.println("Enter value of column of first row: ");
num = sc.nextInt();
avar[0][i] = num;
}
// For second row
for(i=0;i<3;i++)
{
System.out.println("Enter value of column of second row: ");
num = sc.nextInt();
avar[1][i] = num;
}
// For third row
for(i=0;i<4;i++)
{
System.out.println("Enter value of column of third row: ");
num = sc.nextInt();
avar[2][i] = num;
}
// Displaying value of array elements
// First row
for (i=0;i<2;i++)
{
System.out.println("Values of first row "+ avar[0][i]);
}
// Second row
for (i=0;i<3;i++)
{
System.out.println("Values of second row "+ avar[1][i]);
}
// Third row
for (i=0;i<4;i++)
{
System.out.println("Values of third row "+ avar[2][i]);
}
}
}
Enter value of column of first row: 20 Enter value of column of first row: 12 Enter value of column of second row: 23 Enter value of column of second row: 45 Enter value of column of second row: 67 Enter value of column of third row: 27 Enter value of column of third row: 78 Enter value of column of third row: 89 Enter value of column of third row: 76 Values of first row 20 Values of first row 12 Values of second row 23 Values of second row 45 Values of second row 67 Values of third row 27 Values of third row 78 Values of third row 89 Values of third row 76