【问题标题】:Java: Find string in array listJava:在数组列表中查找字符串
【发布时间】:2015-08-31 04:55:49
【问题描述】:

我的arraylist 由一个带有字符串列表的数组组成,里面的每个字符串都用逗号分隔,我想将其中一个字符串拆分为用逗号分隔的子字符串,我知道如何使用拆分对数组进行操作方法,但我找不到类似于数组列表的东西,代码如下:

String[] widgets2 = 
        {

            "1,John,Smith,John1989@gmail.com,20,88,79,59",
            "2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85",
            "3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87",
            "4,Erin,Black,Erin.black@comcast.net,22,91,98,82",
            "5,Adan,Ramirez,networkturtle66@gmail.com,100,100,100"

        };

         ArrayList<String> jim = new ArrayList<String>(Arrays.asList(widgets2));

System.out.println(jim.split(0));

【问题讨论】:

  • Java 7:循环、循环和更多循环(好吧,只有一个显式循环:假装它是 C)。
  • jim.get(index).split() 可能吗?你是如何在数组中做到这一点的?
  • 请更改您的头像。
  • jim.get(0).split(",") 可能是?
  • jim.get(0).split(",") 产生:[Ljava.lang.String;@19e0bfd

标签: java arrays string


【解决方案1】:

这与您对arrays 所做的类似。只是实现方式不同。要获取值,只需遍历它们:

List<String> jim = new ArrayList<String>(Arrays.asList(widgets2));
for(String currentString : jim){//ArrayList looping
 String[] separatedStrings = currentString.split(",");
    for(String separatedString : separatedStrings){//Now its array looping
       //Do something now whatever you like to
    }
}

如果您想拥有索引并决定要获取哪个值,请使用普通的for循环使用索引并使用jim.get(index)并使用split()

话虽如此,是什么阻止您循环遍历 List ?这是数组列表的常见用法 - 循环遍历它:)

【讨论】:

    【解决方案2】:
        String jim_string = jim.toString().replace("[", "");
        jim_string.replace("]", "");
        String splitted[] = jim_string.split(",");
        System.out.println(splitted[0]);
    

    【讨论】:

      【解决方案3】:

      将以下代码行放入您的代码中,它会给出您想要的输出。

      int indx=0; //use the index you want to get & split
      String tmp = jim.get(indx); // get the string from the ArrayList
      if(tmp!=null && !tmp.equals("")){ // null check
      for (String retval: tmp.split(";")){ // split & print
           System.out.println(retval);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-26
        • 1970-01-01
        • 1970-01-01
        • 2020-11-26
        • 2020-06-19
        • 1970-01-01
        • 2019-04-14
        • 1970-01-01
        相关资源
        最近更新 更多