Rutgers University ECE 202 - Lecture 4 Strings and Arrays (12 pages)

Previewing pages 1, 2, 3, 4 of 12 page document View the full content.
View Full Document

Lecture 4 Strings and Arrays



Previewing pages 1, 2, 3, 4 of actual document.

View the full content.
View Full Document
View Full Document

Lecture 4 Strings and Arrays

46 views


Pages:
12
School:
Rutgers University- The State University of New Jersey
Course:
Ece 202 - Discrete Mathematics
Unformatted text preview:

Lecture 4 Strings and Arrays Strings Objects which encapsulate a fixed number of character values stored in a particular order String class predefined in the Java Development Kit JDK in the java lang package The java lang package is loaded by default no import statement is needed Characters that can appear in Strings Letters in any alphabet abc Digits 123 123 Special characters Escape sequences n t b Note It is character codes that are stored in memory 1 Strings in memory The simplified view is that a String occupies a contiguous area in the memory in which each character occupies a location s A B C D String s ABCD The reality is that the String variable is a separate entity which contains a reference to the memory area that contains the characters s A B C D When passing Strings as parameters to methods it is the reference that is passed PassParam java vs equals The operation checks whether the references are equal i e whether the two strings refer to the same location of memory The methods String equals checks whether two Strings have the same content i e the references are to areas in the memory that contain the same sequences of characters String equalsIgnoreCase String s1 new String ABCD String s2 s1 String s3 new String s1 s1 A B C D s2 s3 A B C D 2 Undefined null If a String is declared but not defined it contains an undefined reference String s Cannot be used without a value assigned s null indicates an explicit reference to nothing s s is the empty String String literal constants Sequences of characters that appear between double quotes The compiler creates a constant pool where it stores all the constants that appear in a program not only Strings but other types too s1 String s1 ABCD String s2 AB CD String s3 A BCD String s4 new String ABCD s2 Constant pool A B C D s3 s4 A B C D 3 Intern ing Strings Strings created at run time following String operations are not in the constant pool Consequence You need to use equals rather than for comparisons to other Strings A new String can be added to the constant pool with intern Advantage in the future can be used for comparisons this can increase efficiency if many comparisons are needed Note intern uses equals to check if the String is already in the pool if many new Strings appear intern ing them may reduce efficiency Access to String characters The length of a String i e number of characters is given by String length The characters of a String are indexed with values between 0 and length 1 Individual characters of a String can be read with String charAt int index String s ABCD for int i 0 i s length 1 i c out print s charAt i StringIndexOutOfBoundsException Access java BadAccess java A B C D 0 1 2 3 Output ABCD 4 Common operations on Strings Lexicographic comparison int String compareTo String other Lookup int String indexOf int ch int String indexOf int ch int fromIndex int String indexOf String other int String indexOf String other int fromIndex They return the index in the String of the character or String that is looked up or 1 if there is no match lastIndex searches backwards startsWith endsWith are more specific and return boolean Lookup java String immutability Strings are immutable their content cannot be changed Operations that apparently change String content in fact create new Strings String s new String ABCD s A B C D s s EF s s references a different memory area A B C D A B C D E F 5 Changing String content String s abcdef s A s substring 1 s s substring 0 s length 1 F s s substring 0 2 C s substring 3 s s replace e m s s concat t s s trim s s toLowerCase s s toUpperCase abcdef Abcdef AbcdeF AbCdeF AbCdmF AbCdmFt AbCdmFt abcdmft ABCDMFT PassParam java methods that change String content should be functions ExtractSubstrings java Changing String content The actual content does not change The apparent change in content is in fact a change of the reference Methods that change String content should be functions that return a String reference PassParam java passing Strings as parameters to methods ChangeString java test methods that change reverse Strings 6 Other useful tools StringTokenizer identifies tokens substring in String based on delimiters countTokens hasMoreTokens nextToken Conversion to array of characters toCharArray getChars getBytes Conversion from array of characters copyValueOf StringBuffer and efficiency It is inefficient to repeatedly change String content by creating new Strings StringBuffer comes to rescue Its constructor can take a String to make a copy of the String to be modified StringBuffer has insert and append methods StringBuffer toString returns a String when changes are done Ex Put the alphabet in a String MillionChars java 7 Conversions to String byte b 1 short s 2 int i 3 long l 4L float f 1 234F double d 4 321 char c A String bstr b String sstr s String istr i String fstr f String dstr d String cstr c String bstr new Byte b toString String sstr new Short s toString String istr new Integer i toString String lstr new Long l toString String fstr new Float f toString String dstr new Double d toString String cstr new Character c toString For classes of objects the toString method needs to be defined to allow concatenation to a String Pt p new Pt 100 100 c out println Point p p Conversions from String These conversions are also possible thanks to class methods provided by the wrapper classes String bstr 12 String sstr 123 String istr 123456 String lstr 123456789L String fstr 1 234F String dstr 4 321 String cstr A byte b Byte parseByte bstr short s Short parseShort sstr int i Integer parseInt istr long l Long parseLong lstr float f Float parseFloat fstr double d Double parseDoubel dstr char c cstr charAt 0 Warning all the parse class methods of the wrapper classes can fail with a NumberFormatException if the string that is being converted does not constitute a valid number 8 Introduction to arrays Typically used when you need to store and process uniformly a number of values objects Ex MonthTable java TestArrays java Arrays can be Declared int a int a memory allocated for the reference variable Defined int a new int 4 Memory also allocated for the array the size is set Values in the array set to zero Initialized int b 1 2 3 4 Memory allocated and values set for each element of the array What s in the array An array of primitives contains values a int a 1 2 3 4 int b a 1 2 3 4 b a b c d f o u r An array of objects contains references to the objects String s abcd four s When an array is passed


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Lecture 4 Strings and Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 4 Strings and Arrays and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?