Search code examples
javaarraysdatabasesearchinverted-index

How do I get the row number of some text from input file?


I have some question below:

If there is a example database.txt as follow, how do I get the row number of certain number? For example, the number "1" appears in row 1, 3, 5, then i want to build an array which is [a]:[1 0 1 0 0 0 1], the 1 means item "1" appears in that row. Please Help!

Example Database.txt:

1 5
2 7
3 1
4 2
5 3
6 5
7 1

Solution

  • You would want to build a 2 dimensional array. the array would be for example array[7][2]. Then you would assign the numbers to the following array. After that you would make a method and have 2 for loops. 1 for loop will track the rows and the other will track columns.

    public int arrayMethod(array[][] a)
    for(int row=0; row <= a.length; row++){
        for(int col=0; col<=a.length-1; col ++){
     if(array[row][col]== 1)
        {
        return 1;
        }
     else return 0;
    }
       }
    

    It should be something along the lines of this.