Use
ArrayList<CustomClass>
instead of simple array(s). It should be more powerful and flexible to manage data then arrays.
import java.lang.Math;
import java.util.ArrayList;
public class ManagementSystem
{
public static void main(String[] args)
{
ArrayList<Contact> contacts = new ArrayList<Contact>();
Contact c = new Contact("Maciej");
c.setContactNumber(contacts.size()+1);
System.out.print(c + "\n");
contacts.add(c);
c = new Contact("Raja");
c.setContactNumber(contacts.size()+1);
System.out.print(c + "\n");
contacts.add(c);
contacts.remove(c);
System.out.print("Contact 'Raja' has been removed \n");
System.out.print("List of Contacts contains: " + contacts.size() + " element(s)\n");
}
}
public class Contact
{
private String sName = "";
private int iNumber = 0;
public Contact(String _Name)
{
sName = _Name;
}
public void setContactNumber(int _Number){
iNumber = _Number;
}
public String toString()
{
return iNumber + " - " + sName;
}
}
Returns the following output:
1 - Maciej
2 - Raja
Contact 'Raja' has been removed
List of Contacts contains: 1 element(s)
You can try above code on:
http://www.compilejava.net/[
^]
For further information, please see:
ArayList[
^]
Classes[
^]