Posts from December 2007

Dec
20
2007

in_array() and implode() for Java



Posted in Programming and Technology

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()

public 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()

public static 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;
}
Dec
9
2007

Limit and Offset clauses in MSSQL



Posted in Programming and Technology

Until SQL server 2008's released, there’s no easy way to page reults or only show the first n results. I searched around for a while until I found this on MSDN.

SELECT TOP limitnumber
    FROM (
        SELECT TOP (limitnumber/ offset) * limitnumber) * 
        FROM tablename 
            AS T1 
            WHERE clauses 
            ORDER BY sortfield DESC) 
        AS T2 
        ORDER BY sortfield ASC;

The LIMIT and OFFSET values are the same as in PostgreSQL or MySQL. limitnumber is the number to show per page, and offset is the starting row.

MSDN Article