二维数组的查找

package testcase;
/**
 * 
 * @decription 二维数组的查找 
 * @author bjliuzezhou
 * @date 2016年2月23日
 */
public class TypicalArithmetic_02 {
    
    
    public static void main(String[] args) {
        
        int array[][] = {{1,2,8,9,10},{2,4,9,12,13},{4,7,10,13,14},{6,8,11,15,16}};
    
        searchKeyOfArray(13, array);
        
    }
    
    public static void searchKeyOfArray(int key ,int array[][]){
        
        int rowLength = array.length;
        int columnLength = array[0].length;
        int i,j;
        for( i=0,j=0; i<rowLength && j<columnLength; i++,j++){
            
            if(key == array[i][j]){
                
                System.out.println("a[" + i + "][" + j + "]=" + array[i][j]); 
            }
                
            
            else if(key < array[i][j]){
                
                
                for(int k=j; k>=0; k--){
                    if(key == array[i][k]){
                        
                        System.out.println("a[" + i + "][" + k + "]=" + array[i][k]); 
                        
                    }
                }
                
                for(int k=i; k>=0; k--){
                    if(key == array[k][j]){
                        
                        System.out.println("a[" + k + "][" + j + "]=" + array[k][j]); 
                        
                    }
                }
            }
            
        }
            if(rowLength>columnLength){
                while(i<rowLength){
                    for(int k=columnLength-1;k>=0;k--){
                        if(key == array[i][k]){
                            System.out.println("a[" + i + "][" + k + "]=" + array[i][k]); 
                        }
                    }
                    
                        i++;
                }
            }else if(rowLength<columnLength){
                while(j<columnLength){
                    for(int k=rowLength-1;k>=0;k--){
                        if(key == array[k][j]){
                            System.out.println("a[" + k + "][" + j + "]=" + array[k][j]); 
                        }
                    }
                    
                        j++;
                }
            }else if(rowLength==columnLength){
                return;
            }
    }
}
原文地址:https://www.cnblogs.com/cangqiongbingchen/p/5234188.html