【问题标题】:Is there a way to check for a char in all elements of an array?有没有办法检查数组的所有元素中的字符?
【发布时间】:2016-12-17 03:46:58
【问题描述】:

我有一个包含几百个元素的数组,需要找到在元素的第二个字符中有特定字母的数组。有没有比嵌套 if 的怪物更简单的方法?

【问题讨论】:

  • 假设元素是String,在检查element.charAt(1) 时循环遍历元素。如果这没有帮助,请通过示例在问题中提供更多信息。
  • 在发布之前你有没有尝试过?
  • 我正在尝试查找每个数组中打印的第二个是否为“A”。这是我到目前为止所拥有的:
  • char aChar = 对手卡.charAt(1); System.out.println(aChar); if (aChar=='A'){ element.charAt(1); }
  • 您听说过循环吗?如果没有,请返回并阅读您的 Java 材料,例如The Java™ Tutorials - The for Statement

标签: java arrays


【解决方案1】:

也许这对你的字符串数组有帮助

String[] s = {"mnmmm","sdcsdc"};
       for(String h:s){
           if(h.charAt(1)=='n')
               System.out.println(h); // prints the first mnmmm
       }

【讨论】:

    【解决方案2】:

    首先确保你在发布自己之前已经尝试过自己......即使那样你也做不到,然后随时在堆栈溢出中询问......

     String[] string={"string1","string2","string3","etc..."} //array that has a couple hundred elements
    char thatChar='A' //specific letter in the 2nd character in the element
    for(String var:string)
       if(var.charAt(1)==thatChar)
         //work here...
    

    你可以学习[foreach loop]1 和字符串操作:[charAt(index)][2]

    如果有帮助,请确保接受此答案,如果没有帮助,请在降级之前发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2020-02-04
      • 2010-09-22
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多