Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, July 11, 2012

[Java] Find common element between two arrays and put into new array

Find common element between two arrays and put into new array.

Monday, June 18, 2012

[Android] Save parameter before quit the activity

Save fontsize before finish current activity and call the value when activity is restarted.

public static final String KEY_MY_PREFERENCE = "my_preference";
int fontsize;
public void onCreate(Bundle savedInstanceState) 
{
     SharedPreferences prefs = getSharedPreferences("fontsize", MODE_PRIVATE);
     fontsize = prefs.getInt(KEY_MY_PREFERENCE, fontsize);
}
protected void onStop() {
     super.onStop();
     SharedPreferences prefs = getSharedPreferences("fontsize", MODE_PRIVATE);
     SharedPreferences.Editor editor = prefs.edit();
     editor.putInt(KEY_MY_PREFERENCE, fontsize);
     editor.commit();
}


Wednesday, June 6, 2012

[Java]Factorials

public int factorial(int n) {
        if (n == 0) return 1;
        else return (n * factorial(n-1));     // tail recursion
    }

public int factorial(int n) {
        int product = 1;
        int i;
        for (i = n; i >= 1; i--)    
            product = i * product;            // without tail recursion
        return product;
}