java字符串转字符串数组

Java String array is used to hold fixed number of Strings. String array is very common in simple java programs, specially among beginners to java and to test some specific scenarios. Even java main method 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程序中非常常见,特别是在Java初学者和测试某些特定情况的初学者中。 甚至java main方法的参数也是字符串数组– 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 sizeString[] 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[]但以上所示的方法是标准和推荐的方法。 同样在上面的代码中, strArraynullstrArray1值为[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.println(Arrays.toString(strArray1).equals(Arrays.toString(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 java for loop or java foreach loop.

我们可以使用java for循环或java foreach循环遍历字符串数组。

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);elseSystem.out.println(s +" not found in the array");}}

Notice the use of break keyword to get out of the loop as soon as we found the string.

注意,一旦找到字符串,就使用break关键字退出循环。

Java字符串数组排序 (Java String Array Sorting)

We can implement our own sorting algorithm, or we can use Arrays class sorting method.

我们可以实现自己的排序算法,也可以使用Arrays类的排序方法。

String[] vowels = {"a","i","u","e","o"};System.out.println("Before sorting "+Arrays.toString(vowels));Arrays.sort(vowels);System.out.println("After sorting "+Arrays.toString(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 Comparable and Comparator in java.

请注意,String实现了Comparable接口,因此适用于自然排序。 如果要通过其他方式排序,则可以通过传递Comparator来使用Arrays.sort()重载方法。 在Java中的Comparable和Comparator中了解有关这些排序技术的信息。

将字符串转换为字符串数组 (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.println(Arrays.toString(vowels)); //[a, e, i, o, u]

将字符串数组转换为字符串 (Convert String Array to String)

We can use Arrays.toString() 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.

我们可以使用Arrays.toString()方法将String数组转换为String。 请注意,array不实现toString()方法,因此,如果您尝试获取它的字符串表示形式,则必须依赖Arrays类,或者编写自己的自定义代码。

String[] vowels = { "a", "e", "i", "o", "u" };
System.out.println(vowels);
System.out.println(Arrays.toString(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() + "@" + Integer.toHexString(hashCode());}

要列出的Java字符串数组 (Java String Array to List)

We can get a list representation of string array using Arrays.toList() method. Note that this list is backed by the array and any structural modification will result in java.lang.UnsupportedOperationException.

我们可以使用Arrays.toList()方法获得字符串数组的列表表示形式。 请注意,此列表由数组支持,任何结构修改都会导致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.UnsupportedOperationExceptionvowelsList.set(0, "x"); //allowed because no structural modification
System.out.println("vowelsList = "+vowelsList);

That’s all for java string array.

Java字符串数组就这些了。

Reference: Arrays Oracle Documentation

参考: 阵列Oracle文档

翻译自: https://www.journaldev.com/17809/java-string-array

java字符串转字符串数组

java字符串转字符串数组_Java字符串数组相关推荐

  1. java字符串转字符串数组_Java字符串数组到字符串

    java字符串转字符串数组 Today we will look into how to convert Java String array to String. Sometimes we have ...

  2. java字符串的字节数组_Java字节数组到字符串到字节数组

    我正在尝试将byte []转换为字符串,将byte []的字符串表示形式转换为byte []的转换...我将byte []转换为要发送的字符串,然后我期望我的Web服务(用python编写)将数据直接 ...

  3. java字符串 输入到数组_Java字符串到数组的转换--最后放大招

    本文是关于如何在Java中以不同方式将String转换为String Array的几种方法,按照惯例,文末会分享Groovy语言中的实现. split()方法 字符串api是通过split()方法添加 ...

  4. java 字符字节数组_Java字符串与字符、字节数组知识点总结

    (1)用字符数组创建字符串对象,String类中有两个有字符数组创建字符串对象的构造的方法: String(char [ ])该构造方法用指定的字符数组创建构造一个字符串对象: String (cha ...

  5. java查找字符的方法_Java字符串查找(3种方法)

    在给定的字符串中查找字符或字符串是比较常见的操作.字符串查找分为两种形式:一种是在字符串中获取匹配字符(串)的索引值,另一种是在字符串中获取指定索引位置的字符. 根据字符查找 String 类的 in ...

  6. java string 转码问题_Java字符串转码

    Java 正确的做字符串编码转换 字符串的内部表示? 字符串在java中统一用unicode表示( 即utf-16 LE) , 对于 String s = "你好哦!"; 如果源码 ...

  7. java字符串与变量拼接_Java 字符串比较、拼接问题

    @字符串类型-----------------/ Java中用于处理字符串常用的有三个类: 1.java.lang.String 2.java.lang.StringBuffer 3.java.lan ...

  8. java 字符串首字符大写_Java字符串为大写

    java 字符串首字符大写 Java String to uppercase conversion can be done using toUpperCase() method. 可以使用toUppe ...

  9. java 数组对象属性数组_Java中数组的特性

    数组是基本上所有语言都会有的一种数据类型,它表示一组相同类型的数据的集合,具有固定的长度,并且在内存中占据连续的空间.在C,C++等语言中,数组的定义简洁清晰,而在java中确有一些会让人迷惑的特性. ...

最新文章

  1. java-第十一章-类的无参方法-计算器运算
  2. 2018年终总结之人工智能学习
  3. python程序控制结构_python程序控制结构
  4. 学习go语言国内最全资料链接
  5. 逆序对(洛谷P1908题题解,Java语言描述)
  6. Google推出了Python最牛逼的编辑器,不看后悔一辈子!
  7. Centos7.5常用firewall-cmd命令集
  8. elastic-job动态任务配置
  9. 360能卸载oracle,如何完全卸载Oracle
  10. [转]英特尔为什么能在CPU方面领跑?
  11. Hadoop序列化及案例
  12. python算法常用技巧与内置库
  13. 听说C站还有人缺图片素材?一篇跟UI设计师要来的——图片网址整理大全 让你从此远离素材荒!(强烈建议收藏)
  14. 用JS实现图片模糊到清晰预加载效果
  15. unity自带录屏UnityRecorder
  16. 谈MDM主数据管理系统、BI、大数据、SOA之间的关系
  17. Java编程练习:100以内的质数的输出
  18. OSChina 周五乱弹 —— 姑娘在这个节日里表白你接受么?
  19. 带你学会C++文字页面类项目——3.答题器制作
  20. python+opencv 批量修改视频分辨率

热门文章

  1. 【java基础】重载与重写
  2. U-Boot工作过程
  3. ASP.NET配置FCKeditor文本编辑器
  4. 批处理mysql命令
  5. 【SPS2010】现在的这个版本不值得测试。
  6. [转载] Python简介、linux上Python及其IDE的安装和详细配置
  7. [转载] 用pandas进行数据分析实战
  8. 电平转换与总线收发器详细介绍
  9. tornado框架基础05-模板继承、UImodul和UImethods
  10. bzoj千题计划269:bzoj2655: calc (拉格朗日插值)