簡體   English   中英

在多個索引處找到相同的值

[英]Finding the same value at multiple indexes

我創建了一個Java程序,該程序將在數組中搜索值,但是我的問題是,當我在不同的索引中輸入相同的值時,第一個索引是將在輸出中唯一的一個索引。

示例index 0 = 2, index 1 = 3, index 2 = 2

輸出: array 2 is found at index 0 only

我在循環上將其中斷以停止,但如果不這樣做,它將循環輸出

這是我想要的輸出: array 2 is found at index 0,2

碼:

import java.awt.*;
import javax.swing.*;
import java.io.*;
public class Buff {
    public static void main(String args[]) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter how many index :");
        int v = Integer.parseInt( in .readLine());
        int x;
        int[] c = new int[v];
        int vv;
        for (x = 0; x < v; x++) {
            System.out.print("Enter your value :");
            c[x] = Integer.parseInt( in .readLine());
        }
        System.out.print("Enter your search number :");
        int xx = Integer.parseInt( in .readLine());
        for (x = 0; x < v; x++) {
            if (c[x] == xx) {
                System.out.print("array " + xx + " found at index :" + x);
                break;
            } else {
                System.out.print("array not found");
            }
        }
    }
}

生成匹配列表並在for循環中填充索引的解決方案。

然后在for循環完成后,打印出結果

List<Integer> foundIndexes = new ArrayList<>();
for (x = 0; x < v; x++) {
     if (c[x] == xx) {
        foundIndexes.add(x);
     } 
}

//now we looped through whole array
if(foundIndexes.isEmpty()){
    System.out.print("array not found");
}else{
     System.out.print("array " + xx + " found at index : ");

    for(Integer i : foundIndex){
      System.out.print(i + ",");
    }
}

這將打印出array 2 is found at index 0,2,array 2 is found at index 0,2,並帶有逗號。 在最后一個索引中沒有逗號結尾會稍微復雜一些,但是我將由您自己決定。

如果您只關心輸出索引,則也可以使用StringBuilder。

StringBuilder sb = new StringBuilder("array" + xx +" is found at index: ");
 for (x = 0; x < v; x++) {
     if (c[x] == xx) {
         sb.append(x).append(",");
     }
 }


if (sb.charAt(sb.length() - 1) == ',') {
    sb.deleteCharAt(sb.length() - 1);
    System.out.println(sb);
} else {
    System.out.println("array not found");
}

如果我正確理解問題,則可能是以下原因:

您在數組中有元素,要檢查某個特定值是否在數組的多個位置中,您的問題是,如果僅刪除break語句,則每次找不到所需數字時都會顯示一條消息,坦白地說,這是我看到的刪除break語句的唯一問題。

就我個人而言,我會做以下兩件事之一:

選項A:您可以創建一個布爾變量,如果找到一個數字,該變量就會更改,然后等待傳遞“找不到數組”消息,直到您停止搜索為止,如下所示:

         boolean found = false;
         for( x=0; x<v; x++)
         {
            if(c[x] == xx)
            {
                System.out.println("array " + xx + " found at index :"+x);
                found = true;
            }
         }
            if (found = false)
            {
                System.out.println("array not found");
            }

println與print的作用相同,只是它在末尾引入了\\ n,因此響應如下所示:

數組2位於索引:0

數組2位於索引:2

代替:

在索引:0處找到的數組2在索引:2處找到的數組2

選項B:可能更優雅的解決方案是創建另一個數組,存儲要在其中找到所需元素的位置,然后一次打印所有這些元素,可以對數組進行兩次操作(一個可以計算出數組必須具有許多位置,另一個用於檢查元素的位置),或者只是使用ArrayList,但是由於這看起來像是學習材料,所以我想這是不可能的。

另外,如果可能的話,請嘗試更好地表達您的問題,因為我仍然不確定這是否是您要的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM