java - 如何阻止变量更新?
问题描述
的值count
随着每个循环(32-64-70 等)不断增加。但我需要在完成每个循环后将该值设为零。
公共类 BinarySearchLecture {
int count = 0;
int binarySearchLecture(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;
count++;
if (arr[mid] == x)
{
count++;
return mid;
}
if (arr[mid] > x)
{
count++;
return binarySearchLecture(arr, l, mid - 1, x);
}
else
{
count++;
return binarySearchLecture(arr, mid + 1, r, x);
}
} else {
return -1;
}
}
}
导入 java.util.Random;公共类 FirstAssignment {
// Driver method to test
public static void main(String args[])
{
BinarySearchLecture bs = new BinarySearchLecture();
int arr[] = new int[65536];
int n = arr.length;
for (int i = 0; i<n; i++)
{
arr[i] = new Random().nextInt(10000000);
}
for (int i = 1; i <=3; i++)
{
int x = new Random().nextInt(10000000);
System.out.println( i + "." + "Random search key: " + x );
int result = bs.binarySearchLecture(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result);
int count = bs.count; // count is an class variable
System.out.println("Number of comparisons are " + count);
// System.out.format("%32s%10d%16s", x , count , "presnt");
} }
解决方案
您的 binarySearchLecture 方法正在增加计数值。
您也可以使用 bs.resetCount()/bs.setCount(0) 之类的方法或类似的方法手动重置它。您可以将其添加到您定义的 bs 类中。
public class BinarySearchLecture {
int count = 0;
int binarySearchLecture(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;
count++;
if (arr[mid] == x)
{
count++;
return mid;
}
if (arr[mid] > x)
{
count++;
return binarySearchLecture(arr, l, mid - 1, x);
}
else
{
count++;
return binarySearchLecture(arr, mid + 1, r, x);
}
} else {
return -1;
}
}
void resetCount(){
count = 0;
}
}
然后你的代码看起来更像这样:
for (int i = 1; i <=3; i++)
{
int x = new Random().nextInt(10000000);
System.out.println( i + "." + "Random search key: " + x );
int result = bs.binarySearchLecture(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result);
int count = bs.count; // count is an class variable
System.out.println("Number of comparisons are " + count);
// System.out.format("%32s%10d%16s", x , count , "presnt");
bs.resetCount();
}
另一个不高效的解决方案 - 每个循环创建一个 bs 类的新实例。
for (int i = 1; i <= 3; i++) {
int x = new Random().nextInt(10000000);
System.out.println(i + "." + "Random search key: " + x);
bs = new BinarySearchLecture();
int result = bs.binarySearchLecture(arr, 0, n - 1, x);
if (result == -1){
System.out.println("Element not present");
}
else {
System.out.println("Element found at index " + result);
}
int count = bs.count; // count is an class variable
System.out.println("Number of comparisons are " + count);
}
推荐阅读
- android - 使用自定义 ScrollingViewBehavior 跳过 Viewpager 底部填充
- python - 如何提取 div 和类
- java - 程序类型已经存在 com.google.gson.FieldAttributes
- java - 在spring boot中添加属性值的正确方法是什么
- macos - 无法在 MacOS 上安装 TypeScript - 终端错误
- reactjs - 如何在 AWS Lambda 中执行 React 应用程序的服务器端渲染
- javascript - 在javascript中,以下代码如何工作
- react-native - react-native 中的 Metro bundler 是什么?
- delphi - Delphi TThread 句柄错误
- flutter - 全局减慢 Flutter 中的动画