Searching an array, help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flipflop
    New Member
    • Mar 2008
    • 2

    Searching an array, help!

    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!
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by flipflop
    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!

    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,

    Code:
     Color c = HashMapObject.get(String);
    You can use the HashMapObject to get all the string and use it in loop...

    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,
    trying to seach an array
    for me is confusing...


    Correct me if im wrong,
    Sukatoa

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Originally posted by flipflop
      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!
      Post a short example program of what you are trying to do.

      Comment

      • flipflop
        New Member
        • Mar 2008
        • 2

        #4
        <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

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Use 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.

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by r035198x
            Use 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.
            Indeed. Parallel arrays like this:

            [CODE=Java]times.depart[i] +times.where[i] +times.arrive[i][/CODE]

            Are a symptom of the code smell "object denial".

            Comment

            Working...