Skip to content

Instantly share code, notes, and snippets.

View MaksimDmitriev's full-sized avatar

Maksim Dmitriev MaksimDmitriev

View GitHub Profile
@MaksimDmitriev
MaksimDmitriev / AndroidManifest.xml
Last active August 29, 2015 14:06
Set text of a TextView programmatically and show it after the screen roitation
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mock_android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
@MaksimDmitriev
MaksimDmitriev / Foo
Last active March 15, 2016 08:04
If you call a method using reflection, and the method throws an exception, will the catch block you use to wrap the reflection, catch the exception thrown by the method you call?
package com.sample;
/**
* Created by Maksim on 3/15/2016.
*/
public class Foo {
public void bar() {
System.out.print("bar");
@MaksimDmitriev
MaksimDmitriev / A.java
Created May 15, 2016 16:14
Enum Serialization
package etf;
public class A {
public int t;
A(int t) {
this.t = t;
}
@MaksimDmitriev
MaksimDmitriev / Main.java
Created September 10, 2016 10:29
PublicInstance Singleton in Java. Why is it not a lazy initialization?
package mian;
import sample.PublicInstance;
public class Main {
public static void main(String[] args) {
System.out.println(PublicInstance.STRING);
}
@MaksimDmitriev
MaksimDmitriev / DoubleFoo.java
Last active September 10, 2016 11:20
Broken multithreaded version. OK, why don't we just make the Helper field volatile?
class DoubleFoo {
private volatile Helper d_helper;
public Helper getHelper() {
Helper result = d_helper;
if (result == null) {
synchronized(this) {
result = d_helper;
if (result == null) {
helper = result = new Helper();
}
public static void myUsFlagSort(int[] input, int stripeLen, int red, int white) {
// Input: 1, 2, 1, 1, 1, 2, 2, 2
// Output: 1, 1, 2, 2, 1, 1, 2, 2
// Let 1 be red
// Let 2 be white
// Red stripes are at 0, 1, 4, 5 ...
int redIndex = 0;
@MaksimDmitriev
MaksimDmitriev / FlagSorts.java
Created October 15, 2016 20:44
myUsFlagSort
private static void checkMyUsFlagSortInput(int[] input, int red, int white) {
if (input.length < 4) {
throw new IllegalArgumentException("the min input size is 4");
}
if (input.length % 2 != 0) {
throw new IllegalArgumentException("the input size " + input.length + " is wrong");
}
int redCount = 0;
int whiteCount = 0;
for (int elem : input) {
@MaksimDmitriev
MaksimDmitriev / RussianFlagProblem.java
Created October 15, 2016 20:47
russianFlagProblem
public static void russianFlagProblem(int[] input, int red, int white, int blue) {
// input: 1, 2, 3, 1, 1, 2, 3, 1, 1
// Dutch: 1, 1, 1, 1, 1, 2, 2, 3, 3
// (1 - red, 2 - white, 3 - blue)
// Russian: 2 - white, 3 - blue, 1 - red
// output: 2, 2, 3, 3, 1, 1, 1, 1, 1
int i = 0; // the end of the white region (exclusive)
@MaksimDmitriev
MaksimDmitriev / GetPermutations.java
Last active October 22, 2016 15:58
Find all permutations of source within input
public class Main {
public static void main(String[] args) {
System.out.println(getPermutations("bbca", "abbca"));
}
private static Set<String> getPermutations(String source, String input) {
if (source == null || input == null || source.isEmpty() || input.isEmpty()) {
return null;
}
@MaksimDmitriev
MaksimDmitriev / FibTailRec.java
Last active November 4, 2016 12:43
fibTailRec
private static long fibTailRec(long n, long c, long pPrev, long prev) {
if (c == 0) {
pPrev = 0;
} else if (c == 1) {
prev = 1;
}
if (c == n) {
return prev;
} else {
return fibTailRec(n, ++c, prev, pPrev + prev);