Tutorial by Examples: ar

Snippet public void DoSomething(int paramValue) { Console.WriteLine(nameof(paramValue)); } ... int myValue = 10; DoSomething(myValue); Console Output paramValue
Snippet public class SomeClass<TItem> { public void PrintTypeName() { Console.WriteLine(nameof(TItem)); } } ... var myClass = new SomeClass<int>(); myClass.PrintTypeName(); Console.WriteLine(nameof(SomeClass<int>)); Console Output TItem S...
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...
We can create an ArrayList (following the List interface): List aListOfFruits = new ArrayList(); Java SE 5 List<String> aListOfFruits = new ArrayList<String>(); Java SE 7 List<String> aListOfFruits = new ArrayList<>(); Now, use the method add to add a String: ...
The LinearLayout is a ViewGroup that arranges its children in a single column or a single row. The orientation can be set by calling the method setOrientation() or using the xml attribute android:orientation. Vertical orientation : android:orientation="vertical" <?xml version=&quo...
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...
This example will start DestinationActivity from OriginActivity. Here, the Intent constructor takes two parameters: A Context as its first parameter (this is used because the Activity class is a subclass of Context) The Class of the app component to which the system should deliver the Intent (i...
Map<Integer, String> map = new HashMap<>(); map.put(1, "First element."); map.put(2, "Second element."); map.put(3, "Third element."); map.clear(); System.out.println(map.size()); // => 0
Consider the following JSON string: { "title": "test", "content": "Hello World!!!", "year": 2016, "names" : [ "Hannah", "David", "Steve" ] } This JSON object can...
// Create a new instance of a JSONArray JSONArray array = new JSONArray(); // With put() you can add a value to the array. array.put("ASDF"); array.put("QWERTY"); // Create a new instance of a JSONObject JSONObject obj = new JSONObject(); try { // Add the JSONAr...
Two methods in java.util.Collection create an array from a collection: Object[] toArray() <T> T[] toArray(T[] a) Object[] toArray() can be used as follows: Java SE 5 Set<String> set = new HashSet<String>(); set.add("red"); set.add("blue"); ...
Methods can also have generic type parameters. public class Example { // The type parameter T is scoped to the method // and is independent of type parameters of other methods. public <T> List<T> makeList(T t1, T t2) { List<T> result = new ArrayList<T&...
Java SE 5 Since Java 1.5 you can get a String representation of the contents of the specified array without iterating over its every element. Just use Arrays.toString(Object[]) or Arrays.deepToString(Object[]) for multidimentional arrays: int[] arr = {1, 2, 3, 4, 5}; System.out.println(Arrays.toS...
In order to define a string array write in a resources file res/values/filename.xml <string-array name="string_array_name"> <item>text_string</item> <item>@string/string_id</item> </string-array> for example res/values/arrays.xml <?x...
In order to define an integer array write in a resources file res/values/filename.xml <integer-array name="integer_array_name"> <item>integer_value</item> <item>@integer/integer_id</item> </integer-array> for example res/values/arrays.xm...
In order to compare Strings for equality, you should use the String object's equals or equalsIgnoreCase methods. For example, the following snippet will determine if the two instances of String are equal on all characters: String firstString = "Test123"; String secondString = "Test...
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"...
A Toolbar is a generalization of ActionBar for use within application layouts. While an ActionBar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. It can be added by performing t...
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...

Page 3 of 218