Single Dimention Array Definition:
Create a package output_pack and a class Output under that
A list of the items can be given one
variable name using only one subscript and such a variable is called one
dimensional array.
Syntax:
Type variable_name[size];
Single Dimensional Array simple program :
Create a package emp_pack and a class Employee under that
package emp_pack;
import output_pack.*;
import details_pack.*;
public class Employee {
public static void main(String args[])
{
Details arr[] = new Details[3];
arr[0] = new Details (1, "Prem Krishnan","MVC");
arr[1] = new Details (2, "Parvathy Krishnan",
"MMC");
arr[2] = new Details (3, "Karthick Prabhu",
"SEZ");
new Output().display(arr);
}
}
Create a package details_pack and a class Details under that
package details_pack;
public class Details {
private int id;
private String name;
private String address;
public Details (int num, String name, String addr )
{
id =
num;
this.name = name;
address =
addr;
}
public String toString()
{
return "Id is :" +id + "\nName is: " +name + "\nAddress is: " +address;
}
}
package output_pack;
import details_pack.*;
public class Output
{
public void display(Details array[]){
for(int i = 0; i < array.length;
i++){
System.out.println(array[i]);
}
}
}
Output:-
Id is :1
Name is: Prem Krishnan
Address is: MVC
Id is :2
Name is: Parvathy Krishnan
Address is: MMC
Id is :3
Name is: Karthick Prabhu
Address is: SEZ
Explanation
No comments:
Post a Comment