数组
在 Java 语言中,数组是具有属性的对象,其中最重要的是 长度 属性,您可以使用该属性确定数组的大小。Java 数组的索引值始终从 0 开始,数组的声明大小包括第 0 个元素。因此,大小为 100 的数组意味着有效索引是从 0 到 99。另外,您还可以将用于表示数组的方括号([ ])与数组类型而非数组名绑定起来。Java 语言允许采用数组字面值,这样可以将数组初始化为一组预定义的值。清单 4 展示了一些例子。
清单 4. 数组
1 Visual Basic Java
2 'An array with 100 integers // An array of 100 integers
3 Dim a(99) As Integer int[] a = new int[100];
4 'An array of Strings initialized // An array of Strings initialized
5 b = Array("Tom","Dick", "Harry") String[] b = {"Tom","Dick", "Harry"};
6
7 'Iterating through an array // Iterating through an array of length 100
8 ' of length 100 int [] c = new int [100];
9 Dim c(99) As Integer for (int i = 0; i <.length; i++) {
10 For i=0 To UBound(c) c[i] = i;
11 c(i) = i }
12 Next
13
2 'An array with 100 integers // An array of 100 integers
3 Dim a(99) As Integer int[] a = new int[100];
4 'An array of Strings initialized // An array of Strings initialized
5 b = Array("Tom","Dick", "Harry") String[] b = {"Tom","Dick", "Harry"};
6
7 'Iterating through an array // Iterating through an array of length 100
8 ' of length 100 int [] c = new int [100];
9 Dim c(99) As Integer for (int i = 0; i <.length; i++) {
10 For i=0 To UBound(c) c[i] = i;
11 c(i) = i }
12 Next
13
字符串
Visual Basic 使用 String 数据类型来表示字符串。您可通过 String 类的对象来表示 Java 字符串。Visual Basic 和 Java 字符串字面值由一系列加引号的字符表示。在 Java 语言中,可以采用两种方法创建 String 对象:使用字符串字面量,或使用 构造函数。 String 对象是固定的,这意味着在赋予某个 String 一个初始值后,就不能改变它。换句话说,如果您要更改 String 引用的值,则需要将一个新 String 对象赋值给该引用。由于 Java 字符串是对象,所以可以通过 String 类定义的 接口与这些字符串进行交互。String 类型包含很多接口以及不少有用的方法。
清单 5 演示了一些最常用的方法。请尝试编译并运行该例子。记住将源文件命名为 StringTest.java,并且不要忘记文件名的大小写很重要。
清单 5. Java 语言中的字符串
1 /*
2 * The StringTest class simply demonstrates
3 * how Java Strings are created and how
4 * String methods can be used to create
5 * new String objects. Notice that when you
6 * call a String method like toUpperCase()
7 * the original String is not modified. To
8 * actually change the value of the original
9 * String, you have to assign the new
10 * String back to the original reference.
11 */
12 public class StringTest {
13 public static void main(String[] args) {
14 String str1 = "Hi there";
15 String str2 = new String("Hi there");
16 // Display true if str1 and str2 have the value
17 System.out.println(str1.equals(str2));
18 // A new uppercase version of str1
19 System.out.println(str1.toUpperCase());
20 // A new lowercase version of str2
21 System.out.println(str1.toLowerCase());
22 System.out.println(str1.substring(1,4));
23 // A new version of str1 w/o any trailing whitespace chars
24 System.out.println(str1.trim());
25 // Display true if str1 start with "Hi"
26 System.out.println(str1.startsWith("Hi"));
27 // Display true if str1 ends with "there"
28 System.out.println(str1.endsWith("there"));
29 // Replace all i's with o's
30 System.out.println(str1.replace('i', 'o'));
31 }
32 }
33
2 * The StringTest class simply demonstrates
3 * how Java Strings are created and how
4 * String methods can be used to create
5 * new String objects. Notice that when you
6 * call a String method like toUpperCase()
7 * the original String is not modified. To
8 * actually change the value of the original
9 * String, you have to assign the new
10 * String back to the original reference.
11 */
12 public class StringTest {
13 public static void main(String[] args) {
14 String str1 = "Hi there";
15 String str2 = new String("Hi there");
16 // Display true if str1 and str2 have the value
17 System.out.println(str1.equals(str2));
18 // A new uppercase version of str1
19 System.out.println(str1.toUpperCase());
20 // A new lowercase version of str2
21 System.out.println(str1.toLowerCase());
22 System.out.println(str1.substring(1,4));
23 // A new version of str1 w/o any trailing whitespace chars
24 System.out.println(str1.trim());
25 // Display true if str1 start with "Hi"
26 System.out.println(str1.startsWith("Hi"));
27 // Display true if str1 ends with "there"
28 System.out.println(str1.endsWith("there"));
29 // Replace all i's with o's
30 System.out.println(str1.replace('i', 'o'));
31 }
32 }
33