Java loop control

151 views Asked by At

This algorithm is made to compare the route of the First ship to a plenty of other ships.I have to tell which ships do not cross the path of the First one.I imagined to solve this by telling which ships do not have common coordinates with the First ship's route in a coordinate system.

The first loop count the other ships which i compare to the first one.

The second loop counts each movements (from which the path of the ships built up) of the actual ship (which is compared to the First). Each movement has 2 coordinates.

The third loop should compare the coords of the other ships(given by the second loop) TO the First Ship.

In the third loop i gave a condition: if both coordinates equal the First ship's coords of its path, the whole damn thing should be started again from the first loop, which means the other ships count. (continue keyword)

If the second loop can ever end, i add the winner ship's index to my ArrayList 'goodships'.


The problem: No ships are added to the ArrayList, though my input coordinates definitely have different from the First ships's coords.


    ArrayList<Integer> goodships = new ArrayList<>();

    first: for (int i = 1; i < control.size(); i++) {

        char[] actdirections = control.get(i).toCharArray();
        int[] actualship = new int[2];

        second: for (int j = 0; j < actdirections.length; j++) {
            if (actdirections[j] == 'E') {
                actualship[0]--;
            } else if (actdirections[j] == 'D') {
                actualship[0]++;
            } else if (actdirections[j] == 'N') {
                actualship[1]--;
            } else if (actdirections[j] == 'K') {
                actualship[1]++;
            }
            third: for (int x = 0; x < firstship.length; x++) {
                if ((actualship[0] == firstship[x][0])
                        && (actualship[1] == firstship[x][1])) {
                    continue first;
                }
            }
        }
        goodships.add(i);
    }
1

There are 1 answers

0
Beny Bosko On

I wrote a new import file, tested the program with every scenario and it seems to be working. The previous import file contained 120+ different ships with different coordinates, it happaned to not have 2 ships with completely different route. Sorry for your time.

However, im curious if this solution is memory efficient. Or theres's a better way?