3 Replies Latest reply: Mar 7, 2007 11:27 AM by 807606 RSS

    comparing a string with a stored list(or array or ...)

    800241
      Hi all. I am trying to figure out the best way to do the following:
      I need to store serveral strings into a structure so that I can compare them against a string and see if the string is contained in the previously mentioned strings.
      I want to make the structure static, as it should not change, and I only need one instance of it, but I can't seem to come up with something I like. I was thinking of just using a string array and then running a loop comparing each position in the array to my string, but that seems inelegant to me for some reason. Anyone have any thoughts, or is that pretty much the best I am going to do ?

      thanks in advance.
        • 1. Re: comparing a string with a stored list(or array or ...)
          807606
          seems simple and elegant enough to me
          • 2. Re: comparing a string with a stored list(or array or ...)
            800241
            seems simple and elegant enough to me
            Alright. Thanks George. I think i've been reading a bit too much about how to make code simple and elegant, and now it's making me second guess my code a bit more than I perhaps ought to. I thought seeing how easy it is to use this forum I would get another coder's opinion.
            • 3. Re: comparing a string with a stored list(or array or ...)
              807606
              I might be inclined to use an ArrayList instead of an array of Strings, which would allow me to use use the contains method to do object comparisons so my code would end up looking more like this.
                      ArrayList list = new ArrayList() ;
                      list.add("Fred") ;
                      list.add("Barney") ;
                      list.add("Wilma") ;
                      list.add("Betty") ;
                      
                      if(list.contains("Barney"))
                      {
                          System.out.println("Hey Barney") ;
                      }
                      else
                      {
                          System.out.println("Absence of Barney detected") ;
                      }
              But it's still doing the same thing you're describing under the hood.

              Hope this helps,

              PS.