Strings and Arrays

Team-Fly

Java supports the storage and manipulation of multiple values through arrays. Strings are simply arrays of characters (strings are actually classes of the java.lang package). Arrays are similar to internal tables in ABAP.

Arrays

Arrays in Java can be declared with the following general statement:

datatype[] arrayname = new datatype[arraysize];

This statement both declares the array and allocates memory for it. After the array has been declared, each element can be accessed through the use of the square brackets, as shown here:

int[] arrayInts = new int[10];     // 10 array elements int    x; arrayInts[0] = ‘0';    // 1st element = ‘0' arrayInts[1] = ‘1';    // 2nd element = ‘1' x = arrayInts.length;    // x is set to 10.
Note 

You can see in the preceding example that Java arrays are indexed starting at ‘0'. Also note that whenever you declare an array, regardless of type, the instance variable 'length' can be used to determine the overall length of the array.

Multidimensional arrays in Java are declared as follows:

int[][] multiArray = new int[10][10]; multiArray[0][0] = ‘456'; multiArray[9][9] = ‘123';

Strings

Java has two different classes for character arrays: String and StringBuffer. The String class holds strings that do not change after they have been initialized or set, whereas the StringBuffer class holds arrays of characters that do change. Usually, you will use the String class for constants and for passing character arrays to and from methods where the array will not be changed.

String newString = "Hi there";            // Allocates a simple constant StringBuffer newBuffer = new StringBuffer(10);   // Allocates  a 10 character                                // array

Methods for the String Class

Here are the most commonly used methods associated with the String class:

  • .length(). Returns the length (in characters) of the string.

  • .charAt(x). Returns the character of the string at position x.

  • .indexof(x). Returns the first index of the string that contains the character equal to the value passed in.

  • .indexof(x,from). Returns the first index of the string that contains the character equal to the value passed in, starting at the index passed to the function in the from variable.

  • .lastindexof. Same as .indexof but looks backward (toward the left of the string instead of the right).

  • .subtring(from,to). Returns a string that starts at the from index and ends at the to index.

  • .valueOf(var). Returns the string equivalent of the value contained in var regardless of data type.

Note 

The valueOf method is associated with most data types, so, for example, if you need to represent the value 5 as a float, you can simply say float.valueof(‘5').

Methods for the StringBuffer Class

Here are the most commonly used methods associated with the StringBuffer class:

  • .length(). Returns the length (in characters) of the string.

  • .charAt(x). Returns the character of the string at position x.

  • .capacity(). Returns the amount of space allocated to the array.

  • .append(x). Appends the character x onto the end of the StringBuffer variable; if the capacity is exceeded, more memory is allocated (although this method is not very efficient).

  • .insert(pos,x). Inserts the character specified in x at the position pos; again, if the capacity of the variable is exceeded, more memory is allocated.

  • .toString(). Converts the StringBuffer to a String. This method is useful when it is necessary to pass a String to a method (instead of a StringBuffer).

The following program uses many of the methods just described.

public class Jsap0701 {     public static void main (String[] args)     { /* Declare a Constant String and a StringBuffer */         String str1 = "c:\testdir\file01.exe";         StringBuffer str2 = new StringBuffer(60);         StringBuffer str3 = new StringBuffer(20);/* Get the length of the String    */         System.out.println(str1.length()); /* Get the length and Capacity of the Stringbuffer */         System.out.println(str2.length());         System.out.println(str2.capacity()); /* Get the Drive letter from the path in str */         str2.append(str1.substring(0,str1.indexOf(‘:')));         System.out.println(str2); /* Convert a floating point number to a string */         str3.append(String.valueOf(1.325));         System.out.println(str3);     } }

This program has the following output:

21 0 60 c 1.325


Team-Fly


Java & BAPI Technology for SAP
Java & BAPI Technology for SAP
ISBN: 761523057
EAN: N/A
Year: 1998
Pages: 199

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net