Hi, I'm having some trouble in writing a piece of code. I'm trying to seach an array, but it has multiple keys. I have currently written code (an if loop nested within a for loop) which will find the first of the keys, but it then stops, not returning the others! If anyone could give me any tips on how to make it carry on searching, it would be much appreciated! Thanks!
Searching an array, help!
Collapse
X
-
Originally posted by flipflopHi, I'm having some trouble in writing a piece of code. I'm trying to seach an array, but it has multiple keys. I have currently written code (an if loop nested within a for loop) which will find the first of the keys, but it then stops, not returning the others! If anyone could give me any tips on how to make it carry on searching, it would be much appreciated! Thanks!
If you mean that you are using hashmap,
for example; HashMap<String, Color>....
You may want to use String as your key.... to find the color...
If i am right, then you can use for loop of it...
then try to get the Color from that hashmap...
As long as you know the String, use can implement an iteration that would compare them by all of you strings....just to get the needed color...
like,
You can use the HashMapObject to get all the string and use it in loop...Code:Color c = HashMapObject.get(String);
example,
Set<String> key = HashMapObject.k eySet();
It returns an array of String (Depends on what you have stored in you
HashMap) that is useful for iteration....
for( Initialize a String here: key)
it will iterate until the last String that you have stored in your
HashMap... then find the color...
If you feel it is too far from my assumptions,
Please be specific,for me is confusing...trying to seach an array
Correct me if im wrong,
Sukatoa -
Post a short example program of what you are trying to do.Originally posted by flipflopHi, I'm having some trouble in writing a piece of code. I'm trying to seach an array, but it has multiple keys. I have currently written code (an if loop nested within a for loop) which will find the first of the keys, but it then stops, not returning the others! If anyone could give me any tips on how to make it carry on searching, it would be much appreciated! Thanks!Comment
-
<code>
public static String destSearch(Stri ng searchDest){
for(int j=0; j<times.arrive. length;++j){
if(where[j].equalsIgnoreCa se(searchDest)) {
Scanner inn=new Scanner(System. in);
System.out.prin tln("Enter the time you want to get on");
Double requested_time = inn.nextDouble( );
System.out.flus h();
for(int i=j; i<times.depart. length;++i){
if(depart[i]==(requested_ti me)){
return times.depart[i] +times.where[i] +times.arrive[i];
}
else {
return times.depart[j] +times.where[j] +times.arrive[j];
}
}
}
}return "Not found";
}
</code>
this is the problematic code. i have three arrays in which journey details are stored: the positions of the data in them matches up, so if the place i wanted to go to was at position 1 in the "where" arrray, the times it left and arrived would be at the same positions in the other arrays.
i am aiming to make the above code look for the place they want to go, and when it finds that look through the "depart" array in order to find the time that they want to go. the problem at the moment is that it stops after it finds the first occurence of a place, whereas it should continue! any ideas would be welcomed!Comment
-
Indeed. Parallel arrays like this:Originally posted by r035198xUse OOP instead.
Create a Journey class with the appropriate fields.
Then you only need to store one ArrayList<Journ ey>.,
Arrays are so un-OOP.
[CODE=Java]times.depart[i] +times.where[i] +times.arrive[i][/CODE]
Are a symptom of the code smell "object denial".Comment
Comment