Tutorial by Examples

It's possible to break / continue to an outer loop by using label statements: outerloop: for(...) { innerloop: for(...) { if(condition1) break outerloop; if(condition2) continue innerloop; // equivalent to: continue; } } There is no ...
The continue statement is used to skip the remaining steps in the current iteration and start with the next loop iteration. The control goes from the continue statement to the step value (increment or decrement), if any. String[] programmers = {"Adrian", "Paul", "John&quot...
Write to a file test.txt: String filepath ="C:\\test.txt"; FileOutputStream fos = null; try { fos = new FileOutputStream(filepath); byte[] buffer = "This will be written in test.txt".getBytes(); fos.write(buffer, 0, buffer.length); fos.close(); } c...
By default class annotations do not apply to types extending them. This can be changed by adding the @Inherited annotation to the annotation definition Example Consider the following 2 Annotations: @Inherited @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Inher...
public static void Main(string[] args) { var studentList = new List<Student>(); studentList.Add(new Student("Scott", "Nuke")); studentList.Add(new Student("Vincent", "King")); studentList.Add(new Student("Craig", "Be...
If you want the Value Types vs Reference Types in methods example to work properly, use the ref keyword in your method signature for the parameter you want to pass by reference, as well as when you call the method. public static void Main(string[] args) { ... DoubleNumber(ref number); ...
User is an ActiveRecord or Mongoid class. Replace User with any Rails class in your project (even something like Integer or Array) my_string = "User" # Capitalized string # => 'User' my_constant = my_string.safe_constantize # => User my_constant.all.count # => 18 my...
This example will not work because the string passed in isn't recognized as a constant in the project. Even if you pass in "array", it won't work as it isn't capitalized. my_string = "not_a_constant" # => 'not_a_constant' my_string.safe_constantize # => nil my_s...
If you are given a JSON string : val str = """{ | "name" : "Jsony McJsonface", | "age" : 18, | "hobbies" : [ "Fishing", "Hunting", "Camping" ], | "pet" : { | ...
C-style bit manipulation A bit can be set using the bitwise OR operator (|). // Bit x will be set number |= 1LL << x; Using std::bitset set(x) or set(x,true) - sets bit at position x to 1. std::bitset<5> num(std::string("01100")); num.set(0); // num is now 01101 ...
C-style bit-manipulation A bit can be cleared using the bitwise AND operator (&). // Bit x will be cleared number &= ~(1LL << x); Using std::bitset reset(x) or set(x,false) - clears the bit at position x. std::bitset<5> num(std::string("01100")); num.reset(2); ...
C-style bit-manipulation A bit can be toggled using the XOR operator (^). // Bit x will be the opposite value of what it is currently number ^= 1LL << x; Using std::bitset std::bitset<4> num(std::string("0100")); num.flip(2); // num is now 0000 num.flip(0); // num is n...
C-style bit-manipulation The value of the bit can be obtained by shifting the number to the right x times and then performing bitwise AND (&) on it: (number >> x) & 1LL; // 1 if the 'x'th bit of 'number' is set, 0 otherwise The right-shift operation may be implemented as either a...
C-style bit-manipulation // Bit n will be set if x is 1 and cleared if x is 0. number ^= (-x ^ number) & (1LL << n); Using std::bitset set(n,val) - sets bit n to the value val. std::bitset<5> num(std::string("00100")); num.set(0,true); // num is now 00101 num.set(...
Self-referential association is used to associate a model with itself. The most frequent example would be, to manage association between a friend and his follower. ex. rails g model friendship user_id:references friend_id:integer now you can associate models like; class User < ActiveRecord:...
The ng-required adds or removes the required validation attribute on an element, which in turn will enable and disable the require validation key for the input. It is used to optionally define if an input element is required to have a non-empty value. The directive is helpful when designing validat...
#include <stddef.h> // size_t, ptrdiff_t //----------------------------------- Machinery: using Size = ptrdiff_t; template< class Item, size_t n > constexpr auto n_items( Item (&)[n] ) noexcept -> Size { return n; } //----------------------------------- Us...
// Example of raw dynamic size array. It's generally better to use std::vector. #include <algorithm> // std::sort #include <iostream> using namespace std; auto int_from( istream& in ) -> int { int x; in >> x; return x; } auto main() -> int { ...
// Example of std::vector as an expanding dynamic size array. #include <algorithm> // std::sort #include <iostream> #include <vector> // std::vector using namespace std; int int_from( std::istream& in ) { int x = 0; in >> x; return x; } ...
// A fixed size raw array matrix (that is, a 2D raw array). #include <iostream> #include <iomanip> using namespace std; auto main() -> int { int const n_rows = 3; int const n_cols = 7; int const m[n_rows][n_cols] = // A raw array matrix. ...

Page 390 of 1336