In: Computer Science
Create a program that keeps track of the following information input by the user: First Name, Last Name, Phone Number, Age Now - let's store this in a multidimensional array that will hold 10 of these contacts. So our multidimensional array will need to be 10 rows and 4 columns. You should be able to add, display and remove contacts in the array. In Java
Arrays can contain only one type of data so contact cannot be stored in array because fname is string and age is int . Better is to create contact class and store contacts in 1d array like this
import java.util.*;
class Contact
{
String fname, lname;
int pno, age;
Contact(String f, String l, int p, int a)
{
fname= f;
lname=l;
pno = p;
age=a;
}
public String toString()
{
return fname+" "+lname+" "+pno+" "+age;
}
}
public class ContactDemo
{
static void removeContact(Contact contacts[], Contact c )
{
for(int i=0; i<contacts.length;i++)
{
if(contacts[i].equals(c))
{
contacts[i]=null;
break;
}
}
}
static void displayContacts(Contact c[])
{
for(int i=0; i<c.length;i++)
{
if(c[i]!=null)
{
System.out.println(c[i]);
}
}
}
public static void main(String a[])
{
Contact[] contacts = new Contact[10];
contacts[0]=new Contact("Asif","Khan",986788998,20);
contacts[1]=new Contact("Iqbal","Khan",986788998,20);
contacts[2]=new Contact("Junaid","Khan",986788998,20);
contacts[3]=new Contact("Naseer","Khan",986788998,20);
removeContact(contacts, contacts[2]);
displayContacts(contacts);
}
}