0

I have 3 arrays example:

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"}

i want to get indexes for elements in the first array(arr1) that starts with ["11" , "88" , "33" ] and after getting them I have to multiply the elements of the other two arrays with the same indexes example:"1150" Starts with "11","3309" starts with "33" so i have to multiply 1*5 , 9*3 ..etc and store the in some variable which I can't imagine what data structure should I use for it.

 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
    }

    }

I tried this:

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

but then I figured out that k should have many values for the different occurrences of my targeted strings. so this line caused me an error. any help would be appreciated thanks !

3
  • 1
    "11" also starts with "11" - Why is it 5*1 and not 9*4 ? Or are you supposed to do both but only provided an example for the first one? Commented Mar 25, 2016 at 16:36
  • 1
    @DarthAndroid I wrote ..etc btw which means I'm supposed to do both and I just wrote an example. Commented Mar 25, 2016 at 16:39
  • Ah, I thought the "etc" was for the example for 88 that wasn't provided; My bad. Commented Mar 25, 2016 at 16:44

3 Answers 3

1

Since it takes two to multiply, you need to make two nested loops iterating the same array arr1. Since you do not want duplicates, start iterating the nested loop from the index one past that of the outer loop. The pattern looks like this:

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
        }
    }
}

The only other thing you should do is making sure that your code does not break when arr1[i] has fewer than two characters. Currently, your code would throw an exception. A better approach is to use StartsWith("11") method, which would not throw even for an empty string.

Sign up to request clarification or add additional context in comments.

Comments

1

Your output is unclear, still some code doing something close to what you asked :

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])));
            }
        }
    }
}

outcome:

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

7 Comments

That worked for me thank you man but I need some help if you don't mind cause I never used Maps before in java. I am stuck with a little problem If I need to check on values of 11 , 33 or 88 which are in the indexes map. How could I control them ? I need to say for example if "11" contains any value > 10 which in that case (5,6) System.out.println("good") btw I printed indexes and i got something like this {_88=[5, 8], _11=[0, 7], _33=[1, 6]} but i cant loop on it or check its values
and when i tried this: ` for (int k = 0; k < indexes.size(); k++) { System.out.println(indexes.get(k)); } ` and the output was 3 nulls
Hm you should read the Map javadoc ;) in my sample you have access to the map indexes with : map.get(index). Ill update my post with some comments explanation !
i think when i use map.get(index) I should get the elements in the map so why does it print 3 null ?
and why when i try system.out.println(indexes.get(0)) or system.out.println(indexes.get(1)) or system.out.println(indexes.get(2)) i get null ??
|
0

You should store each x in an list structure, like LinkedList. You can only have one match for each value of 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.

1 Comment

thanks for being helpful. could you elaborate a bit more ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.