Tutorial by Examples: ce

var sequenceOfSequences = new [] { new [] { 1, 2, 3 }, new [] { 4, 5 }, new [] { 6 } }; var sequence = sequenceOfSequences.SelectMany(x => x); // returns { 1, 2, 3, 4, 5, 6 } Use SelectMany() if you have, or you are creating a sequence of sequences, but you want the result as one long sequen...
The following program: class Program { static void Method(params Object[] objects) { System.Console.WriteLine(objects.Length); } static void Method(Object a, Object b) { System.Console.WriteLine("two"); } static void Main(string[] a...
The following examples will not compile: string s = "\c"; char c = '\c'; Instead, they will produce the error Unrecognized escape sequence at compile time.
There are several places where you can use String.Format indirectly: The secret is to look for the overload with the signature string format, params object[] args, e.g.: Console.WriteLine(String.Format("{0} - {1}", name, value)); Can be replaced with shorter version: Console.WriteLine...
Declaration of an interface using the interface keyword: public interface Animal { String getSound(); // Interface methods are public by default } Override Annotation @Override public String getSound() { // Code goes here... } This forces the compiler to check that we are overri...
An exception can be caught and handled using the try...catch statement. (In fact try statements take other forms, as described in other examples about try...catch...finally and try-with-resources.) Try-catch with one catch block The most simple form looks like this: try { doSomething(); } ...
The following example shows the basics of throwing an exception: public void checkNumber(int number) throws IllegalArgumentException { if (number < 0) { throw new IllegalArgumentException("Number must be positive: " + number); } } The exception is thrown on the 3...
Resources can be added for a specific product flavor. For this example, assume that we have already defined two product flavors called free and paid. In order to add product flavor-specific resources, we create additional resource folders alongside the main/res folder, which we can then add resourc...
A Java class can implement multiple interfaces. public interface NoiseMaker { String noise = "Making Noise"; // interface variables are public static final by default String makeNoise(); //interface methods are public abstract by default } public interface FoodEater { ...
An interface can extend another interface via the extends keyword. public interface BasicResourceService { Resource getResource(); } public interface ExtendedResourceService extends BasicResourceService { void updateResource(Resource resource); } Now a class implementing ExtendedR...
Under most circumstances, it is simpler from a code-design standpoint to use existing generic Exception classes when throwing exceptions. This is especially true if you only need the exception to carry a simple error message. In that case, RuntimeException is usually preferred, since it is not a che...
If your model has private methods, the databinding library still allows you to access them in your view without using the full name of the method. Data model public class Item { private String name; public String getName() { return name; } } Layout XML <?xml versi...
From the documentation: The exception that is thrown when an application attempts to perform a networking operation on its main thread. This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their ma...
You can as well access other interface methods from within your default method. public interface Summable { int getA(); int getB(); default int calculateSum() { return getA() + getB(); } } public class Sum implements Summable { @Override public int get...
public class MyActivity extends Activity { private static final String PREFS_FILE = "NameOfYourPrefrenceFile"; // PREFS_MODE defines which apps can access the file private static final int PREFS_MODE = Context.MODE_PRIVATE; // you can use live template "key"...
public interface MyInterface { public void foo(); int bar(); public String TEXT = "Hello"; int ANSWER = 42; public class X { } class Y { } } Interface members always have public visibility, even if the public keyword is omitted. So both foo...
The ArrayIndexOutOfBoundsException is thrown when a non-existing index of an array is being accessed. Arrays are zero-based indexed, so the index of the first element is 0 and the index of the last element is the array capacity minus 1 (i.e. array.length - 1). Therefore, any request for an array e...
To create an IntentService, create a class which extends IntentService, and within it, a method which overrides onHandleIntent: package com.example.myapp; public class MyIntentService extends IntentService { @Override protected void onHandleIntent (Intent workIntent) { //Do so...
Starting a service is very easy, just call startService with an intent, from within an Activity: Intent intent = new Intent(this, MyService.class); //substitute MyService with the name of your service intent.putExtra(Intent.EXTRA_TEXT, "Some text"); //add any extra data to pass to the s...
Let's say you want to define an interface that allows publishing / consuming data to and from different types of channels (e.g. AMQP, JMS, etc), but you want to be able to switch out the implementation details ... Let's define a basic IO interface that can be re-used across multiple implementations...

Page 3 of 134