1

EDIT: closed because it seems that the code below works

I am currently facing the following problem:
I have two two-dimensional arrays, of which the second dimension always has the size 7 (max. index is therefore 6).
Now I need to compare these arrays.
I want to print out if there has been a field added or removed, at any index.

For example:

Array 1:

 0|1|2|3
0 |1|2|5
1 |4|4|6
(...)
6 |6|2|8

Array 2

 0|1|2|3|4
0 |1|1|2|5
1 |4|4|4|6
(...)
6 |6|7|2|8

As you can see, I have added a column to the second array.
Now I need to print out this column or add it to an array list.

The same has to happen when a column gets removed or changed.

How can I achieve that?

My code so far:

static List<String[]> differences;
static List<String[]> checkForDifferences(String[][] tableOld,String[][] tableNew) {
    differences = new ArrayList<String[]>();
    if(!Arrays.deepEquals(tableNew,tableOld)) {
        for(int hour = 0; hour < tableOld.length;hour++) {
            try {
                boolean removed = true;
                for(int hour2 = 0;hour2 < tableNew.length;hour2++)
                    if(Arrays.equals(tableOld[hour],tableNew[hour2]))
                        removed = false;
                    if(removed)
                        differences.add(new String[]{"-",tableOld[hour][0], tableOld[hour][1], tableOld[hour][2], tableOld[hour][3], tableOld[hour][4], tableOld[hour][5], tableOld[hour][6]});
            } catch (ArrayIndexOutOfBoundsException e){
                e.printStackTrace();
                break;
            }
        }
        for (int hour = 0; hour < tableNew.length; hour++) {
            try {
                boolean added = true;
                for (int hour2 = 0; hour2 < tableOld.length; hour2++)
                    if (Arrays.equals(tableNew[hour], tableOld[hour2]))
                        added = false;
                if (added)
                    differences.add(new String[]{"+", tableNew[hour][0], tableNew[hour][1], tableNew[hour][2], tableNew[hour][3], tableNew[hour][4], tableNew[hour][5], tableNew[hour][6]});
            } catch (ArrayIndexOutOfBoundsException e) {
                e.printStackTrace();
                break;
            }
        }

        return differences;
    } else {
        return null;
    }
}
5
  • I tried to check if sub-array array1[i] is present in array2 somewhere, if not it has been removed; if sub-array array2[i] is not present in array1 it has been added Commented Oct 30, 2016 at 18:02
  • 1
    please edit your post, and add your code to it, so we can try it out/see what is going wrong.
    – n247s
    Commented Oct 30, 2016 at 18:05
  • Welcome to Stack Overflow! Please review our SO Question Checklist to help you to ask a good question, and thus get a good answer.
    – Joe C
    Commented Oct 30, 2016 at 18:07
  • Please re-format your arrays in a more readable (more Java-like?) way (sorry, I can't really figure out anything without guessing). Then add at least two examples of what output you expect from a certain input.
    – piegames
    Commented Oct 30, 2016 at 18:11
  • Ive added my code so far Commented Oct 30, 2016 at 18:16

1 Answer 1

0

Apparently, my code works. I was just using it wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.