【问题标题】:How to connect the last index of array to first while iterating迭代时如何将数组的最后一个索引连接到第一个索引
【发布时间】:2019-10-24 10:58:17
【问题描述】:

我正在尝试迭代一组固定的字符串数组。在迭代期间,我有一个条件来匹配字符串并根据索引 [i+1] 获取数组中的下一项。现在一切正常,直到array.length-2。当迭代达到array.length-1(这是数组的大小)时,由于我的条件是在数组i+1中添加下一个元素,它会触发ArrayOutOfBoundException。我确实使用 try and catch 处理了它。当异常发生时,我已经重置了迭代 i-i,它比较了第一个索引。

好的,我会停下来直奔主题。我真正需要的是一旦迭代到达最后一个索引,代码应该将它与第一个索引进行比较。以下是我编写的代码,但有没有比您在下面看到的更快的方法。

结果是我想要的结果,但是有没有更快的方法可以做到这一点,或者这很好。

public boolean nameSelection(String name) {
isTrue = false;
String[] allNames = {"Remi","Peter","Jones","Spark"};

for(int i=0;i<allNames.length;i++) {   // iterating over array
     if(i<(allNames.length-1)) {      // last index is ignored
         if(allNames[i].equals(name)) {   // matching the name = Remi
           System.out.println(allNames[i+1]); // print the next Item = Peter
           isTrue = true;
           break;
           }
          }
         else if(i>=allNames.length-1) {  // last index execution
            if(allNames[i].equals(name)) { // matching the name
              System.out.println(allNames[i-i]); // matching to first element
              isTrue = true;
          }
       }
    }
        return isTrue;
}

【问题讨论】:

  • 你想比较最后一个字符串和第一个字符串吗?
  • @YogeshNikamPatil 是的,没错。一旦迭代到达最后一个索引,我需要它匹配第一个索引。基本上 array[lastindex] ==array[0]
  • 将这个 : else if(i>=allNames.length-1) 改为 : else if(i==allNames.length-1) 它可能会有所帮助。虽然我不完全明白你为什么要这样做

标签: java arrays


【解决方案1】:

使用% 计算“模数”运算,如下所示:

public static void main(String[] args) {
    String[] strArray = new String[]{
            "one",
            "two",
            "three"
    };
    for(int i = 0; i < strArray.length; i++){
        String firstItem = strArray[i];
        String secondItem = strArray[(i + 1) % strArray.length];
        System.out.println("Comparing [" + firstItem + "] and [" + secondItem + "]");
    }
}

输出:

Comparing [one] and [two]
Comparing [two] and [three]
Comparing [three] and [one]

【讨论】:

  • 非常有创意并且很好地使用了模数。
  • @Alexey,先生,您让我的生活更轻松。这确实是我一直在寻找的,简单而高效。谢谢。
【解决方案2】:

试试这个,

String[] allNames = {"Remi","Peter","Jones","Remi"};
int lastElement = allNames.length-1;

if (allNames[0].equals(allNames[lastElement])) {
        System.out.println("True");
} else {
    System.out.println("false");
}

【讨论】:

    【解决方案3】:

    你能试试这个吗?我认为这应该可行:

    public boolean nameSelection(String name) {
    isTrue = false;
    String[] allNames = {"Remi","Peter","Jones","Spark"};
    
    for(int i=0;i<allNames.length;i++) {   // iterating over array
         if(i<(allNames.length-1)&&allNames[i].equals(name)) {      // last index is ignored and  matching the name = Remi
    
               System.out.println(allNames[i+1]); // print the next Item = Peter
               isTrue = true;
               break;
    
              }
             else if(i==(allNames.length-1)&&allNames[i].equals(allNames[0])) {  // last index execution and matching last element to first element
                  System.out.println(allNames[i-i]); // matching to first element
                  isTrue = true;
           }
        }
            return isTrue;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多