0

I have a 2D arraylist, named as adjLists, which contains arraylists, containing values like these. Each two values are a "pair" and each third is a "flag" for the pair.

[278, 106, 0, 397, 36, 0, 59, 208, 0, 366, 221, 0]
[366, 221, 0, 397, 36, 0, 132, 390, 0, 278, 106, 0, 295, 0, 0]

I use code above to search for specified value pairs in these lists. vertexnum is the number of "sub arraylists" in adjLists.

for (int l = 0; l < vertexnum; l++) {
    if (adjLists.get(l).get(0) == p.x && adjLists.get(l).get(1) == p.y) {
        for (int h = 0; h < adjLists.get(l).size(); h += 3) {
            for (int g = 0; g < vertexnum; g++) {
                if ((vertexSprite[g].getX() + vertexSprite[g].getWidth() / 2) == adjLists.get(l).get(h)
                    && (vertexSprite[g].getY() + vertexSprite[g].getHeight() / 2) == adjLists.get(l).get(h + 1)) {
                    vertexSprite[g].setTexture(img_d);

                    ArrayList sublist = new ArrayList();
                    sublist.add(0, adjLists.get(l).get(h));
                    sublist.add(1, adjLists.get(l).get(h + 1));

                    for (int lj = 0; lj < vertexnum; lj++) {
                        if (Collections.indexOfSubList(adjLists.get(lj), sublist) != -1) {
                            int found = Collections.indexOfSubList(adjLists.get(lj), sublist);
                            adjLists.get(lj).set(found + 2, 1);
                            score++;
                        }
                    }
                    scoreupdate();
                }
            }
        }
        break;
    }
}

This code is to search exact values and replace their flag in every occurences. It can not find all the occurences of the values/pair in the lists, it replaces flag value only a few time. Value of score should be incremented with plus 1 after each found occurence, but this code increments it more than the number of matches. For example one array is like this [pair1, pair1, flag1, pair2, pair2, flag2] and want to change flag1 to 1 if pair1 is equal to the searched.

2
Contributors
2
Replies
16
Views
1 Year
Discussion Span
Last Post by kovacsakos
0

Why do things that complex?
Far simpler to define a class to contain the data pairs and create a List of that class.
Create a proper equals and hashCode method in that class and it's easy to compare with what you're searching for.
And you can if needed work with Comparable as well.

0

Yes, you are right, but I have worked in that solution, so starting a new method from zero has much more time consumption than fix an existing one-

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.