我想创建一个在排序数组中搜索线性的程序,并可以输出找到搜索项的不同位置。目前我的程序只输出找到搜索项的第一个位置,所以这里是我的程序现在所做的一个例子:
Enter number of elements
5
Enter 5 integers
1
3
3
9
15
Enter value to find
3
3 is present at location 2.
现在问题是3位于第2和第3位,这就是我想在程序中编辑但我不知道该怎么做。
这是我的程序代码:
import java.util.Scanner;
class LinearSearchArray1 {
public static void main(String args[]){
int c, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
for (c = 0; c < n; c++)
{
if (array[c] == search) /* Searching element is present */
{
System.out.println(search + " is present at location " + (c + 1) + ".");
break;
}
}
if (c == n) /* Searching element is absent */
System.out.println(search + " is not present in array.");
}
}
答案 0 :(得分:1)
...
System.out.println("Enter value to find");
search = in.nextInt();
boolean exists = false;
for (c = 0; c < n; c++)
{
if (array[c] == search) /* Searching element is present */
{
System.out.println(search + " is present at location " + (c + 1) + ".");
exists = true;
}
}
if (!exists) /* Searching element is absent */
System.out.println(search + " is not present in array.");
您需要删除break;
语句。否则,一旦找到第一个值,循环就会被破坏,&amp;永远不会达到下一场比赛。
答案 1 :(得分:0)
此代码可能适合您。
for (int i = first; i <= last; i++)
{
System.out.println(search + " is present at location " + (i + 1) + ".");
}
现在索引“first”和“last”(包括它们)之间的元素包含搜索到的数字。因此,您基本上搜索与您要查找的第一个和最后一个元素 将其写入控制台的示例:
/app/
/controllers/
/views/
/lib/
/public/
.htaccess
答案 2 :(得分:0)
import java.util.Scanner;
public class BinarySearch {
/*
* A binary search or half-interval search algorithm finds the position of a
* specified value (the input "key") within a sorted array. Binary search
* requires a sorted collection. Also, binary searching can only be applied
* to a collection that allows random access (indexing).
*
* Worst case performance: O(log n)
*
* Best case performance: O(1)
*/
public static int binSearch(int a[], int key) {
int start = 0;
int end = a.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (key == a[mid]) {
return mid;
}
if (key < a[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}
public static void main(String arg[]) {
BinarySearch bi = new BinarySearch();
int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16 };
System.out.println("Please Key to be search");
Scanner sc = new Scanner(System.in);
int input = Integer.parseInt(sc.nextLine());
if (bi.binSearch(arr, input) != -1) {
System.out.println(input + ": " + " Search found at "
+ bi.binSearch(arr, input) + " " + "Position");
} else {
System.out.println(input + ": " + " Search Result not found ");
}
}
}
答案 3 :(得分:0)
使用 ArrayList 存储位置: -
import java.util.ArrayList; import java.util.Scanner;
类LinearSearchArray1 {
public static void main(String args[]){
int c, n, search, array[];
boolean searchStatus=false;
ArrayList<Integer> al=new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
for (c = 0; c < n; c++)
{
if (array[c] == search) /* Searching element is present */
{
al.add(c+1);
searchStatus=true;
}
}
if (searchStatus==false) /* Searching element is absent */
System.out.println(search + " is not present in array.");
else {
System.out.print(search+ " is present in array at location ");
for(Integer i:al)
System.out.print(i+",");
}
}
}