1

I have to compare elements in a multi dimensional array to check if a number is repeating either horizontally or vertically. I have written a program to compare horizontally. But how to update the current value once I have iterated over the array. Please let me know where I am going wrong.

  public class TwoDimensionArray {

    private static int rows = 4;
    private static int columns = 4;
    private static int array[][] = new int[][]{{4,3,9,1},{6,2,4,2},{5,4,5,2},{7,7,2,4}};


    public static void main(String args[]){
      printArray();

        System.out.println("--------------------");
        int counter=0;
        int i=0;
        int j = 0;
        int current=array[i][j];
        for ( i = 0; i<rows; i++){
            for(j=0;j<columns; j++){
                if(array[i][j]==current){
                    System.out.print("i > "+i+" j > "+j);
                }
        }
        System.out.println();
    }
}

private static void printArray(){
    for (int i = 0; i < rows; i++){
        for (int j = 0 ; j < columns; j++){
            System.out.print(array[i][j]+ " ");
        }
        System.out.println();
    }
  }
}
5
  • It is not at all clear what you want to do or what you are asking. If you can clarify your question, I'd be happy to dump your code into IntelliJ and have a closer look. Commented Sep 3, 2015 at 10:11
  • Could you please provide more clarity on this statement, But how to update the current value once I have iterated over the array. E.g 4 is repeated, then with what value you want to replace 4? Commented Sep 3, 2015 at 10:12
  • I want to scan the two dimension array and check if there are any numbers repeating consecutively for four times either horizontally, vertically or diagonally. I have just written for horizontal scan. Commented Sep 3, 2015 at 10:15
  • @Pradeep, so you are looking for code to scan vertically and diagonally? Commented Sep 3, 2015 at 10:21
  • @Sategroup yes. I have written the code to scan and compare horizontally. I have set the initial value to array[0][0], but how to update the value after each pass horizontally. Commented Sep 3, 2015 at 10:23

1 Answer 1

2

I have a solution for you. The result contains every number that meets your criterion:

import java.util.ArrayList;
import java.util.List;

public class TwoDimensionArray
{
static final int RIGHT = 0;
static final int DIAGONAL = 1;
static final int DOWN = 2;

private static int array[][] = new int[][] { { 3, 3, 9, 0 }, { 4, 3, 4, 0 }, { 4, 4, 3, 0 }, { 4, 7, 2, 3 } };
private static int array2[][] = new int[][] { { 0, 3, 9, 0, 5 }, { 0, 3, 4, 0, 5 }, { 0, 4, 3, 0, 5 },
        { 0, 7, 2, 3, 5 }, { 0, 1, 1, 1, 5 } };

public static void main(String args[])
{
    printArray(array);
    checkAndPrint(array);

    System.out.println();

    printArray(array2);
    checkAndPrint(array2);
}

private static void checkAndPrint(int[][] array)
{
    List<Integer> result = new ArrayList<Integer>();

    for (int i = 0; i < array.length; i++)
    {
        for (int j = 0; j < array[i].length; j++)
        {
            if (i == 0 || j == 0)
            {
                checkDirections(array, array[i][j], i, j, result);
            }
        }
    }

    System.out.println(result);
}

private static void checkDirections(int[][] array, int number, int row, int column, List<Integer> container)
{
    checkDirection(array, number, row, column, container, RIGHT, 0);
    checkDirection(array, number, row, column, container, DIAGONAL, 0);
    checkDirection(array, number, row, column, container, DOWN, 0);
}

private static void checkDirection(int[][] array, int number, int row, int column, List<Integer> container,
        int direction, int count)
{
    if (count == array.length - 1)
    {
        container.add(number);
    }

    int nextI = row;
    int nextJ = column;

    switch (direction)
    {
    case RIGHT:
        nextI++;
        break;
    case DIAGONAL:
        nextI++;
        nextJ++;
        break;
    case DOWN:
        nextJ++;
        break;
    default:
        break;
    }

    if (nextI < array.length && nextJ < array.length && array[nextI][nextJ] == number)
    {
        checkDirection(array, number, nextI, nextJ, container, direction, count + 1);
    }
}

private static void printArray(int[][] array)
{
    for (int i = 0; i < array.length; i++)
    {
        for (int j = 0; j < array[i].length; j++)
        {
            System.out.print(array[i][j] + " ");
        }
        System.out.println();
    }
}
}

Output:

3 3 9 0 
4 3 4 0 
4 4 3 0 
4 7 2 3 
[3]

0 3 9 0 5 
0 3 4 0 5 
0 4 3 0 5 
0 7 2 3 5 
0 1 1 1 5 
[0, 5]

It just checks the lines and columns and the diagonal from top left to buttom right, but I think you get the idea to use a recursive approach.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. There is one more scenario to check from left corner to right bottom corner. I should add another switch case right.
@Bambino, excellent work. Pradeep, you should study the code, understand it and then do modifications as per your need rather than asking

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.