0

I am searching for matching elements in two arrays. If the matching element was found I set k and j greater than the for loop conditions to quit the cycle, but I get an exception at inner loop condition. Granted, I can fix it by using break outerloop;, but isn't setting index greater than the for loop condition supposed to exit the loop?

//outerloop:
for (int j = 0; j < listOfSubcategories.size(); j++) {
    if (listOfSubcategories.get(j).subcatName.equals(rawOptions.get(i).subcatName)) { //if the subcatName exists 

        for (int k = 0; k < listOfSubcategories.get(j).listOfID.size(); k++) {

            if (0 == rawOptions.get(i).codeID.compareTo(listOfSubcategories.get(j).listOfID.get(k).codeID)) {//if this id (as in 0090-01 exists
                    listOfSubcategories.get(j).listOfID.get(k).usageCount += rawOptions.get(i).usageCount;
                    isProcessed = true;
                    k=listOfSubcategories.get(j).listOfID.size();  //this fails
                    j = listOfSubcategories.size();
                    //break outerloop;                      //this works
                }

             } 
         }
    }
Lanhua
  • 13
  • 3
  • Why *wouldn't* it lead to an exception? You can't access something that's not there – Andrew Li Jun 22 '16 at 14:10
  • Andrew, your comment is not really helpful to someone learning Java. – sixtytrees Jun 22 '16 at 14:13
  • @sixtytrees why isn't it? I told him why it throws an exception – Andrew Li Jun 22 '16 at 14:14
  • 1
    @Andrew. You effectively said "sure it throws exception because it must throw it. I see the reason, but you, loser, don't. Here is a downvote for you". That is what you did. Very warm welcoming, good job. – sixtytrees Jun 22 '16 at 14:18
  • If you encounter an exception, look at the javadoc for that exception (e.g. [`IndexOutOfBoundsException`](https://docs.oracle.com/javase/7/docs/api/java/lang/IndexOutOfBoundsException.html)) and the method call on which the exception is thrown (e.g. [`List.get(int)`](https://docs.oracle.com/javase/8/docs/api/java/util/List.html#get-int-)) to find out why that exception might be thrown. – Andy Turner Jun 22 '16 at 14:25
  • @Andy Turner This is an RTFM. http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords-in-java 2K upvotes. Now SO answers RTFM to any question. What is the poit of such a community? – sixtytrees Jun 22 '16 at 14:28
  • @sixtytrees The question is something that is very easy to answer as Andy said. If they can't search up the meaning, I'm afraid their questions aren't quite worthwhile – Andrew Li Jun 22 '16 at 14:28
  • @Andy Turner Searchign why char[] is preferred over a String for java password is extremely easy to answer by reading relevant chapters of java docs. Lets delete that question. I afraid community became crabby over time. – sixtytrees Jun 22 '16 at 14:31
  • @sixtytrees it wasnt meant to be "crabby", it was meant to help OP to know how to find the info him/herself next time. "TFM" is pretty big, and can be hard for a beginner to know where to start "R"ing. – Andy Turner Jun 22 '16 at 14:48

1 Answers1

1

First of all, can you please post your stack trace?

Second, your exception is most likely caused by j = listOfSubcategories.size(); This makes comparison k < listOfSubcategories.get(j).listOfID.size() impossible.

The code below works.

public class ForArray {

    public static void main(String[] args) {


        int[] myIntArray = {1, 2, 3};  //This is my Array
        int searchingForThisNumberInArray = 2; //Search for this number in an array
        int index=-1; //Index of the first matching element. -1 if element not found.

        for (int i = 0; i < myIntArray.length; i++) { //Check each element
            if (myIntArray[i] == searchingForThisNumberInArray) { //if this element contains correct number
                index = i; //Save it in index
                i = myIntArray.length; //Quit for cycle by increasing i.
            }
        }
        System.out.println(index);
    }    
}
sixtytrees
  • 1,156
  • 1
  • 10
  • 25