Dec
20
2007

in_array() and implode() for Java



Comments available as RSS 2.0

I'm still getting to grips with Java, but there a few functions I miss coming from PHP. in_array lacks a Java equivalent, as does the implode function. Here are the equvalent methods in Java:

implode()

static String implode(String[] ary, String delim) {
    String out = "";
    for(int i=0; i<ary.length; i++) {
        if(i!=0) { out += delim; }
        out += ary[i];
    }
    return out;
}

in_array()

private boolean in_array(DefaultListModel haystack, String needle) {
    for(int i=0;i<haystack.size();i++) {
        if(haystack.get(i).toString().equals(needle)) {
            return true;
        }
    }
    return false;
}
Like this post? Tell someone!
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Google
  • Technorati

Comments

  1. 1 milo

    super!!!! Really help a lot!!!

  2. 2 rgfdg

    You should use the StringBuilder in your implode method.

Leave a Comment

Login using OpenID or enter your details below to leave a comment.

OpenID
Anonymous


Comment