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
}
引數
資料型別 - 給定陣列的原始資料型別。
變數名 - 將要重新分配陣列值的變數。
陣列名 - 給定陣列的名稱。
示例
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
結論
在本文中,我們學習了字串陣列,討論瞭如何建立它們,以及在字串陣列上執行了一些操作,例如搜尋、訪問和計算元素的數量。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP