Posts

Showing posts from February, 2018

Implicit Intents

When using implicit intents, we have to check at least one app has to be there in the device to handle the intent. The way to check the sendIntent is,

Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage); sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type startActivity(sendIntent); if (sendIntent.resolveActivity(getPackageManager()) != null) { startActivity(sendIntent); }

Spiral Order Matrix Represention

Print the matrix in spiral order.

Steps

1. Declare four variables TOP, LEFT, RIGHT, BOTTOM
2. Then declare another variable called "dir"
3. first travel from Left to right ->, top to bottom , right to left <- amp="" bottom="" finally="" p="" to="" top="">4. increment the direction by one and change the four variables accordingly.
5. another point to note is as we travel through one node, remove the node. This is done by changing the four variables accordingly.
6. if the value of dir variable increases beyond 4, take the remainder of the direction by 4.


public class PrintSpiral { static int[][] A = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} }; public static void main(String[] args) { printSpiral(A, 4, 4); } private static void printSpiral(int[][] A, int row, int col) { int TOP= 0, LEFT= 0, BOTTOM= row-1, RIGHT= col-1; int dir = 0; while (TOP<=…

Swap two variables without a third variable

The code snippet to swap two variables a & b without a third variable,

private static void swap(int a, int b) { a = a + b; b = a - b; a = a - b; System.out.println("a: "+a+" b: "+b); }

Shortcut for System.out.println in intelliJ is cntrl+SPACE