hello there.
i have a two dimensional vector.
int numbers[][]=new int[5][4];
i want to find if elements of row numbers[1] are equal to elements of row numbers[2];
i wrotted this code;
for(int i=0;i<5;i++){
for(int j=0;j<4;j++){
if(numbers[j]==numbers[(i+1)][j]{
System.out.print(numbers[j]);
}
}
}

Dani AI

Generated

A compact, reliable plan that matches what asked for (and what prompted you to design): for each row i, print the elements from row i that also appear anywhere in row i+1, preserving the order they appear in row i. This handles jagged rows (different lengths) and avoids the ArrayIndexOutOfBounds errors you saw when incrementing an index inside the expression.

// compare each row with the next and print common values in the earlier row's order
for (int i = 0; i < numbers.length - 1; i++) {
    int[] a = numbers[i];
    int[] b = numbers[i + 1];

    java.util.Set<Integer> nextSet = new java.util.HashSet<>();
    for (int v : b) nextSet.add(v);

    java.util.Set<Integer> printed = new java.util.HashSet<>(); // prevents duplicate prints
    StringBuilder out = new StringBuilder();
    for (int v : a) {
        if (nextSet.contains(v) && printed.add(v)) {
            if (out.length() > 0) out.append(' ');
            out.append(v);
        }
    }
    System.out.println(out.toString());
}

Notes and troubleshooting

  • Boundaries: loop to numbers.length - 1 so i+1 stays valid. Do not use i++ inside an index expression.
  • Jagged arrays: always use numbers[i].length when indexing columns; rows can differ in length.
  • Duplicates: the printed set ensures each matching value is printed once per row-pair. Remove printed if you want duplicate occurrences kept.
  • Complexity: O(m + n) per adjacent pair (build set of next row, then scan current row). For very large rows and strict memory limits, sort both rows and use a two-pointer intersection instead.

Recommended Answers

All 14 Replies

i want to find if elements of row numbers[1] are equal to elements of row numbers[2];

Are you to test if all are equal or only some? For example: 1,2,3 equals 1,2,3 but does not equal 1,4,3
what about: 1,2,3 and 3,1,2?

What will the results of the compare be?
1)true or false
2)a list of the numbers that are in both arrays
3) something else

i am going to test all elements of a row if are equal to elements of another row.
example:
1 2 3
4 3 6
5 7 6

it should print :
3
6

How would you do it if you were to do it by hand?
If you start at the first element in the first row, what would you compare it to first?
Then what would you compare it to next and so on and so on until you had compared it to all the rest of the elements.
When you had finished the first row, how would you go to the next row?
How many comparisons would you make with the 3x3 array you posted?
As an aid in seeing what needs to be compared, make a list of the comparisons to be done. For example:
1-4, 1-3, 1-6, 1-5, 1-7, 1-6
would be the comparisons for the first element. What are the rest?

the array i posted was just an example,
it should check if elements of first row first column is equal to element of second row first column.
simply
if nr[0][0]==nr[1][0], ...if nr[0][0]==nr[1][1], ... if nr[0][0]==nr[1][2]...etc. after we reach the end of columns, row ++,.........

they are equal, print nr[j]...

Do you have the pattern for how to compare an element from the array with the contents of the rest of the elements in the array?
Can you list the steps in pseudo code to describe how it is to be done.

A simple example:
loop through all elements in the array
get "next" element in the array
loop through remaining elements in the array
if a element == "next" print it
end inner loop
end outer loop

it doesnt work, when i type the code says: array out of index at: if(nr[j]==nr[i++][j])

You need to design the program and how it is to work BEFORE writing the code.
Do you have a design for how the program is supposed to work?
Please post it.
When the logic is worked out, then write the code.

array out of index at: if(nr[j]==nr[i++][j])

One of the index values is past the end of the array.
You need to make sure the indexes are between 0 and the length of the array - 1

this is the vector
1 3 5 7 9
2 4 6 9 7
10 11 2 13
15 16 13 18.

i want to get the result:
9 7
2
13

What are the steps your program must take to get those results?
Must the results be in the same order as you show?

yes they should keep the order.rows order is important

Can you explain why the 9 is before the 7?

sorry for that, 7 must be before 9.

Have you gotten a design for how the program is to work yet?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.