public class exercise {
public static void main(String[] args) {
int key= 2;
int[][] arr = {{1,2},{1,2,3},{2,4,6},{2,2,3}};
Array(key,arr);//这里定义Array方法调用KEY值和数组
}
private static void Array(int key ,int a[][]){
int count=0;
for(int i=0;i
for(int j=0;j
if(a[i][j] ==key){
count++;//如有相同的数字则COUNT加一
}
}
}
System.out.print("共有"+count+"个值与KEY值相同");
}
}
public class TestArray {
public static void main (String[] args) {
int key = 2; //给key一个初始值;
int[][] arr = {{1,2},{1,2,3},{2,4,6},{2,2,3}}; //初始化二维数组;
int num = 0; //记录找到与key相等值的个数;
for(int i=0; i
num++;
}
}
}
System.out.println(num); //输出num值;
}
}
/*定义一个整形的数组,长度为10,任意赋值,用比较排序法将数组排序;
*/
public class E {
public static void main(String[] args) {
int [] a={1,9,6,4,3,8,2,7,5,10};
for(int i=0;i
int zhang=a[i];
a[i]=a[j];
a[j]=zhang;
}
}
}
for(int i=0;i
}
}
}
1楼已经写得差不多了,只是没有单独写一个方法。呃,我也写些吧
public static int[][] test = new int[][]{{1,2,3},{4,5,6},{3,5,9},{1,3,7},{10,3,8}};
public static void main(String[] args){
System.out.println(findKey(3));
}
public static int findKey(int key){
int count = 0;
for(int[] a : test){
for(int b : a){
count += (key == b) ? 1 : 0;
}
}
return count;
}
public static int countNum(int key,int[][] srcDataArr){
int count = 0;
int[][] ab = srcDataArr;
for(int i=0;i
if( ab[i][j]==key ){
count++;
}
}
}
System.out.println( count );
return count;
}
public static int[][] constructTwoDimArray(int firstDimLen,int secondDimLen){
Random random = new Random();
int[][] ab = new int[firstDimLen][secondDimLen];
for(int i=0;i
}
}
return ab;
}
public static void main(String[] args)throws Exception{
int[][] ab = constructTwoDimArray(5,5);
countNum( 4,ab );
}