Hussain Sam
Hussain Sam

Reputation: 71

How to connect the last index of array to first while iterating

I am trying to iterate over a fixed set of array of string. During the iteration, I have a condition to, match the string and get the next item in the array based on the index [i+1]. Now it all works fine, until array.length-2. When the iteration reaches array.length-1 (which is the size of the array), and since my condition is adding next element in the array i+1, it triggers ArrayOutOfBoundException. I did handle it using try and catch. When the exception occurs, I have reset the iteration i-i, that compares the first index.

Ok, I'll stop and get to the point. What I really need is as soon as the iteration reaches the last index, the code should compare it to the first index. Below is what I have coded but is there any faster approach than what you see below.

The result is expected the way I want but is there a faster way to do this or this is fine.

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;
}

Upvotes: 0

Views: 933

Answers (3)

Pankaj Devrani
Pankaj Devrani

Reputation: 511

Can you try this I think this should work :

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;
}

Upvotes: 0

Yogesh Nikam Patil
Yogesh Nikam Patil

Reputation: 1280

Try this,

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");
}

Upvotes: 0

Alexey R.
Alexey R.

Reputation: 8676

Use % to evaluate "modulus" operation like this:

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 + "]");
    }
}

Output:

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

Upvotes: 2

Related Questions