dude546546575 dude546546575 - 7 months ago 15
Java Question

Arrays: Location of certain Values Inputed

So I would like to output the location of a certain value in an array. how do I output the location of a value of an array? (example after the code):

public class Journal5b {

public int[] [] createArray (int rSize , int cSize)
{
int [] [] array = new int [rSize][cSize];
Random r = new Random();
for (int[] array1 : array) {
for (int column = 0; column < array[0].length; column++) {
array1[column] = r.nextInt(26);
}
}

return array;
}

public void print2DArray (int [] [] array)
{
for (int[] array1 : array) {
for (int column = 0; column < array[0].length; column++) {
System.out.print(array1[column] + "\t");
}
System.out.println("\n");
}
}

public int countInstance (int [] [] array, int searchForNum)
{
int count = 0;

for (int row = 0; row < array.length; row++)
{
for (int column = 0; column < array[0].length; column++) {
if (array[row][column] == searchForNum) {

count++;
}
}
}
return count;
}

public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
int [] [] myArray;
Journal5b j5b = new Journal5b();
myArray = j5b.createArray(10, 10);
j5b.print2DArray(myArray);

int val;
System.out.println("Enter the number to search for ");
val = in.nextInt();
System.out.println("Your Number popped up " + j5b.countInstance(myArray, val) + " times");
}
}


So the output I would look something like this:

22 20 12 25 10 8 3 4 12 25

11 24 0 7 3 21 23 9 23 11

22 18 10 24 11 13 0 25 8 18

7 14 0 5 11 12 9 22 0 16

14 11 12 4 16 3 20 22 18 25

8 24 12 6 25 4 3 16 10 23

23 11 8 12 19 15 3 25 12 6

10 3 5 22 11 7 0 7 4 4

18 1 14 23 7 13 9 9 12 9

20 10 6 14 13 1 9 15 0 3

Enter the number to search for
10
Your number popped up 5 times and is located on row 0,2,5,7, 9


How do I output what i want to output if that makes sense... I'm trying to add on to countInstance and find the location of that certain number thats being inputed by each row.

I have this code done for the most part however I've hit a wall with this part of the code. I can't find out my next step.I want to add
is located on row 0,2,5,7, 9

Answer

You can use an ArrayList to return the value from countInstance instead of using global variables.

public ArrayList<Integer> countInstance(int[][] array, int searchForNum) {
    //int count = 0;
    //Use an ArrayList to get the row index
    ArrayList<Integer> countList = new ArrayList<>();

    for (int row = 0; row < array.length; row++) {
        for (int column = 0; column < array[0].length; column++) {
            if (array[row][column] == searchForNum) {

                //count++;
                //add row index to the ArrayList count
                countList.add(row);
            }
        }
    }
    return countList;
}

and modifying your main() method a little bit to print the desired result

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int[][] myArray;
    Journal5b j5b = new Journal5b();
    myArray = j5b.createArray(10, 10);
    j5b.print2DArray(myArray);

    int val;
    System.out.println("Enter the number to search for ");
    val = in.nextInt();

    //get the ArrayList from countInstance
    ArrayList<Integer> countList = j5b.countInstance(myArray, val);
    //you can get the number of countInstances by getting the size of the ArrayList
    System.out.print("Your Number popped up " + countList.size() + " times");
    System.out.print(" and is located on row ");

    //Print the values returned by countInstance as desired
    for (int i = 0; i < countList.size(); i++) {
        System.out.print("" + countList.get(i));
        if (i < (countList.size() - 1)) System.out.print(",");
    }
}
Comments