Arrays and Strings - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays and Strings

Description:

Arrays and Strings CSCI 392 Adapted from Dannelly – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 13
Provided by: Stephe758
Category:

less

Transcript and Presenter's Notes

Title: Arrays and Strings


1
Arrays and Strings
  • CSCI 392
  • Adapted from Dannelly

2
Declaring an Array Variable
  • Two versions of the same declaration
  • int intlist
  • int intlist
  • Declaring a two-dimensional array
  • int twodimarray
  • int twodimarray

3
Creating 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 set the array
    size
  • int runtimeSize 100
  • int myarray new intruntimeSize

4
Initializing 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)

5
Using 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

6
Copying Arrays
  • copy method prior to JDK 1.6
  • System.arraycopy (original, 0,
  • copy, 0,
  • original.length)
  • Copy method JDK 1.6 or later
  • intArray2 Arrays.copyOf (intArray,
    intArray.length)

7
import java.util.Arrays public class Array_copy
public static void main(String args) int
intArray 1,2,3 int intArray2
4,5,6 int count 0 while (count ! 3)
System.out.println ("intArray "
intArraycount) count count 1
count 0 while (count ! 3)
System.out.println ("intArray2 "
intArray2count) count count 1
count 0 intArray2 Arrays.copyOf
(intArray, intArray.length) while (count !
3) System.out.println ("intArray2 "
intArray2count) count count 1

intArray 1 intArray 2 intArray 3 intArray2
4 intArray2 5 intArray2 6 intArray2 1 intArray2
2 intArray2 3
8
Array 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)

9
char ! String
  • public class samCopy2
  • public static void main(String args)
  • char copy1 't', 'e', 's','t'
  • char copy2 'b', 'e', 's', 't'
  • System.out.println ("copy1 "
    String.valueOf(copy1))
  • System.out.println ("copy2 " copy2)

copy1 test copy2 C_at_8f4fb3
10
char ! 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

11
String 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

12
More 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")
Write a Comment
User Comments (0)
About PowerShow.com