Java 字符串数组
字符串和数组是两种不同的东西。数组是一种线性数据结构,用于存储具有相似数据类型的一组元素,而字符串是Java中的一个类,用于存储一系列字符。这些字符实际上是字符串类型的对象。由于字符串是对象,因此我们可以说字符串数组是一组字符串类型的对象。
在本文中,我们将了解Java中的字符串数组,并对它们进行一些操作。
字符串数组
字符串数组是数组和字符串的组合。因此,它具有数组和字符串的属性,例如:
- 它像数组一样按顺序存储数据。
-
创建数组后,无法更改其大小,即它具有固定的长度。
-
值用双引号括起来。
-
Java中的字符串是不可变的,所以字符串数组也是不可变的。这里的不可变意味着字符串数组的元素不能被修改。
语法
String[] nameOfarray;
// declaration
Or,
String nameOfarray[];
// declaration
Or,
// declaration with size
String nameOfarray[] = new String[sizeofarray];
// declaration and initialization
String nameOfarray[] = {values within double quotes separated with comma};
我们可以在程序中使用上述任何一种语法。 实例 –
在这个示例中,我们将声明三个数组’st1’,’st2’和’st3’。同时,我们将声明并初始化一个名为’st4’的数组。
String[] st1;
// declaration
String st2[];
// declaration
String st3[] = new String[10];
// declaration with specified size
// declaration and initialization
String st4[] = { “values”, “within”, “double”, “quotes” };
我们可以在声明的时候初始化,或者先声明然后在程序中需要的时候用值进行初始化。然而,在声明的时候,变量会被初始化为默认值,字符串数组的默认值是’null’。
示例1
以下示例说明了如果我们不对字符串数组进行初始化会发生什么情况。
public class Main {
public static void main(String[] args) {
String st1_arr[] = new String[3];
System.out.println("Elements of the given array: ");
System.out.print( st1_arr[0] + " " + st1_arr[1] + " " + st1_arr[2] );
}
}
输出
Elements of the given array:
null null null
如前所述,我们可以看到’st1_arr’被初始化为’null’值。此字符串数组的所有索引都只包含’null’。
让我们讨论一下我们可以对字符串数组执行的一些操作
访问字符串数组的元素
我们可以通过迭代遍历给定的数组来访问所有元素,使用迭代的方法如for、for each和while循环。
这里我们将使用for each循环来访问字符串数组的元素。
for each循环的语法
for(Data_Type nameOfvariable : nameOfarray) {
// your code will come here
}
参数
Data_Type - 给定数组的原始数据类型。
nameOfvariable - 用于重新分配数组值的变量名称。
nameOfarray - 给定数组的名称。
示例
public class Acces {
public static void main(String[] args) {
String st1_arr[] = {"Laptop", "Desktop", "Tablet", "Smartphone", "Smartwatch"};
System.out.println("Elements of the given array: ");
for(String val : st1_arr){
System.out.print( val + " ");
}
}
}
输出
Elements of the given array:
Laptop Desktop Tablet Smartphone Smartwatch
在上面的代码中,字符串数组“st1_arr”的元素依次存储在变量“val”中,然后打印它们。
计算字符串数组中元素的个数
我们将创建一个字符串数组和一个计数器变量来存储元素的数量。使用for循环遍历这些元素,每次迭代后将计数器变量增加1。
示例
public class Count {
public static void main(String[] args) {
String st_arr[] = {"Tutorials", "point", "and", "Tutorix"};
int counter = 0;
for(int i = 0; i < st_arr.length; i++) {
counter++;
}
System.out.print("Number of elements in the given string array: " + counter);
}
}
输出
Number of elements in the given string array: 4
搜索特定元素
我们将搜索存储在变量’key’中的值。在for循环中,我们遍历所有元素,并使用’equals()’方法检查给定的键是否在数组中可用。如果可用,则’isFound’变量递增1,并且if块将返回’key’的索引值。
示例
public class Srch {
public static void main(String[] args) {
String st_arr[] = {"Tutorials", "point", "and", "Tutorix"};
String key = "Tutorix";
int isFound = 0;
int position = 0;
for(int i = 0; i < st_arr.length; i++) {
if(key.equals(st_arr[i])) {
isFound = 1;
position = i;
}
}
if(isFound == 1) {
System.out.print("Element is available at index: " + position);
} else {
System.out.print("Element is not available");
}
}
}
输出
Element is available at index: 3
结论
在本文中,我们学习了关于字符串数组的知识,讨论了如何创建字符串数组,并且进行了一些操作,例如搜索、访问和计算元素的数量。