Tutorial by Examples: al

class Example { public string Foobar { get; set; } public List<string> Names { get; set; } public Example() { Foobar = "xyz"; Names = new List<string>(){"carrot","fox","ball"}; } }
Basic cases int[] numbers1 = new int[3]; // Array for 3 int values, default value is 0 int[] numbers2 = { 1, 2, 3 }; // Array literal of 3 int values int[] numbers3 = new int[] { 1, 2, 3 }; // Array of 3 int values initialized int[][] numbers4 = { { 1, 2...
Java 7 introduced the very useful Files class Java SE 7 import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.Path; Path path = Paths.get("path/to/file"); try { byte[] data = Files.readAllBytes(path); } catch(IOException e) { e.printStackTrace();...
package com.example; import android.os.Bundle; import android.support.annotation.Nullable; import android.util.Log; import android.view.View; import android.view.ViewTreeObserver; public class ExampleActivity extends Activity { @Override protected void onCreate(@Nullable final ...
If you need to produce a JSON string with a value of null like this: { "name":null } Then you have to use the special constant JSONObject.NULL. Functioning example: jsonObject.put("name", JSONObject.NULL);
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"...
The Arrays.asList() method can be used to return a fixed-size List containing the elements of the given array. The resulting List will be of the same parameter type as the base type of the array. String[] stringArray = {"foo", "bar", "baz"}; List<String> stringL...
It is possible to define an array with more than one dimension. Instead of being accessed by providing a single index, a multidimensional array is accessed by specifying an index for each dimension. The declaration of multidimensional array can be done by adding [] for each dimension to a regular a...
Updating the state of a Swing component must happen on the Event Dispatch Thread (the EDT). The javax.swing.Timer triggers its ActionListener on the EDT, making it a good choice to perform Swing operations. The following example updates the text of a JLabel each two seconds: //Use a timer to updat...
If your computation produces some return value which later is required, a simple Runnable task isn't sufficient. For such cases you can use ExecutorService.submit(Callable<T>) which returns a value after execution completes. The Service will return a Future which you can use to retrieve the r...
TL;DR == tests for reference equality (whether they are the same object) .equals() tests for value equality (whether they are logically "equal") equals() is a method used to compare two objects for equality. The default implementation of the equals() method in the Object class returns...
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class User { private long userID; private String name; // getters and setters } By using the annotation XMLRootElement, we can mark a class as a root element of an XML file. import java.io.File; ...
To read an XML file named UserDetails.xml with the below content <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <user> <name>Jon Skeet</name> <userID>8884321</userID> </user> We need a POJO class named U...
By default, any files that you save to Internal Storage are private to your application. They cannot be accessed by other applications, nor the user under normal circumstances. These files are deleted when the user uninstalls the application. To Write Text to a File String fileName= "hellowor...
Don't just use Optional.get() since that may throw NoSuchElementException. The Optional.orElse(T) and Optional.orElseGet(Supplier<? extends T>) methods provide a way to supply a default value in case the Optional is empty. String value = "something"; return Optional.ofNullable(v...
Use the orElseThrow() method of Optional to get the contained value or throw an exception, if it hasn't been set. This is similar to calling get(), except that it allows for arbitrary exception types. The method takes a supplier that must return the exception to be thrown. In the first example, the...
By default, WebView does not implement JavaScript alert dialogs, ie. alert() will do nothing. In order to make you need to firstly enable JavaScript (obviously..), and then set a WebChromeClient to handle requests for alert dialogs from the page: webView.setWebChromeClient(new WebChromeClient() { ...
OptionalDouble, OptionalInt and OptionalLong work like Optional, but are specifically designed to wrap primitive types: OptionalInt presentInt = OptionalInt.of(value); OptionalInt absentInt = OptionalInt.empty(); Because numeric types do have a value, there is no special handling for null. Empt...
Calendar objects can be created by using getInstance() or by using the constructor GregorianCalendar. It's important to notice that months in Calendar are zero based, which means that JANUARY is represented by an int value 0. In order to provide a better code, always use Calendar constants, such as...
add() and roll() can be used to increase/decrease Calendar fields. Calendar calendar = new GregorianCalendar(2016, Calendar.MARCH, 31); // 31 March 2016 The add() method affects all fields, and behaves effectively if one were to add or subtract actual dates from the calendar calendar.add(Calend...

Page 4 of 269