SHARE
TWEET

Untitled

a guest Oct 15th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Find a value and display its position
  2. public class SearchSample {
  3.     public static void main(String[] args) {
  4.         int[][] list = {{1,13,5},{1,2,5},{2,7,2}};
  5.         int searchValue = 2;
  6.         int positionX = -1;
  7.         int positionY = -1;
  8.  
  9.         PARENT_LOOP: for(int i=0; i<list.length; i++) {
  10.             for(int j=0; j<list[i].length; j++) {
  11.                 if(list[i][j]==searchValue) {
  12.                     positionX = i;
  13.                     positionY = j;
  14.                     break PARENT_LOOP;
  15.                 }
  16.             }
  17.         }
  18.  
  19.         if(positionX==-1 || positionY==-1) {
  20.             System.out.println("Value "+searchValue+" not found");
  21.         } else {
  22.             System.out.println("Value "+searchValue+" found at: " +
  23.             "("+positionX+","+positionY+")");
  24.         }
  25.     }
  26. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top