采用 int 数组和值以及 returns 该值在数组中的索引的方法
Method that takes an int array and a value and returns the index of that value in the array
我花了大约一个小时试图弄清楚这个问题,但我做不到。这是我正在上的 Java 选修课的练习,我需要一些帮助。
Write a method linearSearch() that takes an array of integers, and an
integer value. It should then return the index of the value inside the
array (by using a for loop to traverse the elements one by one in
order). If the value is not found, -1 should be returned. If more than
one value is found, the first occurrence should be returned. Write a
program to test your method.
这就是我尝试做的。
public class Exercise6 {
public static void main(String [] args) {
int[] a = {3, 6, 70, 3, 7, 9};
linearSearch(a, 3);
}
public static void linearSearch(int[] a, int n){
int index;
for (int i = 0; i < a.length; i++){
if (a[i] == n){
index = i;
break;
}
else {
index = -1;
}
}
System.out.print(index);
}
}
但这显然是错误的。你能给我指出正确的方向吗?我不一定要你给我答案,只是让我大概了解一下步骤。
这将通过编译错误,因为 int index;
未初始化。一旦初始化,else 部分将变得多余。休息会工作并为您提供预期的输出。
问题是你没有初始化变量(索引):
int index = -1;
但你应该换个角度思考:
1 - 读你想做的事
2- 您需要创建一个函数,该函数 return 数组特定值的索引
3-定义函数的return类型==>你需要return索引然后函数return类型是int
4- 定义参数 ==> 你有 2 个参数:数组和值
5- 你应该 return 值的索引或者 -1 如果它不存在那么你将初始化索引 = -1
6- 代码如下:
public class Exercise6
{
public static int linearSearch(int[] a, int n){
int index = -1;
for (int i = 0; i < a.length; i++){
if (a[i] == n){
index = i;
break;
}
}
return index;
}
public static void main(String[] args) {
int[] a = {3, 6, 70, 3, 7, 9};
System.out.println(linearSearch(a, 7));
}
}
编码前思考
祝一切顺利
有几个问题。
你想要 return 一个值,所以你需要在方法中通过 return 类型 int
而不是 void
.
你不需要索引变量,你的 for 循环中已经有一个 (i)
所以使用它。
当您遍历循环时,一旦找到值,只需对索引执行 return i;
到 return。您的方法中不需要任何 break
语句。
你不需要一直分配 -1
到索引所以去掉 else clause
(记住你甚至不需要 index
)。
如果您完成循环,则该值一定不存在,所以只有 return -1;
.
最后,由于您要 return 一个值,因此您需要在调用该方法时分配它。 int ret = linearSearch(a, 3);
也是如此。然后打印值。
我花了大约一个小时试图弄清楚这个问题,但我做不到。这是我正在上的 Java 选修课的练习,我需要一些帮助。
Write a method linearSearch() that takes an array of integers, and an integer value. It should then return the index of the value inside the array (by using a for loop to traverse the elements one by one in order). If the value is not found, -1 should be returned. If more than one value is found, the first occurrence should be returned. Write a program to test your method.
这就是我尝试做的。
public class Exercise6 {
public static void main(String [] args) {
int[] a = {3, 6, 70, 3, 7, 9};
linearSearch(a, 3);
}
public static void linearSearch(int[] a, int n){
int index;
for (int i = 0; i < a.length; i++){
if (a[i] == n){
index = i;
break;
}
else {
index = -1;
}
}
System.out.print(index);
}
}
但这显然是错误的。你能给我指出正确的方向吗?我不一定要你给我答案,只是让我大概了解一下步骤。
这将通过编译错误,因为 int index;
未初始化。一旦初始化,else 部分将变得多余。休息会工作并为您提供预期的输出。
问题是你没有初始化变量(索引):
int index = -1;
但你应该换个角度思考:
1 - 读你想做的事
2- 您需要创建一个函数,该函数 return 数组特定值的索引
3-定义函数的return类型==>你需要return索引然后函数return类型是int
4- 定义参数 ==> 你有 2 个参数:数组和值
5- 你应该 return 值的索引或者 -1 如果它不存在那么你将初始化索引 = -1
6- 代码如下:
public class Exercise6
{
public static int linearSearch(int[] a, int n){
int index = -1;
for (int i = 0; i < a.length; i++){
if (a[i] == n){
index = i;
break;
}
}
return index;
}
public static void main(String[] args) {
int[] a = {3, 6, 70, 3, 7, 9};
System.out.println(linearSearch(a, 7));
}
}
编码前思考
祝一切顺利
有几个问题。
你想要 return 一个值,所以你需要在方法中通过 return 类型
int
而不是void
.你不需要索引变量,你的 for 循环中已经有一个
(i)
所以使用它。当您遍历循环时,一旦找到值,只需对索引执行
return i;
到 return。您的方法中不需要任何break
语句。你不需要一直分配
-1
到索引所以去掉else clause
(记住你甚至不需要index
)。如果您完成循环,则该值一定不存在,所以只有
return -1;
.最后,由于您要 return 一个值,因此您需要在调用该方法时分配它。
int ret = linearSearch(a, 3);
也是如此。然后打印值。