Andrew .Del Real
Andrew .Del Real

Reputation: 39

Finding the location of a number in a 2d array

I am trying to create a program that lets you find how many times a curtain number occurs in a 2d array. One of the things that I am required to do is to create a method called countInstence which counts the amount of times a number occurs but also where they are located in the array. the problem i'm having is how do I output the location of the the number

public int[][] createArray(int rSize, int cSize) {

    Random rnd = new Random();
    int[][] array = new int[rSize][cSize];

    for (int row = 0; row < array.length; row++) {
        for (int col = 0; col < array[0].length; col++) {
            array[row][col] = rnd.nextInt(26);
        }
    }
    return array;
}

public void print2dArray(int[][] array) {

    for (int row = 0; row < array.length; row++) {
        for (int col = 0; col < array[0].length; col++) {
            System.out.print(array[row][col] + "\t");
        }
        System.out.println("\n");
    }
}

public int countInstence(int[][] array, int search) {

    int count = 0;

    for (int row = 0; row < array.length; row++) {
        for (int col = 0; col < array[0].length; col++) {
            if (array[row][col] == search)
                count++;

        }
    }
    return count;
}

public static void main(String[] args) {

    Journal5b call = new Journal5b();
    Scanner in = new Scanner(System.in);
    int[][] myArray;
    int value;
    myArray = call.createArray(10, 10);

    call.print2dArray(myArray);

    System.out.println("Enter a number to search for: ");
    value = in.nextInt();

    System.out.println("Your number occurred "
            + call.countInstence(myArray, value) + " Times");
}

Upvotes: 1

Views: 4887

Answers (4)

user3408531
user3408531

Reputation:

I suggest using Points:

 List<Point> locations=new ArrayList<Point>();
        ....
         if (array[row][col] == search)
         {
           count++;
           locations.add(new Point(row,col));
         }

Upvotes: 0

Atul
Atul

Reputation: 1566

If you want to also use location at other place then just declare a List and add location to that list while searching for element.

  public static List<String> location=new ArrayList<String>();
  public int countInstence(int[][] array, int search) {

    int count = 0;

    for (int row = 0; row < array.length; row++) {
        for (int col = 0; col < array[0].length; col++) {
            if (array[row][col] == search)
            {
                count++;
                String loc=row+","+col;
                location.add(loc);
            }
        }
    }
    return count;
}

public static void main(String[] args) {

    Test call = new Test();
    Scanner in = new Scanner(System.in);
    int[][] myArray;
    int value;
    myArray = call.createArray(10, 10);

    call.print2dArray(myArray);

    System.out.println("Enter a number to search for: ");
    value = in.nextInt();

    System.out.println("Your number occurred "
            + call.countInstence(myArray, value) + " Times");
    System.out.println("locations :");
    for(int i = 0; i < location.size(); i++) {
        System.out.println(location.get(i));
    }

}

Upvotes: 0

Adds
Adds

Reputation: 631

Just add this line to the for loop

  for (int row = 0; row < array.length; row++) {
    for (int col = 0; col < array[0].length; col++) {
        if (array[row][col] == search) {
            System.out.printf("Instance found at [%d, %d]\n", row, col);// add this line 
            count++;
        }
    }
}

Upvotes: 0

m_callens
m_callens

Reputation: 6360

Just insert a one line print statement inside of the if-statement and format to print the row and column of the current index, as below...

public int countInstance(int[][] array, int search) {

    int count = 0;

    for (int row = 0; row < array.length; row++) {
        for (int col = 0; col < array[0].length; col++) {
            if (array[row][col] == search) {
                System.out.printf("Instance found at [%d, %d]\n", row, col);
                count++;
            }
        }
    }

    return count;
}

If you aren't familiar with the System.out.printf call or the %d symbols, you can read more about it here.

Upvotes: 2

Related Questions