java 的2维数组的查找



/**
 * @author root 一个2维数组的查找
 */
public class Find {
/**
* 暴力查找 一个个比较

* @param arr
* @param num
* @return
*/
public static String findNum(int[][] arr, int num) {
if (arr == null)
return -1 + "";
int count = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
int[] js = arr[i];
for (int j = 0; j < js.length; j++) {
int element = js[j];
count++;
sb.append(element + " ");
if (element == num)
return "find count is " + add0(count, 2) + " , find sort is " + sb.toString();
}
}
return "find count is " + add0(count, 2) + " , find sort is " + sb.toString();
}


/**
* 优化查找 在数组中存在该元素的情况下 查找次数较为均衡

* @param arr
* @param num
* @return
*/
public static String find(int[][] arr, int num) {
if (arr == null) {
return -1 + "";
}
int count = 0;
int column = arr[0].length - 1;
int row = 0;
StringBuilder sb = new StringBuilder();
while (row < arr.length && column >= 0) {
int element = arr[row][column];
// System.out.println(element);
count++;
sb.append(element + " ");
if (element == num)
return "find count is " + add0(count, 2) + " , find sort is " + sb.toString();
if (element > num) {
column--;
} else {
row++;
}
}
return "find count is " + add0(count, 2) + " , find sort is " + sb.toString();
}


/**
* 遍历数组,将数组中元素挨个查找
* @param arr
* @return
*/
public static String foreach(int[][] arr) {
if (arr == null)
return -1 + "";
StringBuilder str = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
int[] js = arr[i];
for (int j = 0; j < js.length; j++) {
int element = js[j];
String str1 = findNum(arr, element);
String str2 = find(arr, element);
str.append("findNum method: num is " + add0(element, 2) + " , " + str1 + "\n");
str.append("find    method: num is " + add0(element, 2) + " , " + str2 + "\n");
}
}
return str.toString();
}


/**
* 填充0,使打印结果整齐
* @param num
* @param len
* @return
*/
public static String add0(int num, int len) {
String str = num + "";
while (str.length() < len)
str = "0" + str;
return str;
}


/**
* 获取一个随机分布的2维数组
* @param row
* @param column
* @return
*/
public static int[][] get2Arr(int row, int column) {
if (row < 0 || column < 0)
return null;
int[][] arr = new int[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
arr[i][j] = getRandom(0, 99);
}
}
return arr;
}


/**
* 展示数组内部数据
* @param arr
*/
public static void display(int[][] arr) {
if (arr == null)
return;
int row = arr.length;
int column = arr[0].length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < row; i++) {
sb.append("row is " + add0(i + 1, 2) + ": ");
for (int j = 0; j < column; j++) {
sb.append(add0(arr[i][j], 2) + " ");
}
sb.append("\n");
}
System.out.println(sb.toString());
}


public static int getRandom(int num1, int num2) {
int max = Math.max(num1, num2);
int min = Math.min(num1, num2);
return (int) (Math.random() * (max - min + 1) + min);
}


public static void main(String args[]) {
int[][] arr = get2Arr(3, 3);
display(arr);
System.out.println(foreach(arr));
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值