簡體   English   中英

獲取同一數組中不同元素的索引

[英]getting indexes of different elements in the same Array

我有3個數組的例子:

String[]arr1={"1150","3309","44","22","98","88","33","11","880"}
String[]arr2={"5","9","44","22","65","20","41","3","9","5"}
String[]arr3={"1","3","20","22","30","40","15","2","4","0"}

我想獲取以["11" , "88" , "33" ]開頭的第一個array(arr1)中的元素的索引,獲取它們后,我必須將其他兩個數組的元素與相同的索引示例相乘: "1150" Starts with "11" "3309" starts with "33""3309" starts with "33"因此我必須將1*5 5,9 9*3 ..etc乘以並將其存儲在某個變量中,我無法想象應該使用哪種數據結構使用它。

 for (int k = 0; k < index - 1; k++) {
                if (arr1[k].substring(0, 2).equals("11")
                        || arr1[k].substring(0, 2).equals("88")
                        || arr1[k].substring(0, 2).equals("33"))
    {
    //don't know what should i do then
    }

    }

我嘗試了這個:

Integer x=Integer.valueOf(arr2[k])* Integer.valueOf(arr3[k]);

但是后來我發現,對於目標字符串的不同出現,k應該具有許多值。 所以這條線給我一個錯誤。 任何幫助,將不勝感激謝謝!

由於需要兩個乘積,因此需要制作兩個嵌套循環,以迭代同一數組arr1 由於您不希望重復,因此從索引開始迭代嵌套循環,使其比外部循環的索引循環一圈。 該模式如下所示:

for (int k = 0 ; k < arr.Length ; k++) {
    if ( /* check if k is not a good index */) {
        // If k is not an index of something we want, move on with the loop
        continue;
    }
    // Start iterating at the next position after k
    for (int m = k+1 ; m < arr.Length ; m++) {
        if (/* check if m is a good index */) {
            ... // Do something with indexes k and m
        }
    }
}

您唯一要做的另一件事是確保當arr1[i]少於兩個字符時,代碼不會中斷。 當前,您的代碼將引發異常。 更好的方法是使用StartsWith("11")方法,即使是空字符串也不會拋出該方法。

您的輸出不清楚,仍然有些代碼執行的操作接近您的要求:

Match.java

public enum Match {
    // Using an enum to get flexbility
    _11("11"),
    _33("33"),
    _88("88");
    public String value;
    private Match(String value) { this.value = value; }
    // This function checks if the given string starts with one of the matches
    public static Match fromString(String s) {
        for (Match m : Match.values()) { if (s.startsWith(m.value)) return m; }
        return null;
    }
}

Test.java

public class Test {

    public static void main(String[] args) {

        String[]arr1={"1150","3309","44","22","98","88","33","11","880"};
        String[]arr2={"5","9","44","22","65","20","41","3","9","5"};
        String[]arr3={"1","3","20","22","30","40","15","2","4","0"};

        Map<Match, List<Integer>> indexes = new HashMap<Match, List<Integer>>();
        // First initialize the map with empty list
        for (Match m : Match.values()) {
            indexes.put(m, new ArrayList<Integer>());
        }

        // Then a loop to find the indexes in the first array of the elements starting with one of the matches.
        for (int k = 0; k < arr1.length ; k++) {
            String cur = arr1[k];
            Match m = Match.fromString(cur);
            if (m != null) {
                indexes.get(m).add(k);
            }
        }
        // Finally loop on all patterns to get the computed result (based on 2nd and 3rd array content)
        for (Match m : Match.values()) {
            System.out.println("- " + m.name());
            for (Integer i : indexes.get(m)) {
                System.out.println("  - " + i + " > " + (Integer.valueOf(arr2[i]) * Integer.valueOf(arr3[i])));
            }
        }
    }
}

結果:

  • _11
    • 0> 5
    • 7> 6
  • _33
    • 1> 27
    • 6> 615
  • _88
    • 5> 800
    • 8> 36

您應該將每個x存儲在列表結構中,例如LinkedList 每個k值只能有一個匹配項。

List<Integer> results = new LinkedList<>();
for(int k = 0; k < arr1.length; k++) {
    // Check if the first character is "1", "3", or "8", and that the
    // second character is the same as the first. This assumes that
    // there are no null elements and no numbers less than 10.
    if ("183".indexOf(arr1[k].charAt(0)) > 0 && arr1[k].charAt(0) == arr1[k].charAt(1)) {
        results.add(Integer.valueOf(arr2[k]) * Integer.valueOf(arr3[k]));
    }
}
// results now contains all the multiplied values.

暫無
暫無

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

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