Reputation: 63
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 !
Upvotes: 0
Views: 126
Reputation: 3457
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:
Upvotes: 1
Reputation: 3502
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.
Upvotes: 0
Reputation: 726489
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.
Upvotes: 1