获取同一Array中不同元素的索引

时间:2016-03-25 16:34:03

标签: java arrays string

我有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" ]开头的第一个数组(arr1)中元素的索引 在获得它们之后,我必须将其他两个数组的元素与相同的索引相乘:"1150" Starts with "11""3309" starts with "33"所以我必须乘以1*59*3 ..等等,并存储在一些变量中,我无法想象我应该使用哪种数据结构。

 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对于我的目标字符串的不同出现应该有很多值。所以这一行给我一个错误。 任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:1)

由于需要两次乘法运算,因此需要进行两次嵌套循环迭代同一个数组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")方法,即使对于空字符串也不会抛出。

答案 1 :(得分:1)

你的输出不清楚,仍然有些代码做了一些接近你问的事情:

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&gt; 5
    • 7&gt; 6
  • _33
    • 1&gt; 27
    • 6&gt; 615
  • _88
    • 5&gt; 800
    • 8&gt; 36

答案 2 :(得分:0)

您应该将每个x存储在列表结构中,例如LinkedListk的每个值只能匹配一个。

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.