-1

Full code:

  public static String[] dirReduc(String[] arr) {


    LinkedList<String> list = new LinkedList<>(Arrays.asList(arr));


    for (int t = 0; t < list.size(); t++) {
        for (int q = t + 1; q < list.size() ; q++) {


        while (list.get(t).contains("NORTH") && list.get(q).contains("SOUTH") ||
                list.get(t).contains("SOUTH") && list.get(q).contains("NORTH") ||
                list.get(t).contains("WEST") && list.get(q).contains("EAST") ||
                list.get(t).contains("EAST") && list.get(q).contains("WEST"))



            for (int i = 0; i < list.size(); i++) {
                for (int k = i + 1; k < list.size(); k++) {

                    if (list.get(i).contains("NORTH") && list.get(k).contains("SOUTH") ||
                        list.get(i).contains("SOUTH") && list.get(k).contains("NORTH") ||
                        list.get(i).contains("WEST") && list.get(k).contains("EAST") ||
                        list.get(i).contains("EAST") && list.get(k).contains("WEST")) {
                        list.remove(i);
                        list.remove(k - 1);
                    }
                }
                }
            }
        System.out.println(list);
    }
    return null;
}

Loops to remove works fine.

My question about while loop:

in while loop in this field:

 list.get(t).contains("WEST") && list.get(q).contains("EAST") ||

i've got: IndexOutOfBoundsException: Index: 1, Size: 1

Only one element remains in List.

As far as I could get: index q check element after last element in list and get OutOfBound.

How can I fix this?

In similar topics, I couldn't find clear answer, for my question, please help.

IMBABOT
  • 121
  • 2
  • 12
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Vladimir Vagaytsev Aug 26 '18 at 16:30
  • Please provide the full code snippet to help. Thanks. – Nik Aug 26 '18 at 16:42

4 Answers4

0

why not check whether list contains one or not before entering while loop like this because it is this case that is making to have the error.

  if(list.size() == 1)
     q=t

in your code like this:

for (int t = 0; t < list.size(); t++) {
        for (int q = t + 1; q < list.size() ; q++) {
          if(list.size() == 1)
             q=t;
        while (list.get(t).contains("NORTH") && list.get(q).contains("SOUTH") ||
                list.get(t).contains("SOUTH") && list.get(q).contains("NORTH") ||
                list.get(t).contains("WEST") && list.get(q).contains("EAST") ||
                list.get(t).contains("EAST") && list.get(q).contains("WEST"))

or avoid the while loop if list contains exactly one element.

The Scientific Method
  • 2,374
  • 2
  • 14
  • 25
0

You have q equalling (t + 1) which would be out of bounds already so you must check if q is less than or equal to the list size to run the while loop to continue the program.

if (q <= list.size()) {
    // Your while loop
}
Mr Impact
  • 24
  • 3
0

This should fix the issue.

You need to check the index before using it on the list :

while (t < list.size() && q < list.size() &&  (list.get(t).contains("NORTH") && list.get(q).contains("SOUTH") ||
                list.get(t).contains("SOUTH") && list.get(q).contains("NORTH") ||
                list.get(t).contains("WEST") && list.get(q).contains("EAST") ||
                list.get(t).contains("EAST") && list.get(q).contains("WEST")))
Nik
  • 173
  • 2
  • 17
0

Should check not before loop, and loop itself.

Aftel while loop check conditions for break loop.

work like this:

    LinkedList<String> list = new LinkedList<>(Arrays.asList(arr));


    for (int t = 0; t < list.size(); t++) {
        for (int q = t + 1; q < list.size(); q++) {
                while (list.get(t).contains("NORTH") && list.get(q).contains("SOUTH")
                       || list.get(t).contains("SOUTH") && list.get(q).contains("NORTH")
                       || list.get(t).contains("WEST") && list.get(q).contains("EAST")
                       || list.get(t).contains("EAST") && list.get(q).contains("WEST"))

                    if (q < list.size()) q = t;


                for (int i = 0; i < list.size(); i++) {
                    for (int k = i + 1; k < list.size(); k++) {

                        if (list.get(i).contains("NORTH") && list.get(k).contains("SOUTH")
                                || list.get(i).contains("SOUTH") && list.get(k).contains("NORTH")
                                || list.get(i).contains("WEST") && list.get(k).contains("EAST")
                                || list.get(i).contains("EAST") && list.get(k).contains("WEST")) {
                            list.remove(i);
                            list.remove(k - 1);
                        }
                    }
                }
            }
        System.out.println(list);
        }
    return null;
}

Thanks to all who respond.

IMBABOT
  • 121
  • 2
  • 12