java字符串转字符串数组_Java字符串数组
java字符串转字符串数组
Java String array is used to hold fixed number of Strings. is very common in , specially among beginners to java and to test some specific scenarios. Even argument is string array – public static void main(String[] args). So today we will look into different aspects of java string array with example programs.
Java String数组⽤于保存固定数量的String。 在⾮常常见,特别是在Java初学者和测试某些特定情况的初学者中。 甚⾄参数也是字符串数组– public static void main(String[] args) 。 因此,今天我们将通过⽰例程序研究java字符串数组的不同⽅⾯。
Java String array is basically an array of objects.
Java String数组基本上是对象的数组。
There are two ways to declare string array – declaration without size and declare with size.
声明字符串数组的⽅法有两种:不声明⼤⼩和声明⼤⼩。
There are two ways to initialize string array – at the time of declaration, populating values after declaration.
有两种初始化字符串数组的⽅法-在声明时,在声明后填充值。
We can do different kind of processing on string array such as iteration, sorting, searching etc.
我们可以对字符串数组进⾏不同类型的处理,例如迭代,排序,搜索等。
Let’s go over java string array example programs now.
现在让我们来看⼀下Java字符串数组⽰例程序。
Java字符串数组声明 (Java String Array Declaration)
Below code snippet shows different ways for string array declaration in java.
下⾯的代码⽚段显⽰了Java中字符串数组声明的不同⽅法。
String[] strArray; //declare without size
String[] strArray1 = new String[3]; //declare with size
Note that we can also write string array as String strArray[] but above shows way is the standard and recommended way. Also in the above code, strArray is null whereas strArray1 value is [null, null, null].
请注意,我们也可以将字符串数组编写为String strArray[]但以上所⽰的⽅法是标准和推荐的⽅法。 同样在上⾯的代码中,
strArray为null⽽strArray1值为[null, null, null] 。
Java字符串数组初始化 (Java String Array Initialization)
Let’s look at different ways to initialize string array in java.
让我们看看在Java中初始化字符串数组的不同⽅法。
//inline initialization
String[] strArray1 = new String[] {"A","B","C"};
String[] strArray2 = {"A","B","C"};
/
/initialization after declaration
String[] strArray3 = new String[3];
strArray3[0] = "A";
strArray3[1] = "B";
strArray3[2] = "C";
All the three string arrays will have same values. However if you will call equals method on them, it will return false.
三个字符串数组都将具有相同的值。 但是,如果您将对它们调⽤equals⽅法,它将返回false。
System.out.println(strArray1.equals(strArray2)); // false
System.out.String(strArray1).String(strArray2)));// true
The reason is that array are Objects and Object class implements equals() method like below.
原因是数组是Objects,⽽Object类实现了equals()⽅法,如下所⽰。
public boolean equals(Object obj) {
return (this == obj);
}
Second statement is true because when converted to String, their values are same and String class equals() method implementation check for values. For more details, please check String class API documentation.
第⼆条语句是正确的,因为当转换为String时,它们的值是相同的,并且String类equals()⽅法实现会检查值。 有关更多详细信息,请查看String类API⽂档。
遍历Java字符串数组 (Iterating over java string array)
We can iterate over string array using or loop.
我们可以使⽤或循环遍历字符串数组。
String[] strArray2 = {"A","B","C"};
for (int i = 0; i < strArray2.length; i++) {
System.out.print(strArray2[i]);
}
for (String str : strArray2) {
System.out.print(str);
}
在字符串数组中搜索字符串 (Search for a String in the String array)
We can use for loop to search for an string in the array, below is a simple example for that.
我们可以使⽤for循环在数组中搜索⼀个字符串,下⾯是⼀个简单的例⼦。
package com.journaldev.stringarray;
public class JavaStringArrayExample {
public static void main(String[] args) {
String[] strArray = { "A", "B", "C" };
boolean found = false;
int index = 0;
String s = "B";
for (int i = 0; i < strArray.length; i++) {
if(s.equals(strArray[i])) {
index = i; found = true; break;
}
}
if(found)
System.out.println(s +" found at index "+index);
else
System.out.println(s +" not found in the array");
}
}
Notice the use of keyword to get out of the loop as soon as we found the string.
注意,⼀旦到字符串,就使⽤关键字退出循环。
Java字符串数组排序 (Java String Array Sorting)
We can implement our own sorting algorithm, or we can use class sorting method.
我们可以实现⾃⼰的排序算法,也可以使⽤类的排序⽅法。
String[] vowels = {"a","i","u","e","o"};
数组转换成字符串
System.out.println("Before sorting "+String(vowels));
Arrays.sort(vowels);
System.out.println("After sorting "+String(vowels));
Output of above code snippet will be:
上⾯的代码⽚段的输出将是:
Before sorting [a, i, u, e, o]
After sorting [a, e, i, o, u]
Note that String implements Comparable interface, so it works for natural sorting. Incase you want to sort by some other way, you can use Arrays.sort() overloaded method by passing a Comparator. Learn about these sorting techniques at .
请注意,String实现了Comparable接⼝,因此适⽤于⾃然排序。 如果要通过其他⽅式排序,则可以通过传递Comparator来使
⽤Arrays.sort()重载⽅法。 中的了解有关这些排序技术的信息。
将字符串转换为字符串数组 (Convert String to String Array)
We can convert String to string array using it’s split() method. It’s useful when you get a single string as input with values separated using delimiter character.
我们可以使⽤它的split()⽅法将String转换为字符串数组。 当您获得单个字符串作为输⼊,并且使⽤定界符将值分隔开时,这很有⽤。
String str = "a,e,i,o,u";
String[] vowels = str.split(",");
System.out.String(vowels)); //[a, e, i, o, u]
将字符串数组转换为字符串 (Convert String Array to String)
We can String() method to convert String array to String. Note that array doesn’t implement toString() method, so if you will try to get it’s string representation then you will have to rely on Arrays class, or else write your own custom code.
我们可以使⽤String()⽅法将String数组转换为String。 请注意,array不实现toString()⽅法,因此,如果您尝试获取它的字符串表⽰形式,则必须依赖Arrays类,或者编写⾃⼰的⾃定义代码。
String[] vowels = { "a", "e", "i", "o", "u" };
System.out.println(vowels);
System.out.String(vowels));
Output will be like below.
输出将如下所⽰。
[Ljava.lang.String;@3d04a311
[a, e, i, o, u]
The first output is because of Object class toString() implementation like below.
第⼀个输出是由于如下所⽰的Object类toString()实现。
public String toString() {
return getClass().getName() + "@" + HexString(hashCode());
}
要列出的Java字符串数组 (Java String Array to List)
We can get a list representation of string array List() method. Note that this list is backed by the array and any structural modification will result in java.lang.UnsupportedOperationException.
我们可以使⽤List()⽅法获得字符串数组的列表表⽰形式。 请注意,此列表由数组⽀持,任何结构修改都会导
致java.lang.UnsupportedOperationException 。
String[] vowels = { "a", "e", "i", "o", "u", "a", "o" };
List<String> vowelsList = Arrays.asList(vowels);
System.out.println("vowelsList = "+vowelsList);
/
/vowelsList.add("x"); //java.lang.UnsupportedOperationException
vowelsList.set(0, "x"); //allowed because no structural modification
System.out.println("vowelsList = "+vowelsList);
That’s all for java string array.
Java字符串数组就这些了。
Reference:
参考:
java字符串转字符串数组

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。