2

After some research, I found out how to find the index of an item within a 2D Array. However, I'm after just one value, the row number and also what if the item you are looking for appeared more than once? How would you store the row number of all those times?

for(int j = 0; j < size; j++)
{
    if (arr[i][j] == 88)
    {
        return i; // The value i wanna store
        break;
    }
 }

If the number 88 appears more than once, how can I store all the different locations and later retrieve it?

4
  • 2
    use an arrayList to store the values found, as you don't know the length it using an array won't be the best case. Commented Apr 11, 2017 at 19:36
  • 1
    This depends if you really care about all the occurrences or just one. If you care about all occurrences, you may use an array list to store the indices. Commented Apr 11, 2017 at 19:37
  • Don't do a break statement. It will continue the loop Commented Apr 11, 2017 at 19:37
  • 2
    @Dakoda the break will never be reached considering the value is returned. Commented Apr 11, 2017 at 19:37

2 Answers 2

3

You could store the values you want in a List.

List<Integer> rows = new ArrayList<>();
for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) {
        if (arr[i][j] == 88) {
            rows.Add(i); // The value i wanna store
            break; // exit inner loop and continue with next row
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Better yet, like Ousmane shows, make it a strongly typed list of Integers. I'll update my answer with that.
2

i'm after just one value, the row number But if 88 appears more than once, how can I store all the different locations and later retrieve it?

Considering you don't know how many duplicated copies of the value you're looking for there could be, I'd suggest using an ArrayList to store the indexes.

create this before the loops:

List<Integer> indexList = new ArrayList<>();

then within the if block simply add the index value for the value you've found to the ArrayList:

if (arr[i][j] == 88){
    indexList.add(i);
    break;
}

you can then return the ArrayList if your method requires returning the data:

return indexList; // after the loops have finished processing

However, if the method return type is void then you can simply ignore the return indexList;

3 Comments

he/she doesn't want the value, they want the row index it appears in
valuesList.add(arr[i][j]); will only add 88 in the list, you should modify it to store the i or j or have two lists one for each or have single list of custom class type whose object can hold a pair of i and j
@nits.kk i misunderstood OP's requirement, it seems he/she wants just the row index, hence i have updated my answer to fit that.

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.