Title: Arrays and Strings
1Arrays and Strings
2Declaring an Array Variable
- Here are two version of the same declaration
- int intlist
- int intlist
- Declaring a two-dimensional array
- int twodimarray
- int twodimarray
3Creating Space for an Array
- Given the declaration
- int myarray
- You must also tell the VM to create space
- myarray new int100
- You can do both steps in one line
- int myarray new int100
- You can also wait until runtime to decide how
much space to use - int myarray new intcomputeSize()
4Initializing Array Values
- Version One
- int one_to_four new int 1,2,3,4
- Version Two
- int one_to_four 1,2,3,4
- An Array of Strings
- String names "Bob","Joe","Sue","Fred"
- Computing Initial Values at Runtime
- int examscores gradeExam(1),gradeExam(2)
5Using Arrays
- Array Variables are Objects. So, they have their
own class methods and instance properties. For
example - myarray.length
- Arrays.sort(myarray)
- Array indexes must be of type int.
- Using an index out of bounds throws
ArrayIndexOutOfBoundsException
6Copying Arrays
- Shallow Copy
- int original 1,2,3,4
- int copy (int) original.clone()
- Better copy method
- System.arraycopy (original, 0,
- copy, 0,
- original.length)
7class array_test_1 static void main(String
args) char msg 'B','o','b'
char copy1 msg char copy2 (char)
msg.clone() copy10 'b' copy22
'p' System.out.print ("msg ")
System.out.println(msg) System.out.print
("copy1 ") System.out.println(copy1)
System.out.print ("copy2 ")
System.out.println(copy2)
msg bob copy1 bob copy2 Bop
8class array_test_2 static void main(String
args) char msg 'B','o','b'
char copy1 msg char copy2 (char)
msg.clone() if (msg copy1)
System.out.println ("msg equals copy1")
else System.out.println ("msg not equals
copy1") if (copy1 copy2)
System.out.println ("copy1 equals copy2")
else System.out.println ("copy1 not equals
copy2")
msg equals copy1 copy1 not equals copy2
9Array Methods
- import java.util.Arrays
- // compare the contents of two arrays
- if (Arrays.equals (array1, array2))
- System.out.println ("contents are the same")
- // quick sort an array
- Arrays.sort (myarray)
- // initialize the first ten positions with a 1
- Arrays.fill (myarray, 0, 9, 1)
- // fill entire array with value of 1
- Arrays.fill (myarray, 1)
10char ! String
- The "" operator is overloaded for the String
class, so this code does not run correctly. - class string_test_1
-
- static void main(String args)
-
- char msg 'B','o','b'
- System.out.println ("msg " msg)
-
-
- Correct Version
- System.out.println("msg " String.valueof(msg)
11char ! String
- This code yields a compiler error.
- class string_test_2
-
- static void main(String args)
-
- String myvar "bob"
- myvar0 'B' // error myvar is not an
array -
-
12String Methods
- Compare two strings
- String name1, name2
- ...
- if ( name1.compareTo(name2) lt 0 ) // like C
- Convert an integer into a string
- String mystring String.valueOf (myint)
- Convert a string into an integer
- int myint Integer.parseInt (mystring)
- Append
- name " "
- name first last
13More String Methods
- Extract a Substring
- String s1 name.substring(0, 4)
- Remove spaces at the end
- name name.trim()
- Does the string contain a number?
- String str1
- ...
- char c str1.charAt (0) // get first char
- if (Character.isDigit(c)) // test that char
- System.out.print ("str1 starts with a number")
14Files
- See course web page for example code