




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Example: Array of Strings. String[ ] greatCities = new String[5]; greatCities[2] = new String( "Paris" ); greatCities[0] = "Tokyo"; greatCities[4] = "Beltsville"; greatCities[1] = greatCities[2]; int k = 4; int x = greatCities[k].length( ); char c = greatCities[2].charAt( 2 );
Example: Array of Strings String[ ] moreCities = { "New York", "Boston", "Kathmandu" }; Example: Initializing using a non-constant expression. String[ ] cityState = { moreCities[0] + ", NY", moreCities[1] + ", MA" }; Example: Array of Dates (constructor is given month, day, year) Date[ ] birthDays = { new Date( 2, 12, 1809 ), new Date( 2, 11, 1731 ) };
public static void main( String[ ] args ) … public: externally visible. static: not associated with any one particular object instance. void: returns no type. String[ ] args: is called with an array of String command-line arguments. What are these arguments?
% emacs fooBar.java
private String addr: email address private String body: email body
private EmailMessage[ ] msgs: Array of Email messages private int nMsgs: Number of active messages
EmailMessage( String a, String b ): Standard constructor is given the address a and body b. EmailMessage( EmailMessage e): Copy constructor is given an email message e. String toString( ): Converts to string. From: Body: [Hi Everybody]
public MailManager( int max ) : Constructor is given the maximum number of emails allowed. It allocates the array for the messages and sets the current number of messages to 0. msgs = new EmailMessage[max]; nMsgs = 0; public boolean isFull( ) : This tests whether the email array is full. return ( nMsgs >= msgs.length ); public boolean addMsg( EmailMessage m ) : Adds a given message to the end of the list (nMsgs) and increments the size (nMsgs++). If the list is full, it returns false, and otherwise it returns true. if ( isFull( ) ) return false; msgs[nMsgs++] = new EmailMessage( m ); return true;
public class MailManager { private EmailMessage[ ] msgs; // the messages private int nMsgs; // current number of messages
public MailManager( int max ) { msgs = new EmailMessage[max]; nMsgs = 0; } public boolean isFull( ) { return ( nMsgs >= msgs.length ); } public boolean addMsg( EmailMessage m ) { if ( isFull( ) ) return false; msgs[nMsgs++] = new EmailMessage( m ); return true; } // … more to come … }
public boolean deleteMsg( int d ) : This deletes a single email at index d. If d is out of range, we return false. Otherwise, we eliminate the entry by sliding the subsequent emails down by one. for ( int j = d+1; j < nMsgs; j++ ) msgs[j-1] = msgs[j]; nMsgs--; public void clear( ) : Clears the entire list. for ( int i = 0; i < nMsgs; i++ ) msgs[i] = null; nMsgs = 0;
public class MailManager { private EmailMessage[ ] msgs; // the messages private int nMsgs; // current number of messages // … prior methods omitted … public String toString( ) { String result = "Mailbox: "; if ( nMsgs == 0 ) return result + "empty"; for ( int i = 0; i < nMsgs; i++ ) result = result + "\n " + msgs[i]; return result; } public EmailMessage[ ] getMessages( ) { EmailMessage[ ] result = new EmailMessage[nMsgs]; for ( int i = 0; i < nMsgs; i++ ) { result[i] = new EmailMessage( msgs[i] ); } return result; } }
public static void main( String[ ] args ) { MailManager myMail = new MailManager( 5 ); myMail.addMsg( new EmailMessage( "[email protected]", "Hi Everybody" ) ); myMail.addMsg( new EmailMessage( "[email protected]", "I hate spam" ) ); System.out.println( myMail ); myMail.addMsg( new EmailMessage( "[email protected]", "Me too" ) ); myMail.deleteMsg( 0 ); System.out.println( myMail ); myMail.clear( ); System.out.println( myMail ); } Mailbox: From: Body: [Hi Everybody] From: Body: [I hate Mailbox: From: Body: [I hate spam] From: Body: [Me too] Mailbox: empty
EmailMessage[ ] myMessages = myMail.getMessages( ); EmailMessage[ ] result = new EmailMessage[nMsgs]; for ( int i = 0; i < nMsgs; i++ ) result[i] = new EmailMessage( msgs[i] );
result = msgs; // Shallow copy! Dangerous return result; There are two problems with this: Minor problem: Gives the entire array, not just the current messages. Major problem: We give the class user a pointer directly to our private data (msgs). Now they can do whatever they want to it! This is called a privacy leak: private data is directly accessible to the outside world.
EmailMessage[ ] result = new EmailMessage[nMsgs]; for ( int i = 0; i < nMsgs; i++ ) result[i] = msgs[i]; // Shallow: Copies a pointer to msgs[i] Not always bad: It is harmless if it is not possible to change the underlying objects (e.g., String). Otherwise it is harmful. Classes that cannot be changed after creation are immutable.