当前位置:   article > 正文

Java中的字符串数组_java string 数组

java string 数组

Java中的字符串数组
 

在本指南中,您将了解java中的字符串数组、如何使用它们以及可以在java中对字符串数组执行的各种操作。

字符串数组是字符串的集合,存储在连续的内存位置。

例如:以下字符串数组包含四个元素。这些元素存储在连续的内存位置,可以使用数组索引进行访问,例如:names[0]表示第一个元素“Chaitanya”。类似地,名称[1]表示第二个元素“Ajeet”,名称[2]表示第三个元素“Hari”等等。

String[] names = new String[] {"Chaitanya", "Ajeet", "Hari", "Rahul"};

字符串数组声明
在Java中有两种方法可以声明String数组

1.在不指定数组大小的情况下:

String[] strArray;

2.指定了数组大小:以下数组最多可容纳5个字符串。

String[] strArray = new String[5];

字符串数组初始化

String[] names = new String[] {"Chaitanya", "Ajeet", "Hari", "Rahul"};

OR

String[] names = {"Chaitanya", "Ajeet", "Hari", "Rahul"};

String[] names= new String[4];  
names[0]= "Chaitanya"; //first element
names[1]= "Ajeet";  //second element
names[2]= "Hari";  //third element
names[3]= "Rahul"; //last element

Java中的简单字符串数组示例
在这个例子中,我们有一个字符串数组水果。此数组包含三个元素(字符串)。我们正在使用for循环显示字符串数组的元素。数组的length属性(fruits.length)返回数组中元素的数量,在本例中为3。

  1. public class JavaExample {
  2. public static void main(String a[]){
  3. //declared and initialized a string array
  4. String[] fruits = new String[]{"Apple", "Orange", "Banana"};
  5. for (int i=0; i<fruits.length; i++)
  6. {
  7. System.out.println("fruits["+i+"]: "+fruits[i]);
  8. }
  9. }
  10. }

迭代字符串数组
让我们看看如何迭代字符串数组。我们可以使用normal for循环或enhanced for循环(针对每个循环)进行迭代。

  1. public class JavaExample {
  2. public static void main(String a[]){
  3. //declared and initialized a string array
  4. String[] fruits = new String[]{"Apple", "Orange", "Banana"};
  5. //iterating using normal for loop
  6. System.out.println("Iterating using for loop:");
  7. for (int i=0; i<fruits.length; i++)
  8. {
  9. System.out.println("fruits["+i+"]: "+fruits[i]);
  10. }
  11. //iterating using for-each loop
  12. System.out.print("Iterating using foreach loop: ");
  13. for (String str: fruits)
  14. {
  15. System.out.print(str+ " ");
  16. }
  17. }
  18. }

向字符串数组添加元素
您已经了解到数组的大小是固定的,这意味着如果数组已满,则不能再向其中添加任何元素。但是,有两种方法可以向数组添加元素。从技术上讲,它并不是将元素添加到现有数组中,而是将以前数组的所有元素与新元素一起添加到一个新数组中。
1.创建新阵列
2.使用ArrayList

1.通过创建新数组向数组中添加元素
此程序中遵循的步骤是:
1.创建一个更大尺寸的新数组以容纳新元素。
2.将所有元素从旧数组复制到新数组。
3.向新数组中添加新元素。
4.打印新数组

  1. public class JavaExample {
  2. public static void main(String a[]){
  3. //declared and initialized a string array
  4. String[] fruits = new String[]{"Apple", "Orange", "Banana"};
  5. //we want to add two more elements to the fruits array so let's
  6. //create a new array with the size of 5
  7. String[] newFruits = new String[fruits.length+2];
  8. //copying elements from old array to new array
  9. for (int i=0; i<fruits.length; i++)
  10. {
  11. newFruits[i] = fruits[i];
  12. }
  13. //Adding new elements
  14. newFruits[newFruits.length-2]= "Mango"; //second last element
  15. newFruits[newFruits.length-1]= "Kiwi"; //last element
  16. //print new array
  17. for (String str: newFruits)
  18. {
  19. System.out.println(str);
  20. }
  21. }
  22. }

Output:

Apple
Orange
Banana
Mango
Kiwi
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/946966
推荐阅读
相关标签
  

闽ICP备14008679号